gpt4 book ai didi

javascript - 如何根据父id和级别[ag-grid]对行类别数据进行排序

转载 作者:行者123 更新时间:2023-11-27 23:17:38 26 4
gpt4 key购买 nike

我正在尝试根据父 ID 对层次结构类别对象进行排序,然后根据显示它及其父级的级别进行排序。

这是我的对象看起来像

[{category_id: "168"
category_level: "1"
category_name: "Home"
parent_id: "1"},

{category_id: "166"
category_level: "1"
category_name: "Living"
parent_id: "1"},

{category_id: "171"
category_level: "2"
category_name: "Chairs"
parent_id: "168"},

{category_id: "172"
category_level: "3"
category_name: "Single Sofa"
parent_id: "170"},

{category_id: "169"
category_level: "1"
category_name: "Patio"
parent_id: "1"},

{category_id: "170"
category_level: "2"
category_name: "Sofa"
parent_id: "166"},

]

** JSON 预期输出**

[{category_id: "168"
category_level: "1"
category_name: "Home"
parent_id: "1"},

{category_id: "169"
category_level: "1"
category_name: "Patio"
parent_id: "1"},

{category_id: "166"
category_level: "1"
category_name: "Living"
parent_id: "1"},


{category_id: "170"
category_level: "2"
category_name: "Sofa"
parent_id: "166"},

{category_id: "172"
category_level: "3"
category_name: "Single Sofa"
parent_id: "170"},

{category_id: "171"
category_level: "2"
category_name: "Chairs"
parent_id: "168"}]

输出应该看起来像

Home
Patio
Living
Living >> Sofa
Living >> Sofa >> Single Sofa
Living >> Chairs

我在这里使用 ag-grid 并尝试通过 lodash.js 解决它这是我用来在列定义中渲染单元格的代码:

var columnDefs = [
{headerName: "", field: "icon", width:52},
{headerName: "Categories",field:"category_name", width:1000, cellRenderer:function(params){

function getParentIndex(pid)
{
var index = $scope.categories.map(function(el){
return el.category_id;
}).indexOf(pid);
return index
}

var val="", parentIndex = getParentIndex(params.data.parent_id);

if(parentIndex > -1)
{

val = $scope.categories[parentIndex].category_name + " >> " +params.data.category_name;
var grandParentIndex = getParentIndex($scope.categories[parentIndex].parent_id);
if(grandParentIndex > -1)
{
val = $scope.categories[grandParentIndex].category_name + " >> " + val;
}
}
else {
val = params.data.category_name;
}
return val;
}},

{headerName: "", field: "options", width:87, suppressMenu: true, onCellClicked(params){
$scope.modifyOptions(params.data);
}, template:"<a class='btn' href='javascript:void(0)'>options</a>"
}
];

上面的代码很好地连接了字符串,但仍然需要对其进行排序,如输出所示。这是我的 lodash.js 代码,很短,但仅限于单个类别。

var parentId = 1;
var sortedList = [];
var byParentsIdsList = _.groupBy(scope.categories, "parent_id"); // Create a new array with objects indexed by parentId

while (byParentsIdsList[parentId]) {
sortedList.push(byParentsIdsList[parentId][0]);
parentId = byParentsIdsList[parentId][0].category_id;
}


scope.categories = sortedList;

最佳答案

我建议先构建一棵树,然后迭代分支并为 mapping 构建 map 排序结果。

var data = [{ category_id: "168", category_level: "1", category_name: "Home", parent_id: "1" }, { category_id: "166", category_level: "1", category_name: "Living", parent_id: "1" }, { category_id: "171", category_level: "2", category_name: "Chairs", parent_id: "168" }, { category_id: "172", category_level: "3", category_name: "Single Sofa", parent_id: "170" }, { category_id: "169", category_level: "1", category_name: "Patio", parent_id: "1" }, { category_id: "170", category_level: "2", category_name: "Sofa", parent_id: "166" }, ],
sorted,
tree = {},
map = [];

data.forEach(function (a, i) {
tree[a.category_id] = tree[a.category_id] || {};
tree[a.category_id].category_id = a.category_id;
tree[a.category_id].index = i;
tree[a.parent_id] = tree[a.parent_id] || {};
tree[a.parent_id].children = tree[a.parent_id].children || [];
tree[a.parent_id].children.push(tree[a.category_id]);
});

function getChildren(o) {
o.children.forEach(function (a) {
map.push(a.index);
a.children && getChildren(a);
});
}
getChildren(tree[1]); // assuming it starts with parent_id 1

sorted = map.map(function (a) {
return data[a];
});

document.write('<pre>' + JSON.stringify(sorted, 0, 4) + '</pre>');

关于javascript - 如何根据父id和级别[ag-grid]对行类别数据进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35696885/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com