ant design中實(shí)現(xiàn)table的表格行的拖拽
前言:
首先剛開始知道要書寫一個(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)文章
JavaScript實(shí)現(xiàn)16進(jìn)制顏色值轉(zhuǎn)RGB的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)16進(jìn)制顏色值轉(zhuǎn)RGB的方法,是javascript比較典型的數(shù)值轉(zhuǎn)換應(yīng)用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-02-02手寫TypeScript?時(shí)很多人常犯的幾個(gè)錯(cuò)誤
這篇文章主要介紹了手寫TypeScript?時(shí)很多人常犯的幾個(gè)錯(cuò)誤,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的抽卡,重要的朋友可以參考一下2022-09-09Bootstrap柵格系統(tǒng)使用方法及頁面調(diào)整變形的解決方法
這篇文章主要介紹了Bootstrap柵格系統(tǒng)使用方法及頁面調(diào)整變形的解決方法,需要的朋友可以參考下2017-03-03JavaScript在網(wǎng)頁中畫圓的函數(shù)arc使用方法
這篇文章主要介紹了JavaScript在網(wǎng)頁中畫圓的函數(shù)arc使用方法的相關(guān)資料,需要的朋友可以參考下2015-11-11JS判斷form內(nèi)所有表單是否為空的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)硪黄狫S判斷form內(nèi)所有表單是否為空的簡(jiǎn)單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09