Dojo 网格嵌套 json

发布于 2024-11-28 05:18:09 字数 1380 浏览 0 评论 0原文

我想要一个连接到服务器 url 的 dojo 网格,该服务器 url 输出以下 json : {标识符:“id” 项目:[ { id:“1”,姓名:“约翰”,大学:{姓名:“XXX”,地址:“YYY”} }]。

基本上我有一个嵌套的 json。我想将大学名称和大学地址表示为网格中的单独列。

我尝试使用 dojox.grid.DataGrid 对象并创建网格布局,但不知道如何引用嵌套元素,并且 University.name 和 University.address 似乎不起作用。 我使用的是dojo 1.6.1。

有人有任何指点吗?

这是我使用的js代码:

    dojo.require("dojox.grid.DataGrid");
    dojo.require("dojo.data.ItemFileReadStore");

    dojo.addOnLoad(function(){
    // our test data store for this example:
    var jsonStore = new dojo.data.ItemFileReadStore({
        url: '/MainDeployer/ajax/users/get.json'
    });

    var layoutUsers = [
       [{
               field: "name",
               name: "Name",
               width: 10
           },
           {
               field: "university.name",
               name: "University Name",
               width: 10
           },
           {
               field: "university.address",
               name: "University Address",
               width: 'auto'
           }]];

    // create a new grid:
    var grid = new dojox.grid.DataGrid({
        query: {},
        store: jsonStore,
        clientSort: true,
        rowSelector: '20px',
        structure: layoutUsers
    },
    document.createElement('div'));

    dojo.byId("usersTable").appendChild(grid.domNode);

    grid.startup();
});

谢谢, 克里斯蒂安

I'd like to have a dojo grid which connects to a server url which outputs the following json :
{identifier : "id"
items : [ { id: "1", name: "John", university : { name: "XXX", address: "YYY" } }].

Basically I have a nested json. I would like to represent the university name and University address as separate columns in the grid.

I tried using the dojox.grid.DataGrid object and creating a gird layout, but do not know how to refer to the nested elments and university.name and university.address don't seem to work.
I am using dojo 1.6.1.

Does anybody have any pointers?

This is the js code I use :

    dojo.require("dojox.grid.DataGrid");
    dojo.require("dojo.data.ItemFileReadStore");

    dojo.addOnLoad(function(){
    // our test data store for this example:
    var jsonStore = new dojo.data.ItemFileReadStore({
        url: '/MainDeployer/ajax/users/get.json'
    });

    var layoutUsers = [
       [{
               field: "name",
               name: "Name",
               width: 10
           },
           {
               field: "university.name",
               name: "University Name",
               width: 10
           },
           {
               field: "university.address",
               name: "University Address",
               width: 'auto'
           }]];

    // create a new grid:
    var grid = new dojox.grid.DataGrid({
        query: {},
        store: jsonStore,
        clientSort: true,
        rowSelector: '20px',
        structure: layoutUsers
    },
    document.createElement('div'));

    dojo.byId("usersTable").appendChild(grid.domNode);

    grid.startup();
});

Thanks,
Cristian

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨轻弹 2024-12-05 05:18:09

您使用什么类型的商店?看一下 dojo.data.ItemFileReadStore 文档,有一个与您类似的情况的示例:
http://dojotoolkit.org/reference-guide/dojo/data /ItemFileReadStore.html#item-struct

这将帮助您通过一次调用“fetch”方法来获取所有项目。如果由于某些原因由于 json 结构不同而无法工作,您可以继续使用 ItemFileReadStore ,并创建一个函数来循环 json 中的所有对象,并使用 loadItem 方法以这种方式逐一添加项目(它不漂亮,但有效):

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

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});
            });
        })
    });
}
苍景流年 2024-12-05 05:18:09

se 格式化程序:

    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 
           }]];

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 
           }]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文