react實(shí)現(xiàn)記錄拖動(dòng)排序
最近項(xiàng)目中要做一個(gè)拖動(dòng)排序功能,首先想到的是之前項(xiàng)目中用過(guò)的antd自帶的tree和table的拖動(dòng)排序,但是只能在對(duì)應(yīng)的組建里使用。這里用的是自定義組件,隨意拖動(dòng)排序,所以記錄一下實(shí)現(xiàn)流程
- react-dnd antd組件的拖動(dòng)排序都是用的這個(gè)庫(kù),使用比較靈活,但是要配置的東西比較多,需求復(fù)雜時(shí)使用這個(gè)庫(kù);
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 在項(xiàng)目中看到引用了這個(gè)庫(kù),使用起來(lái)也不算復(fù)雜,就試著用了這個(gè)庫(kù),不過(guò)只支持水平或垂直拖動(dòng),一行或者一列元素時(shí)可以使用,可惜這個(gè)需求時(shí)兩行多列元素,也沒辦法用;
<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) => ( <div ref={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 最后在網(wǎng)上搜索的時(shí)候,又看到這個(gè)庫(kù),使用起來(lái)比較簡(jiǎn)單,使用SortableList包裹要拖拽元素的容器,SortableElement包裹要拖拽的子元素,設(shè)置容器拖拽方向
axis={'xy'}即可使grid布局的多個(gè)元素支持水平和豎直方向拖動(dòng)排序;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>
好久沒更新博客了,最近工作比較忙,差不多每天都要加班,中間有經(jīng)歷搬家,沒時(shí)間坐下來(lái)總結(jié)學(xué)到的東西。工作的時(shí)候因?yàn)檫@塊花費(fèi)了一些時(shí)間,想到可能別人也會(huì)遇到類似問(wèn)題,所以就記錄下來(lái)了
到此這篇關(guān)于react實(shí)現(xiàn)記錄拖動(dòng)排序的文章就介紹到這了,更多相關(guān)react記錄拖動(dòng)排序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- react-dnd實(shí)現(xiàn)任意拖動(dòng)與互換位置
- react-native 圓弧拖動(dòng)進(jìn)度條實(shí)現(xiàn)的示例代碼
- react項(xiàng)目中使用react-dnd實(shí)現(xiàn)列表的拖拽排序功能
- react中實(shí)現(xiàn)拖拽排序react-dnd功能
- react?實(shí)現(xiàn)表格列表拖拽排序的示例
- React如何使用sortablejs實(shí)現(xiàn)拖拽排序
- React.js組件實(shí)現(xiàn)拖拽排序組件功能過(guò)程解析
- react.js組件實(shí)現(xiàn)拖拽復(fù)制和可排序的示例代碼
相關(guān)文章
關(guān)于React狀態(tài)管理的三個(gè)規(guī)則總結(jié)
隨著 JavaScript 單頁(yè)應(yīng)用開發(fā)日趨復(fù)雜,JavaScript 需要管理比任何時(shí)候都要多的 state (狀態(tài)),這篇文章主要給大家介紹了關(guān)于React狀態(tài)管理的三個(gè)規(guī)則,需要的朋友可以參考下2021-07-07
在 React 項(xiàng)目中優(yōu)雅實(shí)現(xiàn)新用戶引導(dǎo)HagiCode 的 driver.js
本文介紹了在HagiCode項(xiàng)目中使用driver.js實(shí)現(xiàn)新用戶引導(dǎo)的經(jīng)驗(yàn),通過(guò)輕量級(jí)、簡(jiǎn)潔API和靈活配置,配合狀態(tài)管理、目標(biāo)元素定位和動(dòng)態(tài)導(dǎo)入等技術(shù)手段,實(shí)現(xiàn)精準(zhǔn)的用戶引導(dǎo),旨在幫助新用戶快速理解產(chǎn)品工作流,并提高用戶體驗(yàn),感興趣的朋友跟隨小編一起看看吧2026-04-04
React使用TypeScript的最佳實(shí)踐和技巧
在React項(xiàng)目中使用TypeScript可以顯著提高代碼的可維護(hù)性和可讀性,并提供強(qiáng)大的類型檢查功能,減少運(yùn)行時(shí)錯(cuò)誤,以下是一些優(yōu)雅地將TypeScript集成到React項(xiàng)目中的最佳實(shí)踐和技巧,需要的朋友可以參考下2024-06-06
react實(shí)現(xiàn)瀏覽器自動(dòng)刷新的示例代碼
這篇文章主要介紹了react實(shí)現(xiàn)瀏覽器自動(dòng)刷新的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
React項(xiàng)目中hook實(shí)現(xiàn)展示對(duì)話框功能
Modal(模態(tài)框)是 web 開發(fā)中十分常見的組件,即從頁(yè)面中彈出的對(duì)話框,下面這篇文章主要給大家介紹了關(guān)于React項(xiàng)目中hook實(shí)現(xiàn)展示對(duì)話框功能的相關(guān)資料,需要的朋友可以參考下2022-05-05
react-router實(shí)現(xiàn)跳轉(zhuǎn)傳值的方法示例
這篇文章主要給大家介紹了關(guān)于react-router實(shí)現(xiàn)跳轉(zhuǎn)傳值的相關(guān)資料,文中給出了詳細(xì)的示例代碼,對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。2017-05-05
如何不使用eject修改create-react-app的配置
許多剛開始接觸create-react-app框架的同學(xué),不免都會(huì)有個(gè)疑問(wèn):如何在不執(zhí)行eject操作的同時(shí),修改create-react-app的配置。2021-04-04
淺談對(duì)于react-thunk中間件的簡(jiǎn)單理解
這篇文章主要介紹了淺談對(duì)于react-thunk中間件的簡(jiǎn)單理解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05

