【问题标题】:showing all fields in a Dojo Data Grid with dojo.store.JsonRest使用 dojo.store.JsonRest 显示 Dojo 数据网格中的所有字段
【发布时间】:2011-05-11 05:49:17
【问题描述】:

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

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

谢谢。

【问题讨论】:

    标签: django json dojo


    【解决方案1】:

    这实际上是一个问题,“我如何与 javascript 对象交互?”鉴于您问题中的 JSON,并假设您将其分配给变量 obj,您可以使用 obj[0]['fields']['mail_name'] 或使用点符号 obj[0].fields.mail_name 访问 mail_name。我没用过Dojo,但我敢打赌你只需要将fields.mail_name 设置为layoutGridContacts 中的field

    【讨论】:

    • 感谢您的提示。不幸的是,Dojo Store 似乎无法使用这些方法与 JSON 进行交互。
    【解决方案2】:

    我能够让服务器生成一个不包含嵌套对象的 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")  
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        您可以使用格式化程序方法来检索数据。对于您的示例,它将如下所示

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

        【讨论】:

          猜你喜欢
          • 2012-10-27
          • 2011-10-25
          • 1970-01-01
          • 2012-03-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-11-26
          相关资源
          最近更新 更多