如何使Dojo TreeGrid按两列分类?

时间:2018-08-01 05:57:54

标签: javascript json dojo treegrid dojox.grid

我有一个简单的dojo treeGrid,仅按第一列进行了分类。但是如何使它归类/可折叠呢?请注意,treeGrid的总计显示在每个类别中。另外,有没有办法将总计移到类别级别而不移到底部?

var layout = [ 
    { cells: [ 
       [ {field: "year", name: "Year"}, 
         {field: "childItems", 
           children: [ { field: "unid", name: "unid", hidden: true},
                       { field: "geography", name: "Geography"}, 
                       { field: "country", name: "Coungtry"}, 
                       { field: "status", name: "Status"}, 
                       { field: "credit", name: "Credit"}, 
                       { field: "debit", name: "Debit"}
                     ], 
                  aggregate: "sum" 
                  } 
                  ]] } ]

var jsonStore = new dojo.data.ItemFileWriteStore({ url: <...............>});


var grid = new dojox.grid.TreeGrid({ 
    structure: layout, 
    store: jsonStore, 
    query: {type: 'year'}, 
    queryOptions: {deep: true},
    rowSelector: true, 
    openAtLevels: [false],
    autoWidth: true,
    autoHeight: true
    }, 
    dojo.byId("treeGrid"));

grid.startup();

dojo.connect(window, "onresize", grid, "resize");

示例JSON存储:

{
  "identifier": "id",
  "label": "name",
  "items": [
    {
      "id": "2018",
      "type": "year",
      "year": "2018",
      "childItems": [
        {
          "id": "id0",
          "geography": "Asia Pacific",
          "country": "Australia",
          "programname": "Program 1",
          "totalPlanned": 0,
          "totalForecasted": 0
        },
        {
          .....
        }
      ]
    },
    {
          .....
    }
  ]
}

enter image description here

1 个答案:

答案 0 :(得分:6)

您可以在here上找到完全有效的示例:

现在,让我尝试解释一下:

数据

首先,要支持网格中的多个级别,必须使数据具有相同的格式。对于具有n个级别的树,您需要在数据本身中具有n-1个级别分组。 例如,下面的JSON对象具有2级分组(年份,地理位置),以支持3级分组的树(根,父级和子级)。

{
   "identifier":"id",
   "label":"name",
   "items":[
      {
         "id":"2018",
         "type":"year",
         "year":"2018",
         "geography":[
            {
               "id":"id1",
               "geography":"Asia Pacific",
               "childItems":[
                  {
                     "id":"ci1",
                     "country":"Australia",
                     "programname":"Program 1",
                     "credit":100,
                     "debit":50
                  }
               ]
            }
         ]
      }
   ]
}

布局

要渲染具有n级的树,您必须确保树的布局已正确配置为具有与数据相同的嵌套。为了支持上述JSON对象的数据结构,您需要将布局设置为:

[
   {
      cells:
      [
         [
            { field:"year", name:"Year" },
            {
              field:"geography",
              children:
              [
                  { field:"geography", name:"Geography" },
                  { 
                    field:"childItems",
                    children:[
                        { field:"unid", name:"unid", hidden:true },
                        { field:"country", name:"Country" },
                        { field:"programname", name:"Program" },
                        { field:"credit", name:"Credit" },
                        { field:"debit", name:"Debit" }
                     ],
                     aggregate:"sum",
                  },
              ]
            }
         ]
      ]
   }
]

您会看到,对于每个子级别,您都必须添加一个组(我想称呼它)字段并将该组中的第一个字段设置为您的实际组字段。

我希望这个例子能解决您的疑问。 PS:在jsfiddle版本中,我使用格式化程序只是为了隐藏字符串字段的聚合值。