1

我正在尝试根据父 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;
4

1 回答 1

1

我建议先构建一棵树,然后遍历分支并构建一个映射来映射排序结果。

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>');

于 2016-02-29T10:41:14.903 回答