为了账号安全,请及时绑定邮箱和手机立即绑定

使用拼接函数时,如何在数组中移动javascript整个对象?(反应.js)

使用拼接函数时,如何在数组中移动javascript整个对象?(反应.js)

侃侃无极 2022-08-04 16:21:01
我正在 React 中使用拖放交互.js。我正在使用拼接函数对“行”数组进行重新排序,该函数在拖动完成后使用下面的 onDragEnd 函数调用:onDragEnd = (result) => {    const { destination, source, draggableId, type } = result;    if (!destination) {        return;    }    if (        destination.draggableId === source.droppableId &&        destination.index === source.index    ) {        return;    }    if (type === "row") {        const newRowOrder = Array.from(**this.state.currentRows**);        newRowOrder.splice(source.index, 1);        newRowOrder.splice(destination.index, 0, **draggableId**);        const newState = {            ...this.state,            currentRows: newRowOrder,        };        this.setState(newState);    }};在 onDragEnd 函数称为 currentRow 状态之前,如下所示:currentRow 状态在 onDragEnd 之前当调用该函数时,拼接函数起作用(我认为),但它不会移动数组中的整个对象,而只是移动ID。拼接函数中使用的拖动 Id 是需要移动的对象的 ID。在 onDragEnd 函数被调用后,currentRow 状态如下所示:currentRow 状态在 onDragEnd 之后是否可以将整个对象移动到新索引?
查看完整描述

1 回答

?
摇曳的蔷薇

TA贡献1793条经验 获得超6个赞

我认为您只是插入dragableId,您可以使用该函数找到整个对象并插入整个对象newRowOrder.splice(destination.index, 0, **draggableId**);Array.find


onDragEnd = (result) => {

    const { destination, source, draggableId, type } = result;

    if (!destination) {

        return;

    }

    if (

        destination.draggableId === source.droppableId &&

        destination.index === source.index

    ) {

        return;

    }


    if (type === "row") {

        const draggableRow = this.state.currentRows.find(row => row.id === draggableId);

        const newRowOrder = Array.from(this.state.currentRows);

        newRowOrder.splice(source.index, 1);

        newRowOrder.splice(destination.index, 0, draggableRow);


        const newState = {

            ...this.state,

            currentRows: newRowOrder,

        };


        this.setState(newState);

    }

}


查看完整回答
反对 回复 2022-08-04
  • 1 回答
  • 0 关注
  • 148 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号