dE_*_*dE_ 6 javascript office-ui-fabric office-ui-fabric-react fluent-ui
今天我尝试使用Fluent-ui 的分组详细信息列表。
我的期望:我需要声明一些组,比如说红色、蓝色、绿色,然后添加到每个项目,我想添加到列表中,这是一个将项目映射到组的特定属性。例如:
groups: [
{ key: 'red', name: 'Color: "red"'},
{ key: 'green', name: 'Color: "green"'},
{ key: 'blue', name: 'Color: "blue"' },
],
items: [...,
{ key: 'red',anyProp1: "abc", anyProp2: "dfg",...},
...,
]
Run Code Online (Sandbox Code Playgroud)
我发现我必须做的事情:我需要对包含我的项目的数组进行排序,属于红色组的所有项目都需要位于一个块中。例如:[红,红,红,蓝,蓝,绿,绿,绿]。现在我需要提供有关 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 },
],
Run Code Online (Sandbox Code Playgroud)
我不明白他们为什么这样做(对我来说这样很不方便)。所以,虽然我在 JS 方面处于初学者和中级之间。我认为实施这个的人都是专业人士。一定是有原因的。也许这与性能有关?我可以想象,当涉及到非常大的列表时,这种方式表现得更好,但我不确定。
有谁知道这方面的一些细节并可以解释一下吗?
小智 3
面临同样的问题并在这里得到了线索。然后建立我的解决方案。以下是从按分组列排序的给定项目列表生成组数组的函数:
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
}
Run Code Online (Sandbox Code Playgroud)