2

分机 JS 4.1

要从“Ext.data.Store”获取值数据,只需使用

var data1 = store.data.items[0].data.fieldname;

或者

var data1 = store.getAt(0).data.fieldname;

但是,在“Ext.data.TreeStore”中,这种方法是行不通的!

有什么建议么?

4

2 回答 2

9

这是对 Extjs 的常见误解。Ext.data.TreeStore 和 Ext.data.Store 实际上不是很相似,也没有继承自另一个。

商店以模型数组的格式保存数据:

[
    {
        attribute1,
        attribute2,
        ...
    },
    ...
]

而树存储以节点树结构的格式保存数据,如下所示:

{
    attribute1,
    attribute2,
    ...,
    children: [
        {
            attribute1,
            attribute2,
            ...,
            children: [
                ...
            ]
        }
    ]
}

由于常规 store 和 treestore 底层的这些完全不同的结构,它们上的功能是完全不同的。我猜在上面的示例中,正确使用 store 和 node 函数将是:

var data1 = treestore.getRootNode().getChildAt(0).get(fieldname);

getRootNode()获取树的根节点,该根节点由具有检索根节点的第一个子节点的NodeInterface方法的类表示。getChildAt(index)然后我使用该get(dataIndex)函数从节点中获取您想要的属性。

于 2013-04-16T21:24:20.750 回答
0

This is in fact a much needed functionality.

Consider the situation when you have a tree grid with a check column, you're listening to a "checkchange" event of that column and want to find a record that was just changed.

Unfortunately, all ExtJS provides you with is a record index. Fortunately though, no one is stopping us from defining our own getAt method in our own store that does exactly what we want:

{
  store: 'tree',
  getAt: function (index) {
    var current = 0;
    return (function find(nodes) {
      var i, len = nodes.length;
      for (i = 0; i < len; i++) {
        if (current === index) {
          return nodes[i];
        }
        current++;
        var found = find(nodes[i].childNodes);
        if (found) {
          return found;
        }
      }
    }(this.tree.root.childNodes));
  }
}
于 2013-12-03T03:56:45.240 回答