国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

react實現記錄拖動排序

瀏覽:6日期:2022-06-13 11:14:30

最近項目中要做一個拖動排序功能,首先想到的是之前項目中用過的antd自帶的tree和table的拖動排序,但是只能在對應的組建里使用。這里用的是自定義組件,隨意拖動排序,所以記錄一下實現流程

react-dnd antd組件的拖動排序都是用的這個庫,使用比較靈活,但是要配置的東西比較多,需求復雜時使用這個庫;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),);<DndProvider backend={HTML5Backend}> <Table columns={columns} dataSource={this.state.data} components={this.components} onRow={(record, index) => ({ index, moveRow: this.moveRow, })} /></DndProvider>react-beautiful-dnd 在項目中看到引用了這個庫,使用起來也不算復雜,就試著用了這個庫,不過只支持水平或垂直拖動,一行或者一列元素時可以使用,可惜這個需求時兩行多列元素,也沒辦法用;<DragDropContext onDragEnd={this.onDragEnd}> <Droppable droppableId='phone-droppable'> {droppableProvided => ( <div ref={droppableProvided.innerRef} {...droppableProvided.droppableProps}>{this.state.phoneImages.map((image, index) => ( <Draggable key={image||'upload'} draggableId={image||'upload'} index={index}> {(provided, snapshot) => ( <divref={provided.innerRef}{...provided.draggableProps}{...provided.dragHandleProps}style={{ ...provided.draggableProps.style, opacity: snapshot.isDragging ? 0.8 : 1, display: 'inline-block'}} ><img src={img}/> </div> )} </Draggable>))}{droppableProvided.placeholder} </div> )} </Droppable></DragDropContext>react-sortable-hoc 最后在網上搜索的時候,又看到這個庫,使用起來比較簡單,使用SortableList包裹要拖拽元素的容器,SortableElement包裹要拖拽的子元素,設置容器拖拽方向axis={'xy'}即可使grid布局的多個元素支持水平和豎直方向拖動排序;const SortableItem = SortableElement(({children}) => ( <div>{children}</div>));const SortableList = SortableContainer(({children}) => { return ( <div style={{display: 'grid', gridTemplateRows: '117px 117px', gridTemplateColumns: '120px 120px 120px 120px'}}> {children} </div> );});<SortableList onSortEnd={this.onPadSortEnd} helperClass={Styles.sortableHelper} axis={'xy'}> {this.state.padImages.map((image, index) => ( <SortableItem key={`pad-item-${index}`} index={index} className={Styles.sortableItem}> <img src={img}/> </SortableItem> ))}</SortableList>

好久沒更新博客了,最近工作比較忙,差不多每天都要加班,中間有經歷搬家,沒時間坐下來總結學到的東西。工作的時候因為這塊花費了一些時間,想到可能別人也會遇到類似問題,所以就記錄下來了

到此這篇關于react實現記錄拖動排序的文章就介紹到這了,更多相關react記錄拖動排序內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: JavaScript
主站蜘蛛池模板: 桐柏县| 吉安市| 铜川市| 海盐县| 巴塘县| 平泉县| 南通市| 安西县| 巩留县| 利辛县| 江安县| 枞阳县| 运城市| 乌拉特前旗| 昆明市| 府谷县| 桑日县| 康定县| 正镶白旗| 通化市| 白朗县| 肇源县| 同仁县| 井冈山市| 安乡县| 普宁市| 道真| 建始县| 大竹县| 额敏县| 长沙县| 喀喇| 方山县| 阳信县| 九江县| 山东省| 尖扎县| 新郑市| 论坛| 壤塘县| 台湾省|