【问题标题】:Populate Dojo's datagrid with JsonRest and custom json arrays使用 JsonRest 和自定义 json 数组填充 Dojo 的数据网格
【发布时间】:2013-04-14 10:03:56
【问题描述】:

我有一个网格,我正在创建一个格式如下的 JSON 数据源:

[{"user":{"username":"foo","url":"bar"}},
   [{"product":{"name":"banana","price":"85"}},
    {"product":{"name":"peach","price":"66"}},
    {"product":{"name":"strawberry","price":"78"}}
   ]
]

但我不知道如何告诉数据网格使用产品的内容来填充数据网格。这是我的数据网格代码:

        <script>
        require(["dojo/store/JsonRest"], function (JsonRest) {
            myStore = new JsonRest({ target: 'myurl', handleAs: 'json'
            });
        });

        require(["dojox/grid/DataGrid", "dojo/data/ObjectStore", "dojo/domReady!"
        ], function (DataGrid, ObjectStore) {
            grid = new DataGrid({
                store: dataStore = new ObjectStore({ objectStore: myStore }),
                structure: [
                    { name: "Procuct", field: "name", width: "200px" }
                ]
            }, "grid3");

            grid.startup();

        });
    </script>
    <div id="grid3"></div>

我没有收到任何错误,但我看不到网格已填充。

THIS类似的问题,只是数据结构有点不同。

【问题讨论】:

    标签: json dojo dojox.grid.datagrid


    【解决方案1】:

    我认为这与您的 json 结构有关。 jsonArray 的第一部分是一个对象,第二部分是一个数组:

    [object,ArrayOfProducts]

    如果您将数据隐藏在数组中的数组中,然后在属性产品中,DataGrid 应该如何找到必要的数据。 尝试通过 json 传递一些简单的东西,例如:

     [{"name":"banana","price":"85"},
      {"name":"peach","price":"66"},
      {"name":"strawberry","price":"78"}]
    

    【讨论】:

    • 我无法更改 json 结构。是否有可能以某种方式改变结构?
    • 您能否使用提供我发布的数据的虚拟 url 来测试我的建议,以确保这是解决方案?
    • 我试过用 sinon 来伪造 json 和你的响应建议。但我看不到网格。
    • 我在 jsfiddle 中粘贴了没有 sinon 的代码,一切正常,预计数据未显示:jsfiddle.net/mWuQm
    • 你使用的是哪个版本的道场(应该很久以前就问过了)?
    【解决方案2】:

    您是否尝试过 grid.renderArray(dataStore) 用内容填充网格?

    【讨论】:

      【解决方案3】:

      一种选择是在调用 dataStore.query() 之前将新属性附加到 json 对象。这可以通过dojo/aspect 完成。其他示例见article

      aspect.before(dataStore, "query", function(items) {
          items.forEach(function(item) { 
              //Do something here. I'll combine two properties.
              item.newProperty = item.propertyValueA + "-" item.propertyValueB;
                  return item;
              });
          return items;
      });
      

      调用dataStore.query()时,上面的函数也被调用。这会导致向 json 对象添加一个新属性。在上面的示例中,newProperty 是 propertyValueA 和 propertyValueB 的串联。

      这可能允许您根据需要操作 json。

      【讨论】:

        猜你喜欢
        • 2013-11-01
        • 2013-09-13
        • 2013-04-26
        • 1970-01-01
        • 1970-01-01
        • 2013-10-07
        • 1970-01-01
        • 1970-01-01
        • 2012-04-07
        相关资源
        最近更新 更多