linq js

时间:2016-01-29 14:02:06

标签: javascript group-by jstree linq.js

我有这个json格式

var personArray = 
[
    {name:"person1",code:"101011",mainDept:"mainD 1",dept:"dept1",SubDept:"Sub01"},
    {name:"person2",code:"201012",mainDept:"mainD 1",dept:"dept1",SubDept:"Sub11"},
    {name:"person3",code:"301013",mainDept:"mainD 1",dept:"dept2",SubDept:"Sub12"},
    {name:"person4",code:"401014",mainDept:"mainD 1",dept:"dept2",SubDept:"Sub12"},
    {name:"person5",code:"501015",mainDept:"mainD 1",dept:"dept2",SubDept:"Sub13"},
    {name:"person6",code:"601116",mainDept:"mainD 1",dept:"dept3",SubDept:"Sub21"},
    {name:"person7",code:"701117",mainDept:"mainD 1",dept:"dept3",SubDept:"Sub21"},
    {name:"person8",code:"801118",mainDept:"mainD 1",dept:"dept4",SubDept:"Sub22"},
    {name:"person9",code:"901119",mainDept:"mainD 2",dept:"dept12",SubDept:"Sub23"},
    {name:"person10",code:"101111",mainDept:"mainD 2",dept:"dept12",SubDept:"Sub24"},
    {name:"person12",code:"121012",mainDept:"mainD 2",dept:"dept13",SubDept:"Sub25"},
    {name:"person13",code:"131013",mainDept:"mainD 2",dept:"dept131",SubDept:"Sub26"},
    {name:"person14",code:"141014",mainDept:"mainD 3",dept:"dept132",SubDept:"Sub27"},
    {name:"person15",code:"151015",mainDept:"mainD 3",dept:"dept132",SubDept:"Sub27"},
    {name:"person16",code:"161116",mainDept:"mainD 4",dept:"dept141",SubDept:"Sub1"},
    {name:"person17",code:"171117",mainDept:"mainD 4",dept:"dept141",SubDept:"Sub1"},
    {name:"person18",code:"181118",mainDept:"mainD 4",dept:"dept141",SubDept:"Sub1"},
    {name:"person21",code:"211012",mainDept:"mainD 4",dept:"dept141",SubDept:"Sub1"},
    {name:"person22",code:"221013",mainDept:"mainD 4",dept:"dept141",SubDept:"Sub001"},
    {name:"person23",code:"231014",mainDept:"mainD 4",dept:"dept151",SubDept:"Sub002"},
    {name:"person24",code:"241015",mainDept:"mainD 5",dept:"dept161",SubDept:"Sub003"},
    {name:"person25",code:"251116",mainDept:"mainD 5",dept:"dept161",SubDept:"Sub003"},
    {name:"person26",code:"261117",mainDept:"mainD 5",dept:"dept161",SubDept:"Sub003"},
    {name:"person27",code:"271118",mainDept:"mainD 5",dept:"dept161",SubDept:"Sub003"},
    {name:"person28",code:"281119",mainDept:"mainD 5",dept:"dept161",SubDept:"Sub003"},
    {name:"person29",code:"291119",mainDept:"mainD 5",dept:"dept161",SubDept:"Sub003"}];

我想为jsTree(https://www.jstree.com/docs/json/)构建数据 多级分组将是这样的,即mainDept - > dept - > SubDept - >人

我试过这个以获得一个级别的分组,但是无法弄清楚如何进行多级分组。

var linq = Enumerable.From(personArray);
var grp = linq.GroupBy("$.mainDept","{text:$.dept}","{name:$,children:$$.ToArray()}").ToArray()

2 个答案:

答案 0 :(得分:1)

首先,您必须将personArray解析为jsTree可接受的json,然后将其提供给jsTree初始化。我无法帮助你使用linq,但使用普通的javascript,它可以像在这个演示中一样工作 - Fiddle

答案 1 :(得分:1)

对于任意深度嵌套的分组来说,确实没有很好的方法,特别是如果你需要在每个级别做一些不同的事情。进行某种递归使这很简单。幸运的是,Linq.js有一个Let()函数来允许这个。通过一些特制的功能,可以很好地完成。

function grouper(propertyName, selector) {
  return function (e) {
    return e.GroupBy("$." + propertyName, null, function (k, g) {
      return {
        text: k,
        children: g.Let(selector).ToArray()
      };
    });
  };
}
var items = Enumerable.From(personArray)
  .Let(grouper('mainDept', function (g1) {
    return g1.Let(grouper('dept', function (g2) {
      return g2.Let(grouper('SubDept', function (g3) {
        return g3.Select("$.name").ToArray();
      }));
    }));
  }))
  .ToArray();

fiddle

对于不同的方法,您可以使用jstree的替代形式来提出父/子关系。您不再需要嵌套任何东西,只需提供一个配置节点的平面列表。

var items = Enumerable.From(personArray)
  .Let(function (e) {
    var roots = { '#': {}, mainDept: {}, dept: {}, SubDept: {} };
    e.ForEach(function (p) {
      roots['#'][p.mainDept] = '#';
      roots['mainDept'][p.dept] = p.mainDept;
      roots['dept'][p.SubDept] = p.dept;
      roots['SubDept'][p.name] = p.SubDept;
    });
    function makeNode(root) {
      return Enumerable.From(roots[root]).Select("{ parent: $.Value, id: $.Key, text: $.Key }");
    }
    return makeNode('#').Concat(makeNode('mainDept')).Concat(makeNode('dept')).Concat(makeNode('SubDept'));
  })
  .ToArray();

fiddle