How to load grid data with json data on ajax sucess

非 Y 不嫁゛ 提交于 2019-12-02 10:19:07

I suggest you this solution. Just define the empty store with fields definitions, then load data using Ext.JSON.decode() and store.loadData().

Working example:

File grid_json_load.json:

{
    "data": [
    {
        "dataIndex1": "Value1",
        "dataIndex2": "Value2"
    },
    {
        "dataIndex1": "Value1",
        "dataIndex2": "Value2"
    }
    ],
    "success": true
}

File grid_json_load.html:

<html>
    <head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <link rel="stylesheet" type="text/css" href="../ext/resources/css/ext-all.css"> 
    <script type="text/javascript" src="../ext/ext-all-debug.js"></script>
    <script type="text/javascript">

Ext.define('Test.TestWindow', {
    extend: 'Ext.window.Window',

    closeAction: 'destroy',
    border: false,
    width: 560,
    height: 500,
    modal: true,
    closable: true,
    resizable: false,
    layout: 'fit',

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

        me.store = Ext.create('Ext.data.ArrayStore', {
            data: [],
            fields: [
                {name: "dataIndex1", type: "string"},
                {name: "dataIndex2", type: "string"}
            ]
        });

        me.grid = Ext.create('Ext.grid.Panel', {
            autoScroll: true,
            stripeRows: true,
            width: 420,
            height: 200,
            store: me.store,
            columnLines: false,
            columns : [
                {header : 'Data Index 1', width: 100, dataIndex : 'dataIndex1'},
                {header : 'Data Index 2', width: 100, dataIndex : 'dataIndex2'}
            ]
        });
        me.add(me.grid);
    }

}); 


Ext.onReady(function(){

    var win = Ext.create('Test.TestWindow', {
    });


    Ext.Ajax.request({
        url: 'grid_json_load.json',
        method: 'POST',
        timeout: 1200000,
        params: {
        },

        success: function (response, opts) {
            var win = new Test.TestWindow({

            });

            var r = Ext.JSON.decode(response.responseText);
            if (r.success == true) { 
                win.show();
                win.store.loadData(r.data);     
            } else {
                Ext.Msg.alert('Attention', 'Error');        
            };
        }
    });

});
    </script>   
    <title>Test</title>
    </head>
    <body>
    </body>
</html>

Notes:

Tested with ExtJS 4.2 and ExtJS 6.6.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!