【问题标题】:How to structure drag and drop data in firebase firestore?如何在 Firebase Firestore 中构建拖放数据?
【发布时间】: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


    【解决方案1】:

    我建议你看看这个answer,你的问题有多种选择。

    其中一个建议是创建一个具有所需顺序的数组,例如。初始顺序:[1,2,3,4,5],用户移动后:[2,1,4,3,5],并将其存储在单独集合中文档内的数组中,以避免重新更新每次移动中的所有元素。

    【讨论】:

    • 另外我建议你看看这个video,这里展示了如何用AngularFire实现它。
    • 感谢您的回答,我已经通过在 flexbox 容器中使用 css 顺序解决了问题。而且我只是将订单号保存在firestore中。无论如何,正如我所说,列对我来说并不重要。每次我移动卡片时,我都会更新订单值。有了这个,我可以让我的容器保持响应。
    • 很高兴听到它已解决!我建议您发布解决方案的答案,以防其他用户遇到同样的问题。
    • 当然,我会的。
    猜你喜欢
    • 2018-03-20
    • 2020-11-13
    • 2018-06-28
    • 2021-07-25
    • 2019-11-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    相关资源
    最近更新 更多