使用dojo.store.JsonRest显示Dojo数据网格中的所有字段

时间:2011-05-11 05:49:17

标签: django json dojo

我有一个Dojo数据网格,用于显示仅显示两列值的联系信息:“model”和“pk”。其他列是空白的,可能是因为来自服务器的JSON响应将其他名称/值对放在“fields”中:

[{"pk": 1, "model": "accounting.contacts", "fields": {"mail_name": "Andy", "city": "Grand Rapids", "zip": "49546", "country": "US", "state": "MI"}}]

让我的所有字段都显示在网格中的最佳方法是什么?

以下是Django中的相关视图:

def contacts(request):  
    json_serializer = serializers.get_serializer("json")()  
    json_contacts = json_serializer.serialize(Contacts.objects.all(),    ensure_ascii=False)  
    return HttpResponse(json_contacts, mimetype="application/json")  

这是我的Dojo页面:

<script src="http://ajax.googleapis.com/ajax/libs/dojo/1.6/dojo/dojo.xd.js"
    data-dojo-config="isDebug: true,parseOnLoad: true">
</script>
<script type="text/javascript">
    dojo.require("dojo.store.JsonRest");
    dojo.require("dojox.grid.DataGrid");
dojo.require("dojo.data.ObjectStore");

    dojo.ready(function(){
        objectStore = new dojo.store.JsonRest({target:"/contacts/"});
        //alert(objectStore);
        dataStore = new dojo.data.ObjectStore({objectStore: objectStore});

        //alert(dataStore);

        layoutGridContacts = [{
            field: 'mail_name',
            name: 'Name',
            width: '200px'
        },
        {
            field: 'model',
            name: 'DB Table',
            width: '100px'
                    ...

        }];

        gridContacts = new dojox.grid.DataGrid({
            query: {
                name: '*'
            },
            store: dataStore,
            clientSort: true,
            structure: layoutGridContacts
        }, dojo.byId("containerGridContacts"));

        gridContacts.startup();
    });
</script>
    <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.6/dijit/themes/claro/claro.css" />

    <style type="text/css">
        @import "http://ajax.googleapis.com/ajax/libs/dojo/1.6/dojox/grid/resources/Grid.css";
        @import "http://ajax.googleapis.com/ajax/libs/dojo/1.6/dojox/grid/resources/claroGrid.css";
        .dojoxGrid table {margin: 0; } html, body { width: 100%; height: 100%;
        margin: 0;}         
    </style>
</head>

<body class="claro">
    <div id="containerGridContacts" style="width: 100%, height: 100%;">
    </div>
</body>

感谢。

4 个答案:

答案 0 :(得分:0)

这真的是一个问题,“我如何与javascript对象进行交互?”鉴于您的问题中的JSON,并假设您已将其分配给变量obj,您可以使用mail_name或使用点表示法obj[0]['fields']['mail_name']访问obj[0].fields.mail_name。我没有使用过Dojo,但是我只是需要将fields.mail_name设置为field中的layoutGridContacts

答案 1 :(得分:0)

我能够让服务器生成不包含嵌套对象的JSON响应,因此Dojo Store能够使用它。为此,我将观点改为:

def contacts(request):  
    all_contacts = list(iter(Contacts.objects.values()))  
    json_contacts = simplejson.dumps(all_contacts)  
    return HttpResponse(json_contacts, mimetype="application/json")  

答案 2 :(得分:0)

使用“字段”。在您的字段标识符前面访问字段内的属性:

layoutGridContacts = [{
        field: 'fields.mail_name',
        name: 'Name',
        width: '200px'
    },
   ...

答案 3 :(得分:0)

您可以使用formatter方法检索数据。对于你的例子,它将类似于下面的

{name:"Name", 
 field: "fields", 
 width: "20%", 
 cellStyles:"background-color:#e3690b;",
 formatter: function(field){ 
             if(!field){return;}
       return field.mail_name;
 }
}