欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ant design中實(shí)現(xiàn)table的表格行的拖拽

 更新時(shí)間:2022年03月22日 11:11:48   作者:前端歌謠  
這篇文章主要介紹了ant design中實(shí)現(xiàn)table的表格行的拖拽,文章圍繞table表格行拖拽實(shí)現(xiàn)的相關(guān)資料展開詳細(xì)的代碼內(nèi)容,具有一定的參考價(jià)值,需要的小伙伴可以參考一下

前言:

 首先剛開始知道要書寫一個(gè)這樣的功能我的內(nèi)心是比較崩潰的 完全沒有思路,   然后就打開官網(wǎng)的文檔進(jìn)行觀看。一開始準(zhǔn)備寫 打開官網(wǎng)的一個(gè)文檔是4.0的 看起來也是一臉的蒙蔽,接著找到3的一個(gè)文檔,接下來直接說說如何讓實(shí)現(xiàn)當(dāng)前這個(gè)功能上代碼

代碼部分:

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;
?
class BodyRow extends React.Component {
? render() {
? ? const { isOver, connectDragSource, connectDropTarget, moveRow, ...restProps }?
? ? = this.props;
? ? const style = { ...restProps.style, 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) {
? ? dragingIndex = props.index;
? ? return {
? ? ? index: props.index,
? ? };
? },
};
?
const rowTarget = {
? 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('row', rowTarget, (connect, monitor) => ({
? connectDropTarget: connect.dropTarget(),
? isOver: monitor.isOver(),
}))(
? DragSource('row', rowSource, connect => ({
? ? connectDragSource: connect.dragSource(),
? }))(BodyRow),
);
?
const columns = [
? {
? ? title: 'Name',
? ? dataIndex: 'name',
? ? key: 'name',
? },
? {
? ? title: 'Age',
? ? dataIndex: 'age',
? ? key: 'age',
? },
? {
? ? title: 'Address',
? ? dataIndex: 'address',
? ? key: 'address',
? },
];
?
class DragSortingTable extends React.Component {
? state = {
? ? data: [
? ? ? {
? ? ? ? key: '1',
? ? ? ? name: 'John Brown',
? ? ? ? age: 32,
? ? ? ? address: 'New York No. 1 Lake Park',
? ? ? },
? ? ? {
? ? ? ? key: '2',
? ? ? ? name: 'Jim Green',
? ? ? ? age: 42,
? ? ? ? address: 'London No. 1 Lake Park',
? ? ? },
? ? ? {
? ? ? ? key: '3',
? ? ? ? name: 'Joe Black',
? ? ? ? age: 32,
? ? ? ? address: 'Sidney No. 1 Lake Park',
? ? ? },
? ? ],
? };
?
? components = {
? ? body: {
? ? ? row: DragableBodyRow,
? ? },
? };
?
? moveRow = (dragIndex, hoverIndex) => {
? ? 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
? ? ? ? ? columns={columns}
? ? ? ? ? dataSource={this.state.data}
? ? ? ? ? components={this.components}
? ? ? ? ? onRow={(record, index) => ({
? ? ? ? ? ? index,
? ? ? ? ? ? moveRow: this.moveRow,
? ? ? ? ? })}
? ? ? ? />
? ? ? </DndProvider>
? ? );
? }
}
?
ReactDOM.render(<DragSortingTable />, mountNode);
#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;
}

這是官網(wǎng)的示例 ,那么我們?nèi)绾螌?shí)現(xiàn)呢?

第一步 引入第一個(gè)類

第二步 找準(zhǔn)數(shù)據(jù)

第三步 進(jìn)行數(shù)據(jù)的一個(gè)賦值我這邊是dva.js賦值

第四步 回調(diào)或者確定按鈕處理數(shù)據(jù)(這邊是確定按鈕處理值然后調(diào)接口)

總結(jié):

這樣的話 表格行就可以進(jìn)行拖拽操作了

到此這篇關(guān)于ant design中實(shí)現(xiàn)table的表格行的拖拽的文章就介紹到這了,更多相關(guān)table表格行拖拽實(shí)現(xiàn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論