带有dojo / data / ObjectStore的JSON子对象 [英] JSON child object with dojo/data/ObjectStore

查看:217
本文介绍了带有dojo / data / ObjectStore的JSON子对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于以下JSON数据:

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

我正在创建一个dojox / DataGrid,但我似乎找不到一种访问字段孩子的方法。

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

这是javascript:

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>

我在定义字段时尝试使用fields.title和fields.body,但没有起作用

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

在这个例子中,我如何访问fields子句?

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

推荐答案

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

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

请记住,您需要在字段中传递 _item / code>,这将给你整个行格式化方法和使用点符号,你可以返回reuqired数据

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

这篇关于带有dojo / data / ObjectStore的JSON子对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆