How to load grid data with json data on ajax sucess

梦想的初衷 提交于 2019-12-20 06:28:13

问题


In my ajax sucess i am calling this funcction and there from response I am extracting the data which I need to load.

   success: function(response){
     StoreloadData(this,response);
    },

In StoreloadData function i am trying to load data in my grid like this but not getting data.

StoreloadData(me,response){
        var myGrid = Ext.getCmp('#myGrid');
        var myGridStore = myGrid.getStore();
        var gridData = response.myData.data;
        var total = gridData[0].recordsCount;
        myGridStore.load(gridData); // Not loading
        myGridStore.loadData(gridData); // Not loading
        myGrid.refresh(); // Error. 
    }

Here I have myJSon data in this line var gridData = response.myData.data; this is a simple json object like this.

[{dataIndex1: "Value1",dataIndex2: "Value2"},
{dataIndex1: "Value1",dataIndex2: "Value2"},
{dataIndex1: "Value1",dataIndex2: "Value2"}]

Can anyone please suggest me how to overcome from this.


回答1:


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.



来源:https://stackoverflow.com/questions/50828444/how-to-load-grid-data-with-json-data-on-ajax-sucess

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