【问题标题】:Get JSON Data from ExtJS TreePanel从 ExtJS TreePanel 获取 JSON 数据
【发布时间】:2010-12-08 19:37:50
【问题描述】:

假设我有一个 Ext.tree.TreePanel 对象,它具有从外部文件加载的数据,例如:

var tree = new Ext.tree.TreePanel({
    ...
    loader: new Ext.tree.TreeLoader({
        dataUrl:'./some_file.json'
    }),
    ...
});

此文件是定义树的对象数组。

假设用户向树中添加新节点并移动一些节点。是否可以从树中获取 JSON 数据,以便下次用户加载树时使用?

编辑(代码解决方案)

这是基于 Juan 回复中的想法的解决方案。我把这个贴出来,以防将来有人发现这个线程并正在寻找一些代码。

function getNodeList(bfsQueue) {
    var node = bfsQueue.pop();
    var nodeQueue = [];

    for (var ii = 0; ii < node.childNodes.length; ii++) {
        bfsQueue.push( node.childNodes[ii] );
        nodeQueue.push( node.childNodes[ii] );
    }
    if (bfsQueue.length === 0) {
        return nodeQueue;
    } else {
        return nodeQueue.concat( getNodeList(bfsQueue) );
    }
}

var startQueue = [];
var nodeList = [];

startQueue.push( tree.getRootNode() );
nodeList.push( tree.getRootNode() );
nodeList = nodeList.concat(getNodeList( startQueue ));
console.dir(nodeList);

for ( var nn = nodeList.length-1; nn >= 0; nn-- ) {

    var params = [];
    for (var pp in nodeList[nn].attributes) {
        if (pp === "children" || pp === "loader") {continue;}
        params.push('"' + pp + '":' + JSON.stringify(nodeList[nn].attributes[pp]) + '');
    }

    if ( nodeList[nn].childNodes.length > 0) {
        var childList = [];

        for (var ii = 0; ii < nodeList[nn].childNodes.length; ii++) {
            childList.push( nodeList[nn].childNodes[ii].json );
        }

        params.push('"children": [' + childList.join(',') + ']');
    }

    nodeList[nn].json = "{" + params.join(",") + "}";
}

console.log(nodeList[0].json); // root node

【问题讨论】:

    标签: javascript json extjs tree treepanel


    【解决方案1】:

    首先,你真正想要的是attributes属性,它是用来创建节点的JSON。大多数相关属性都会更新,但 childNodes 不会。所以你必须写一些东西把它放回去。

    通过使用childNodes遍历树,可以得到所有的节点。您需要将它们重新组合成一个 json。

    Ext.data.Node.prototype.getJson = function () {
          // Should deep copy so we don't affect the tree
          var json = this.attributes;
    
          json.children = [];
          for (var i=0; i < node.childNodes.length; i++) {
              json.children.push( node.childNodes[i].getJson() )
          }
          return json;
    }
    
    tree.getRootNode().getJson();
    

    这个例子并不完美,但应该足以让您入门。

    更新

    在 Ext-JS 4.0 中,节点现在被修饰为 Records。因此,所有额外的属性都应该通过记录/模型接口记录并使用getset 方法检索集

    【讨论】:

    • 我不知道 attributes 属性。那和 childNodes 数组正是我所需要的。谢谢你的信息!
    • @Stevanicus 感谢 Ext-JS 4.0 更新。请注意,您应该更喜欢设置模型定义和 get/set 接口,而不是直接访问 .data 属性,这样您就不会使用未记录的属性
    【解决方案2】:

    在最新的 ExtJS 版本中,树节点的 NodeInterface 具有执行此操作的序列化函数。也许这与您有关。

    【讨论】:

      【解决方案3】:
      getTreeObjectJSONData: function (objectPanel) {
          var objectStore = objectPanel.getStore(),
              dataCollection = [];
          if (objectStore.data.items !== undefined) {
              $.each(objectStore.data.items, function (index, objectData) {
                  if (!objectData.data.leaf) {
                      dataCollection['groups'].push({
                          display_name: objectData.data.name,
                          group: objectData.data.group,
                          crudState: objectData.data.crudState,
                          unique_id: objectData.data.unique_id
                      });
                  } else {
                      dataCollection['fields'].push({
                          display_name: objectData.data.name,
                          group: objectData.data.group,
                          type: objectData.data.type != undefined ? objectData.data.type : 'null',
                          crudState: objectData.data.crudState,
                          unique_id: objectData.data.unique_id
                      });
                  }
              })
      
          }
          return Ext.util.JSON.encode(dataCollection);
      }
      

      可能会有帮助

      【讨论】:

        【解决方案4】:
        var root = TreePanel.getRootNode();
        var res = root.serialize();
        console.log(res)
        

        【讨论】:

        • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。您可以在帮助中心找到更多关于如何写好答案的信息:stackoverflow.com/help/how-to-answer。祝你好运?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-06-09
        • 2013-07-07
        • 1970-01-01
        • 2015-11-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多