【发布时间】:2021-04-25 05:47:41
【问题描述】:
我想为我的 react-firebase 应用添加实时拖放支持。现在的结构就像,我有“会话”集合,在它下面有一个“想法”集合,它有多个字段,我正在尝试添加对“想法”的拖放支持。
在我的情况下,列名并不重要,我肯定会一直有 5 列,目前我的拖放支持在不更新 firestore 的情况下运行良好。在这一点上,我不确定如何构造数据。在本地,我的数据结构如下:
[[{single idea}, {single idea}], [{single idea}], [], [], [], []]
您可以将数组视为列,其元素在该列内排序。我尝试在想法中添加列和订单字段,但我认为无法正确更新它。
也许我可以实现这样的数据结构:
[{col1: [{}, {},]}, {col2: [{}]}]
就像你在它下面有一个数组一样,有一个以列名作为键值的映射,而在它下面还有另一个包含想法映射的数组。同样,我不确定如何实现这样的结构,这是我的重新排序和移动功能:
重新排序:用于对列内的元素进行排序
const reorder = (list, startIndex, endIndex) => {
const result = Array.from(list);
const [removed] = result.splice(startIndex, 1);
result.splice(endIndex, 0, removed);
console.log(result);
return result;
};
移动:将元素移动到其他列
const move = (source, destination, droppableSource, droppableDestination) => {
const sourceClone = Array.from(source);
const destClone = Array.from(destination);
const [removed] = sourceClone.splice(droppableSource.index, 1);
destClone.splice(droppableDestination.index, 0, removed);
const result = {};
result[droppableSource.droppableId] = sourceClone;
result[droppableDestination.droppableId] = destClone;
return result;
};
【问题讨论】:
标签: reactjs firebase google-cloud-firestore drag-and-drop react-beautiful-dnd