Extjs 4 商店上的加载侦听器不起作用

发布于 2025-01-04 10:29:56 字数 3639 浏览 4 评论 0原文

我一直在寻找类似 Extjs 4 中商店的“加载”监听器之类的东西,但我遇到了一些问题。

1)我的印象是“load”侦听器方法的“success”参数告诉我们操作是否成功,但“success”参数包含一个数组。我不知道该数组包含什么,但在调用“success.length”属性后,我发现它包含我的服务器端代码作为响应发送的实际行数。所以我认为“成功”属性实际上包含我的数据,但我不确定。

2)如果我在“记录”或“成功”参数上使用 Ext.each() 方法,我将无法看到加载的实际数据。如何查看实际数据?

商店代码如下所示:

Ext.define('myCompany.store.customerDistribution', {
    extend: 'Ext.data.TreeStore',
    autoLoad: true,

    proxy: {
        type: 'ajax',
        url: 'data/customerDistribution/customerCount.json',
        reader: {
            type: 'array',
            root: 'resultset'
        }
    },
    listeners: {
        load: function(store, records, success) {
            /*console.log('store - ' + store +
                        ', typeof(store) - ' + typeof(store) +
                        ', records - ' + records +
                        ', records data - ' + records.data +
                        ', success - ' + success +
                        ', type of success - ' + typeof(success)+
                        ', success length - ' + success.length);
            if(records == true) {
                console.log('records is data property...');
            }
            if(store == true) {
                console.log('store is a data property...');
            }
            for(r in records) {
                console.log(r + '\ttypeof(r) -> ' + typeof(r));
            } */
            if(success && records.length > 0) {
                var allCountries = [];
                var previousCountry = undefined;

                var countryIndex = 0;
                var stateIndex = 1;
                var cityIndex = 2;
                var customerCountIndex = 3;

                Ext.each(records, function(record, index){
                    console.log('record - ' + record[0] + ', ' + record[1]);
                });
            }
        }
    }
});

我正在尝试将基于行的 json 转换为分层 json 以显示在树面板中。这就是我使用负载列表器的原因。 我的 Json 如下所示:

{
    "queryInfo":{"totalRows":"100"},

    "resultset":[
        ["India","India","India",63],
        ["India",""," Tirupati ",1],
        ["India",""," UTTARPARA ",1],
        ["India","Andhra Pradesh",null,61],
        ["India","Andhra Pradesh"," Chittoor ",1],
        ["India","Andhra Pradesh"," Guntur ",2],
        ["India","Andhra Pradesh"," Hyderabad ",58]
      ],
    "metadata":[
        {"colIndex":0,"colType":"String","colName":"Country"},
        {"colIndex":1,"colType":"String","colName":"State"},
        {"colIndex":2,"colType":"String","colName":"City"},
        {"colIndex":3,"colType":"Integer","colName":"customer_count"}
    ]
}

我想将其转换为:

"resultset": [
    countries: [
    {
        name: "India",
        customer_count: 63
        states: [
            {
                name: "Andhra Pradesh",
                customer_count: 61,
                cities: [
                    {
                        name: "Tirupati",
                        customer_count: 1
                    },
                    {
                        name: "UTTARPARA",
                        customer_count: 1
                    },
                    {
                        name: "Chittoor",
                        customer_count: 1
                    },

                    {
                        name: "Guntur",
                        customer_count: 2
                    },
                    {
                        name: "Hydrabad",
                        customer_count: 58
                    }
                ]
            }
        ]
    }
]

请帮助!

I was looking for something like 'load' listener of store in Extjs 4, but I am facing some problems.

1) I was under impression that 'success' parameter to 'load' listener method tells us whether operation was successful or not, but 'success' parameter contains one array. I dont know what do that array contains but after invoking 'success.length' property I found out that it contains actual number of rows which my server side code had sent as a response. So I think 'success' property actually contains my data, but I am not sure about it.

2) If I use Ext.each() method on 'records' or 'success' parameter, I am not able to see actual data loaded. How to see the actual data?

Code for store looks as below :

Ext.define('myCompany.store.customerDistribution', {
    extend: 'Ext.data.TreeStore',
    autoLoad: true,

    proxy: {
        type: 'ajax',
        url: 'data/customerDistribution/customerCount.json',
        reader: {
            type: 'array',
            root: 'resultset'
        }
    },
    listeners: {
        load: function(store, records, success) {
            /*console.log('store - ' + store +
                        ', typeof(store) - ' + typeof(store) +
                        ', records - ' + records +
                        ', records data - ' + records.data +
                        ', success - ' + success +
                        ', type of success - ' + typeof(success)+
                        ', success length - ' + success.length);
            if(records == true) {
                console.log('records is data property...');
            }
            if(store == true) {
                console.log('store is a data property...');
            }
            for(r in records) {
                console.log(r + '\ttypeof(r) -> ' + typeof(r));
            } */
            if(success && records.length > 0) {
                var allCountries = [];
                var previousCountry = undefined;

                var countryIndex = 0;
                var stateIndex = 1;
                var cityIndex = 2;
                var customerCountIndex = 3;

                Ext.each(records, function(record, index){
                    console.log('record - ' + record[0] + ', ' + record[1]);
                });
            }
        }
    }
});

I am trying to convert row based json to hierarchical json to be displayed in tree panel. That is why I am using load listner.
My Json looks as follows :

{
    "queryInfo":{"totalRows":"100"},

    "resultset":[
        ["India","India","India",63],
        ["India",""," Tirupati ",1],
        ["India",""," UTTARPARA ",1],
        ["India","Andhra Pradesh",null,61],
        ["India","Andhra Pradesh"," Chittoor ",1],
        ["India","Andhra Pradesh"," Guntur ",2],
        ["India","Andhra Pradesh"," Hyderabad ",58]
      ],
    "metadata":[
        {"colIndex":0,"colType":"String","colName":"Country"},
        {"colIndex":1,"colType":"String","colName":"State"},
        {"colIndex":2,"colType":"String","colName":"City"},
        {"colIndex":3,"colType":"Integer","colName":"customer_count"}
    ]
}

and I want to convert it to :

"resultset": [
    countries: [
    {
        name: "India",
        customer_count: 63
        states: [
            {
                name: "Andhra Pradesh",
                customer_count: 61,
                cities: [
                    {
                        name: "Tirupati",
                        customer_count: 1
                    },
                    {
                        name: "UTTARPARA",
                        customer_count: 1
                    },
                    {
                        name: "Chittoor",
                        customer_count: 1
                    },

                    {
                        name: "Guntur",
                        customer_count: 2
                    },
                    {
                        name: "Hydrabad",
                        customer_count: 58
                    }
                ]
            }
        ]
    }
]

Please help !!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒冷纷飞旳雪 2025-01-11 10:29:56

您选择了错误的加载事件签名。在TreeStore中,它具有以下签名load(Ext.data.TreeStore this, Ext.data.NodeInterface节点,Ext.data.Model[]记录,Boolean success,Object eOpts)。此外,记录以树结构排列,因此您无法使用 each 迭代它们。

以下是将树中的每条记录记录到控制台的示例代码:

var store = Ext.create('Ext.data.TreeStore', {
    model: 'Task',
    proxy: {
        type: 'ajax',
        url: 'treegrid.json'
    },

    logRecord: function(r, depth) {
        if (!depth) { depth = ''; }
        console.log(depth + Ext.encode(r.data));

        Ext.each(r.childNodes, function(record, index){
            this.logRecord(record, depth + '    ');
        }, this);
    },

    listeners: {
        load: function(sender, node, records) {
            Ext.each(records, function(record, index){
                this.logRecord(record);
            }, this);
        }
    }
});

You have choosen wrong load event signature. In TreeStore it has following signature load( Ext.data.TreeStore this, Ext.data.NodeInterface node, Ext.data.Model[] records, Boolean successful, Object eOpts ). In addition records are arranged in tree structure, so you can't iterate through them with each.

Here is example code which logs each record in tree to console:

var store = Ext.create('Ext.data.TreeStore', {
    model: 'Task',
    proxy: {
        type: 'ajax',
        url: 'treegrid.json'
    },

    logRecord: function(r, depth) {
        if (!depth) { depth = ''; }
        console.log(depth + Ext.encode(r.data));

        Ext.each(r.childNodes, function(record, index){
            this.logRecord(record, depth + '    ');
        }, this);
    },

    listeners: {
        load: function(sender, node, records) {
            Ext.each(records, function(record, index){
                this.logRecord(record);
            }, this);
        }
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文