构造/嵌套json数据以填充树网格

时间:2013-05-28 12:14:39

标签: json extjs extjs4.1

我一直在努力用json数据填充树网格。我正在从数据库中读取数据,然后将其加载到树形网格中。

我从数据库获得的json数据格式如下:

{   "data": [{            
    "bike": "Yamaha",
    "color": "Black",
    "cost": 870000
},{
    "bike": "Honda",
    "color": "Red",
    "cost": 675000
},{
    "bike": "Honda",
    "color": "Blue",
    "cost": 690000
},{
    "bike": "Suzuki",
    "color": "White",
    "cost": 800000"
},{
    "bike": "Harley",
    "color": "Yellow",
    "cost": 980000
},{
    "bike": "Harley",
    "color": "Black",
    "cost": 880000
}]}

在我的树状面板中,第一列是树列。 我为树面板设置了以下属性

                displayField: 'bike',
                rootVisible: false,
                useArrows: true,

我可以将它加载到树形网格中,但是每个数据的内容都显示在树中的单独节点中。我知道这是因为json结构不适合树。

我想以特定格式转换或嵌套json数据,如下所示:

{"data": [{            
    "bike": "Yamaha",
    "data": [{
        "bike": "Yamaha",
        "color": "Black",
        "cost": 870000
    }]
},{
    "bike": "Honda",
    "data": [{
        "bike": "Honda",
        "color": "Red",
        "cost": 675000
    },
    {
        "bike": "Honda",
        "color": "Blue",
        "cost": 690000      
    }]
},{
    "bike": "Suzuki",
    "data": [{
        "bike": "Suzuki",
        "color": "White",
        "cost": 800000"
    }]
},{
    "bike": "Harley",
    "data": [{
        "bike": "Harley",
        "color": "Yellow",
        "cost": 980000
    }]
},{
    "bike": "Harley",
    "data": [{
        "bike": "Harley",
        "color": "Black",
        "cost": 880000
    }]
}]}

这样我就可以获得以下输出..

enter image description here

我不确定如何转换json数据。我无法在网络上找到合适的解决方案。请帮忙

这就是我的商店的样子:

var MyStore = Ext.create('Ext.data.TreeStore', {
        autoLoad: true,
        autoSync: true,
        storeId: 'MyStore',
        model: 'model',
        proxy: {
            type: 'ajax',
            api: {
                read: 'http://localhost/Files/data.json'
            },
            reader: {
                type: 'json',
                messageProperty: 'message',
                successProperty: 'success',
                root: 'data',
                totalProperty: 'total'
            }
        }

});

3 个答案:

答案 0 :(得分:1)

我自己解决了这个问题 我不得不修改我的SQL查询本身以嵌套格式获取Json数据以填充树网格:

//我得到了节点参数的值

$node = $_POST['node'];
if (empty($node)){
    $node = $_GET['node'];
}

//基于我查询数据的节点类型

if (strcmp(strtolower($node),"root")==0) {
    // If root node then query all bikes with remaining data as null for setting the parent nodes
    $query = "Select 'PARENT_' || b.id as ID, Null as color, Null as cost from Bikes b";    
} else {
    // Splitting the Id from string
    $node = explode("_", $node);
    $nodeType = $node[0];
    $bikeID = $node[1];

    if (strcmp(strtolower($nodeType),"parent")==0) {            
        // if node is a parent node, then load all child nodes based on bike ID
        $query = "select 'CHILD_' || b.id as ID, b.color as color, b.cost as cost from Bikes b where b.id = ".sprintf('%d',intval($bikeID));                    
    }
}
// execute the query
execute($query);

答案 1 :(得分:0)

Tree Store中的root配置是什么样的?默认情况下,嵌套数据的预期root属性是“children”,但可以更改(在您的情况下)为“data”

答案 2 :(得分:0)

数据格式正确,但您的语法错误:

{
    "bike": "Suzuki",
    "data": [{
        "bike": "Suzuki",
        "color": "White",
        "cost": 800000"
    }]
}

费用后有一个意外的"字符。修复后,数据将使用如下配置的树:

Ext.create('Ext.tree.Panel', {
    renderTo: Ext.getBody()
    ,height: 300
    ,rootVisible: false

    ,columns: [{
        xtype: 'treecolumn'
        ,dataIndex: 'bike'
        ,text: 'Bikes'
    },{
        dataIndex: 'color'
        ,text: 'Color'
        ,flex: 2
    },{
        dataIndex: 'cost'
        ,text: 'Cost'
    }]    

    ,store: {
        autoLoad: true,
        autoSync: true,
        fields: ['bike', 'color', 'cost'],
        proxy: {
            type: 'memory'
            ,reader: {
                type: 'json'
                ,root: 'data'
            }
            ,data: data // that's your data object
        }
    }
});

此外,如果您的服务器不支持过滤(例如,平面json文件),或者您想要避免大量请求(这可能是您尝试首先加载嵌套数据的原因),那么您将会必须将leaf: true添加到......井,叶子的所有节点。或者商店将为每个非离开节点发出服务器请求。