如何将分层Json映射到ItemFileWriteStore?

时间:2012-04-30 18:42:58

标签: dojo dojox.grid dojox.grid.datagrid

我有包含子元素的Json数据。我需要将商店绑定到可编辑的网格,并将编辑内容填充到商店。 数据树确实填充到ItemFileWriteStore中。 datagrid仅显示父数据,而不显示任何子数据。

SAMPLE.TXT

    {
    "items": [
        {
            "profileId": "1",
            "profileName": "ABC",
            "profileType": "EmailProfile",
            "profilePreferences": [
                {
                    "profilePreferenceId": "1",
                    "displayText": "Bob",
                    "address": "primary@some.com"
                },
                {
                    "profilePreferenceId": "2",
                    "displayText": "Sally",
                    "address": "secondary@some.com"
                },
                {
                    "profilePreferenceId": "3",
                    "displayText": "Joe",
                    "address": "alternate@some.com"
                }
            ]
        }
    ]
}

的javascript

var sampleLayout = [
  [
  { field: 'profileName', name: 'profileName', width: '100px' },
  { field: 'profilePreferences.displayText', name: 'displayText', width: '100px' },
  { field: 'profilePreferences.address', name: 'address', width: '100px' }      
  ]];


function populateGrid() {
    var url = "sample.txt"; //Will be replaced with endpoint URL

    dojo.xhrGet({
        handleAs: 'json',
        url: url,
        error: function (e) {
            alert("Error: " + e.message);
        },
        load: showJsonData
    });
}


function showJsonData(response, ioArgs) {
    var profileStore = new dojo.data.ItemFileWriteStore({
        data: {
            items: response.items
        }
    });

    var sampleGrid = dijit.byId("sampleGrid");
    sampleGrid.store = profileStore;
    sampleGrid.startup();
}

1 个答案:

答案 0 :(得分:1)

你需要使用 dojox.grid.TreeGrid 或'假冒'JSON来为每个偶数行显示一个空白的profileName。下面是两个示例,一个是TreeGrid,另一个是DataGrid - 虽然没有在工作环境中测试过。

鉴于 Hierachial JSON

{
  identifier: 'id' // a good custom to make an id pr item, note spaces and odd chars are invalid

      items: [{
         id: '1',
         profileName: 'Admin',
         profilePreferences: [
           { id: '1_1', displayText: 'John Doe', address: 'Big Apple' }
           { id: '1_2', displayText: 'Jane Doe', address: 'Hollywood' }
         ]

      }, {
         id: '2',
         profileName: 'Visitor',
         profilePreferences: [
           { id: '2_1', displayText: 'Foo', address: 'Texas' }
           { id: '2_2', displayText: 'Bar', address: 'Indiana' }
         ]

      }]
    }

TreeGrid结构

{
    cells: [
      [
        { field: "profileName", name: "profileName", width: "100px" },
        { field: "profilePreferences",
          children: [
            { field: "displayText" name: "displayText", width: "100px" },
            { field: "address" name: "address", width: "100px" }
          ]
      ]
    ]
  }

参考:dojo docs

鉴于扁平'假孩子'JSON

{
  identifier: 'id' // a good custom to make an id pr item, note spaces and odd chars are invalid

      items: [{
         id: '1',
         profileName: 'Admin', preferenceText: '', preferenceAddr: ''
     }, {
        id: '2', 
        profileName: '',      preferenceText: 'John', preferenceAddr: 'NY'
     }, {
         id: '3',
         profileName: 'Visitor', preferenceText: '', preferenceAddr: ''
     }, {

         id: '4',         // Not with '.' dot seperator like so
         profileName: '',    preference.Text: 'Jane Doe', preference.Addr: 'Hollywood'
     } ]

DataGrid结构

[[
  {'name': 'Profilename', 'field': 'profileName', 'width': '100px'},
  {'name': 'User name', 'field': 'preferenceText', 'width': '100px'},
  {'name': 'Address', 'field': 'preferenceAddr', 'width': '200px'}
]]

参考dojo docs