JSON child object with dojo/data/ObjectStore

大城市里の小女人 提交于 2019-12-08 04:26:41

问题


Given the following JSON data:

[
{
    "pk": 2, 
    "model": "corkboard.announcement", 
    "fields": {
        "body": "Test announcement 2 body.", 
        "date": "2012-04-10T00:59:12Z", 
        "title": "Test Announcement 2"
    }
}, 
{
    "pk": 1, 
    "model": "corkboard.announcement", 
    "fields": {
        "body": "Test Announcement 1 body.", 
        "date": "2012-04-10T00:58:56Z", 
        "title": "Test Announcement 1"
    }
}
]

I'm creating a dojox/DataGrid but I can't seem to find a way to access "fields" children.

Here is the javascript:

<script>
        var announcementStore, dataStore, grid;
        require(["dojo/store/JsonRest", "dojo/store/Memory", "dojo/store/Cache", "dojox/grid/DataGrid", "dojo/data/ObjectStore", "dojo/query", "dojo/domReady!"], 
            function(JsonRest, Memory, Cache, DataGrid, ObjectStore, query){
            announcementStore = Cache(JsonRest({target:"/corkboard/announcements/"}), Memory());
            grid = new DataGrid({
                store: dataStore = ObjectStore({objectStore: announcementStore}),
                structure: [
                    {name:"Title", field:"title", width: "200px"},
                    {name:"Body", field:"body", width: "200px", editable: true}
                ]
            }, "target-node-id");
            grid.startup();
            query("#save").onclick(function(){
                dataStore.save();
            });
        });
</script>

I tried using fields.title and fields.body when defining the field, but that didn't work.

In this example, how would I access "fields" children?


回答1:


You need to use the formatter method in the structure of the grid like below.

{name:"Title",field:"_item",widht:"200px",formatter:function(item){return item.fields.title}}

Remember, you need to passs _item in the field, which will give you entire row in the formatter method and using dot notation, you can return the reuqired data



来源:https://stackoverflow.com/questions/10115518/json-child-object-with-dojo-data-objectstore

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!