与道场/数据/ ObjectStore的JSON子对象(JSON child object with

2019-09-16 11:40发布

鉴于以下JSON数据:

[
{
    "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"
    }
}
]

我创建一个DojoX中/ DataGrid中,但我似乎无法找到一种方法来访问“域”的孩子。

这里是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>

我试图定义字段时使用fields.title和fields.body,但没有奏效。

在这个例子中,我将如何访问“域”的孩子?

Answer 1:

您需要使用的formatter方法类似下面的网格结构。

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

请记住,你需要passs _itemfield ,它会给你整行格式化的方法和使用点符号,你可以返回reuqired数据



文章来源: JSON child object with dojo/data/ObjectStore
标签: json dojo