Dojo grid nested json

主宰稳场 提交于 2019-11-28 13:04:27

What kind of store are you using? Have a look at the dojo.data.ItemFileReadStore documentation, there is an example with a situation like yours: http://dojotoolkit.org/reference-guide/dojo/data/ItemFileReadStore.html#item-structure

This would help you fetching all the items with a single call to the method "fetch". If for some reasons it doesn't work due to the different json structure, you can continue using ItemFileReadStore , and create a function that loops over all the objects in your json and uses the loadItem method for adding items one by one in this way (it's not beautiful but it works):

var myData = {"items" : []};
var myStore = new dojo.data.ItemFileWriteStore({data: myData});
var myLayout = [{
    field: 'name',
    name: 'Name',
    width: '200px'
},
{
field: 'universityName',
name: 'University Name',
width: '100px'
},
{
field: 'universityAddress',
name: 'University Address',
width: '60px'
}];
var myGrid;

dojo.addOnLoad(function(){
    myGrid = new dojox.grid.DataGrid({
        store: myStore,
        structure: myLayout
    }, document.createElement('div'));
    dojo.byId("myGridContainer").appendChild(myGrid.domNode); 
    myGrid.startup();

    dojo.xhrGet({
        url: myURL,
        handleAs: "json",
        headers: {
            "Content-Type": "application/json; charset=uft-8", 
            "Accept" : "application/json"
        },
        load: function(responseObject, ioArgs) {
            myList = responseObject;
            dojo.forEach(myList.items, function(element) {     
                myStore.newItem({"name": element.name, 
                    "universityName": element.university.name,
                    "universityAddress": element.university.address});
            });
        })
    });
}

se a formatter:

    var nameFormatter = function(value, rowIdx){
        return value.name;
    };
    var addressFormatter = function(value, rowIdx){
        return value.address;
    };

    var layoutUsers = [
       [{
               field: "name",
               name: "Name",
               width: 10
           },
           {
               field: "university",
               name: "University Name",
               width: 10,
               formatter: nameFormatter 
           },
           {
               field: "university",
               name: "University Address",
               width: 'auto',
               formatter: addressFormatter 
           }]];
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!