0

我的数据最初存储在一个像这样结构的 xml 文件中(有一堆实体,每个实体中的行和每行中的单元格)

    <entity i=1>
        <row i =1>
           <cell i=1>
           <cell i=2>
        </row>

        <row i=2>
           <cell i=1>
           <cell i=2>
        </row>
    </entity>

我从 dojo 教程中读到的示例是这样的:

    require([

    'dojo/store/Memory',
    'gridx/Grid',
    'gridx/core/model/cache/Sync'
    ], function(Store, Grid, Cache){
  var store = new Store({
    data: [
        {id: 1, title: 'Hey There', artist: 'Bette Midler'},
        {id: 2, title: 'Love or Confusion', artist: 'Jimi Hendrix'},
        {id: 3, title: 'Sugar Street', artist: 'Andy Narell'}
    ]
});
......
});

我应该如何使用 XML 样式表创建 dojo 商店?我应该在我的 XML 样式表中使用嵌入式 javascript 吗?

4

1 回答 1

0

好吧,您可以使用该dojo/query模块来遍历您的 XML 文档。您首先要做的唯一一件事是解析您的 XML(如果还没有的话),例如:

require([ "dojo/query", "dojox/xml/parser", "dojo/dom-attr" ], function(query, xml, domAttr) {
    var content = xml.parse("<entity><row i=\"1\"><cell i=\"1\">Cell 1</cell><cell i=\"2\">Cell 2</cell></row><row i=\"2\"><cell i=\"1\">Cell 3</cell><cell i=\"2\">Cell 4</cell></row></entity>");
    var out = query("row", content).map(function(node) {
        var data = {};
        query("cell", node).forEach(function(cell) {
            data[domAttr.get(cell, "i")] = cell.textContent;
        });
        return data;
    });
    console.log(out);
});

这将产生一个包含两个对象的数组,其中属性的值i 是属性名称,<cell>元素的文本内容是值。

[{
  "1": "Cell 1",
  "2": "Cell 2"
}, {
  "1": "Cell 3",
  "2": "Cell 4"
}]

然后您可以在商店中使用它。

完整示例可以在 JSFiddle 上找到:http: //jsfiddle.net/bUjN7/

于 2014-06-23T06:05:16.733 回答