使用具有不同子字段的 JSON 加载 TreeStore [英] Loading TreeStore with JSON that has different children fields

查看:18
本文介绍了使用具有不同子字段的 JSON 加载 TreeStore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像下面这样的 JSON 数据.

I am having a JSON data like below.

{
    "divisions": [{
        "name": "division1",
        "id": "div1",
        "subdivisions": [{
            "name": "Sub1Div1",
            "id": "div1sub1",
            "schemes": [{
                "name": "Scheme1",
                "id": "scheme1"
            }, {
                "name": "Scheme2",
                "id": "scheme2"
            }]
        }, {
            "name": "Sub2Div1",
            "id": "div1sub2",
            "schemes": [{
                "name": "Scheme3",
                "id": "scheme3"
            }]
        }

        ]
    }]
}

我想将其读入 TreeStore,但不能将子字段(divisionssubdivisionsschemes)更改为相同(例如,children).

I want to read this into a TreeStore, but cannot change the subfields ( divisions, subdivisions, schemes ) to be the same (eg, children).

我怎样才能做到这一点?

How can achieve I this?

推荐答案

当嵌套 JSON 加载到 TreeStore 时,本质上通过 TreeStore.fillNode() 方法之间的递归调用加载子节点和 NodeInterface.appendChild().

When nested JSON is loaded into a TreeStore, essentially the children nodes are loaded through a recursive calls between TreeStore.fillNode() method and NodeInterface.appendChild().

每个节点的子字段的实际检索在此行的 TreeStore.onNodeAdded() 中完成:

The actual retrieval of each node's children field is done within TreeStore.onNodeAdded() on this line:

dataRoot = reader.getRoot(data);

阅读器的 getRoot() 是在阅读器的 buildExtractors() 方法中动态创建的,这是您需要覆盖以处理变化的嵌套 JSON 中的子字段.这是它的完成方式:

The getRoot() of the reader is dynamically created in the reader's buildExtractors() method, which is what you'll need to override in order to deal with varying children fields within nested JSON. Here is how it's done:

Ext.define('MyVariJsonReader', {
    extend: 'Ext.data.reader.Json',
    alias : 'reader.varijson',

    buildExtractors : function()
    {
        var me = this;    
        me.callParent(arguments);

        me.getRoot = function ( aObj ) {                
            // Special cases
            switch( aObj.name )
            {
                case 'Bill':   return aObj[ 'children' ];
                case 'Norman': return aObj[ 'sons' ];                    
            }

            // Default root is `people`
            return aObj[ 'people' ];
        };
    }
});

这将能够解释这样的 JSON:

This will be able to interpret such JSON:

{
   "people":[
      {
         "name":"Bill",
         "expanded":true,
         "children":[
            {
               "name":"Kate",
               "leaf":true
            },
            {
               "name":"John",
               "leaf":true
            }
         ]
      },
      {
         "name":"Norman",
         "expanded":true,
         "sons":[
            {
               "name":"Mike",
               "leaf":true
            },
            {
               "name":"Harry",
               "leaf":true
            }
         ]
      }
   ]
}

请参阅此 JsFiddle 以获取完整的代码.

See this JsFiddle for fully working code.

这篇关于使用具有不同子字段的 JSON 加载 TreeStore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆