我一直在编码这个简单的单列拖放表,但是当我拖动它们时行并没有移动。我在哪里修复或检查?
我正在使用React,AntDesign和JavaScript(带有TypeScript)
import * as React from 'react';
import ReactDOM from "react-dom";
import { Table } from "antd";
import { DndProvider, DragSource, DropTarget } from "react-dnd";
import HTML5Backend from "react-dnd-html5-backend";
import update from "immutability-helper";
let dragingIndex = -1;
interface propsDD {
isOver: any,
connectDragSource: any,
connectDropTarget: any,
moveRow: any,
restProps: {
readonly [x: string]: any;
children?: React.ReactNode;
}
className: any,
index: any,
}
class BodyRow extends React.Component<propsDD>{
render() {
const { isOver, connectDragSource, connectDropTarget, moveRow, ...restProps } = this.props;
const style = { ...restProps, cursor: 'move' };
let { className } = restProps;
if (isOver) {
if (restProps.index > dragingIndex) {
className += " drop-over-downward";
}
if (restProps.index < dragingIndex) {
className += " drop-over-upward";
}
}
return connectDragSource(
connectDropTarget(
<tr {...restProps} className={className} style={style} />
)
);
}
}
const rowSource = {
beginDrag(props: any) {
dragingIndex = props.index;
return {
index: props.index,
};
},
};
const rowTarget = {
drop(props: any, monitor: any) {
const dragIndex = monitor.getItem().index;
const hoverIndex = props.index;
if (dragIndex === hoverIndex) {
return;
}
props.moveRow(dragIndex, hoverIndex);
monitor.getItem().index = hoverIndex;
},
};
const DragableBodyRow = DropTarget("row", rowTarget, (connect, monitor) => ({
connectDropTarget: connect.dropTarget(),
isOver: monitor.isOver()
}))(
DragSource("row", rowSource, connect => ({
connectDragSource: connect.dragSource()
}))(BodyRow)
);
const columns = [
{
title: 'Orden de Ejecución',
dataIndex: 'attributes.name',
key: 'name',
},
];
type propsFromList = {
receivedTasks: Task[],
onReceivedTasks: (tasks: Task[]) => void,
}
export default class DDTasks extends React.Component<propsFromList, State>{
public state: State = {
data: [],
};
components = {
body: {
row: DragableBodyRow,
},
};
onReceivedTasks(tasks: Task[]): void {
this.setState({
data: this.props.receivedTasks,
} as State)
}
moveRow = (dragIndex: any, hoverIndex: any) => {
const { data } = this.state;
const dragRow = data[dragIndex];
this.setState(
update(this.state, {
data: {
$splice: [[dragIndex, 1], [hoverIndex, 0, dragRow]]
}
})
);
};
render() {
return (
< DndProvider backend={HTML5Backend} >
<Table
rowKey="id"
bordered={true}
pagination={false}
columns={columns}
dataSource={this.props.receivedTasks}
components={this.components}
onRow={(index) => ({
index,
moveRow: this.moveRow,
})}
/>
</DndProvider >
);
}
}
我希望拖动行。这些行的内容实际上是显示的。
答案 0 :(得分:1)
这是一个简单的示例,它基于我在网上发现的许多不同内容,最终提出了一些适用于我的代码的内容:
基本上,这是一个React组件表的主体,我正在通过索引映射位置列表,并且每一行都可以拖动以允许用户修改表。我还包括了与属性关联的函数调用
return (
<Tbody>
{items.map((item, index) => (
<Tr key={index} className="item.name"
draggable={true}
onDragStart={this.dragstart}
onDragEnd={this.dragend}
onDragLeave={this.dragend}
onDragEnter={this.dragend}
onDrop={this.drop}
onDragOver={this.dragend}
id={index} >
<Td><span>{item.name}</span></Td>
<Td><span>{item.latitude}</span></Td>
<Td><span>{item.longitude}</span></Td>
<Td type="data"><span>{this.checkForZero(this.props.propDistances[index])}</span></Td>
<Td type="data"><span>{this.checkForZero(this.props.propCumulativeDist[index])}</span></Td>
</Tr>
))}
{this.finishItineraryTable()}
</Tbody>
);
dragstart(event) {
let dataTransfer = event.dataTransfer;
let node = event.target;
dataTransfer.setData('text/plain',node.innerHTML);
dataTransfer.setData('id',node.id);
event.stopPropagation();
}
dragend(event) {
event.preventDefault();
}
drop(event) {
event.preventDefault();
let dragObjHtml = event.dataTransfer.getData("text/plain");
let dragObjId = document.getElementById(event.dataTransfer.getData("id"));
let dropTarget = event.target.closest("tr");
let temp = dropTarget.innerHTML;
dropTarget.innerHTML = dragObjHtml;
dragObjId.innerHTML = temp;
}
答案 1 :(得分:0)
您是否添加了可排序表格的CSS以显示拖动效果?
#components-table-demo-drag-sorting tr.drop-over-downward td {
border-bottom: 2px dashed #1890ff;
}
#components-table-demo-drag-sorting tr.drop-over-upward td {
border-top: 2px dashed #1890ff;
}
答案 2 :(得分:0)
在粘贴我长长的代码之前,我想说几件事:
<table>
。onDragSort
,每次发生某种拖动排序时都会被调用。该参数包括排序后的数据。首先安装软件包react-dnd
和react-dnd-html5-backend
。将此添加到您的DragSortingTable.tsx
:
import * as React from 'react';
import Table, { TableProps, TableComponents } from 'antd/lib/table';
import { DndProvider, DropTarget, DragSource, DragElementWrapper, DropTargetSpec } from 'react-dnd';
import HTML5Backend from 'react-dnd-html5-backend';
let dragingIndex = -1;
interface BodyRowProps {
isOver: boolean;
connectDragSource: DragElementWrapper<{}>;
connectDropTarget: DragElementWrapper<{}>;
style?: React.CSSProperties;
index: number;
className?: string;
}
const BodyRow: React.FC<BodyRowProps> = props => {
const style = { ...props.style, cursor: 'move' };
let { className } = props;
if (props.isOver) {
if (props.index > dragingIndex) {
className += ' drop-over-downward';
}
if (props.index < dragingIndex) {
className += ' drop-over-upward';
}
}
return props.connectDragSource(
props.connectDropTarget(
<tr className={className} style={style}>
{props.children}
</tr>
)
);
};
const rowSource = {
beginDrag(props) {
dragingIndex = props.index;
return {
index: props.index
};
}
};
interface DropProps {
index: number;
moveRow: (dragIndex: number, hoverIndex: number) => void;
}
const rowTarget: DropTargetSpec<DropProps> = {
drop(props, monitor) {
const dragIndex = monitor.getItem().index;
const hoverIndex = props.index;
// Don't replace items with themselves
if (dragIndex === hoverIndex) {
return;
}
// Time to actually perform the action
props.moveRow(dragIndex, hoverIndex);
// Note: we're mutating the monitor item here!
// Generally it's better to avoid mutations,
// but it's good here for the sake of performance
// to avoid expensive index searches.
monitor.getItem().index = hoverIndex;
}
};
const DragableBodyRow = DropTarget<DropProps>('row', rowTarget, (connect, monitor) => ({
connectDropTarget: connect.dropTarget(),
isOver: monitor.isOver()
}))(
DragSource('row', rowSource, connect => ({
connectDragSource: connect.dragSource()
}))(BodyRow)
);
interface DragSortingTableProps<T> extends TableProps<T> {
onDragSort?: (sortedData: T[]) => void;
}
type extractTableType<T> = T extends DragSortingTableProps<infer T> ? T : never;
export const DragSortingTable: <T>(
props: DragSortingTableProps<T>
) => React.ReactElement<DragSortingTableProps<T>> = props => {
const [dataSource, setDataSource] = React.useState(props.dataSource);
React.useEffect(() => {
setDataSource(props.dataSource);
}, [props.dataSource]);
const components: TableComponents = {
body: {
row: DragableBodyRow
}
};
const moveRow: DropProps['moveRow'] = (dragIndex, hoverIndex) => {
const dragRow = dataSource[dragIndex];
const remaining = dataSource.filter(i => i !== dragRow);
const sorted = [...remaining.slice(0, hoverIndex), dragRow, ...remaining.slice(hoverIndex)];
setDataSource(sorted);
if (props.onDragSort) {
props.onDragSort(sorted);
}
};
const tableProps: TableProps<extractTableType<typeof props>> = {
...props,
className: props.className ? props.className + ' drag-sorting-table' : 'drag-sorting-table',
components,
dataSource,
onRow: (_record, index) => ({ index, moveRow })
};
return (
<DndProvider backend={HTML5Backend}>
<Table {...tableProps}>{props.children}</Table>
</DndProvider>
);
};
在antd.less
覆盖中添加以下样式:
.drag-sorting-table tr.drop-over-downward td {
border-bottom: 2px dashed @primary-color;
}
.drag-sorting-table tr.drop-over-upward td {
border-top: 2px dashed @primary-color;
}
按如下所示使用新的拖动排序表:
<DragSortingTable<Users>
dataSource={props.users}
rowKey='id'
>
<Column
title='Title'
dataIndex='id'
key='id'
/>
</DragSortingTable>