5

今天尝试使用fluent-ui的Grouped DetailsList

我的期望:我需要声明一些组,比如说红色、蓝色、绿色,然后添加到每个项目,我想添加到列表中,一个特定的属性,将项目映射到组。例如:

groups: [
            { key: 'red', name: 'Color: "red"'},
            { key: 'green', name: 'Color: "green"'},
            { key: 'blue', name: 'Color: "blue"' },
          ],

items: [...,
        { key: 'red',anyProp1: "abc", anyProp2: "dfg",...},
       ...,
      ]

我发现我必须做的事情:我需要对包含我的项目的数组进行排序,所有属于红色组的项目都需要放在一个块中。例如:[红、红、红、蓝、蓝、绿、绿、绿]。现在我需要提供有关 startIndex 和 count 的信息,以将我的项目数组映射到组。

这就是组的定义:

 groups: [
        { key: 'groupred0', name: 'Color: "red"', startIndex: 0, count: 2, level: 0 },
        { key: 'groupgreen2', name: 'Color: "green"', startIndex: 2, count: 0, level: 0 },
        { key: 'groupblue2', name: 'Color: "blue"', startIndex: 2, count: 3, level: 0 },
      ],

我不明白他们为什么这样做(对我来说这样很不方便)。所以,虽然我更多地介于 JS 的初学者和中级之间。我认为执行此操作的人是专业人士。一定是有原因的。也许它与性能有关?我可以想象,当涉及到非常大的列表时,这种方式会表现得更好,但我不确定。

有人知道这方面的一些细节并可以解释吗?

4

1 回答 1

1

面临同样的问题,并在这里得到了线索。然后破坏我的解决方案。以下是根据分组列排序的给定项目列表生成组数组的函数:

function groupsGenerator(itemsList, fieldName) {
    // Array of group objects
    const groupObjArr =[]

    // Get the group names from the items list
    const groupNames = [...new Set(itemsList.map(item => item[fieldName]))]

    // Iterate through each group name to build proper group object
    groupNames.forEach(gn => {
        // Count group items
        const groupLength = itemsList.filter(item => item[fieldName] === gn).length
        
        // Find the first group index
        const groupIndex = itemsList.map(item => item[fieldName]).indexOf(gn)

        // Generate a group object
        groupObjArr.push({
            key: gn, name: gn, level: 0, count: groupLength, startIndex: groupIndex
        })
    })

    // The final groups array returned
    return groupObjArr
}
于 2021-01-21T18:58:14.363 回答