循环遍历Json数据存储区extjs

时间:2012-05-09 21:16:14

标签: javascript extjs

我需要循环遍历Json数据存储并收集所有值。最后,它应该在警报窗口中显示所有价格的总金额。我不知道如何循环遍历Json数据stoe,我感兴趣的是如果有更快或更简单的方法来做到这一点。我的想法是:

var myStore = new Ext.data.Store({
    id: 'ID_Store',
    proxy: new Ext.data.HttpProxy({
        url: 'get.php',
        method: 'POST'
    }),
    baseParams: {
        task: "LIST"
    },
    reader: new Ext.data.JsonReader({
        root: 'results',
        totalProperty: 'total',
        id: 'id'
    }, [{
        name: 'IDbook',
        type: 'int',
        mapping: 'id_book'
    }, {
        name: 'price',
        type: 'int',
        mapping: 'price'
    }])
});

var sum = 0;

myStore.load();
myStore.on('load', function () {
    for (var i = 0; i < myStore.getTotalCount(); i++) {
        sum = sum + myStore.price[i];
    }
});

alert("total sum is:", sum);

JSON是:

{success:true}{total:5,results:[{"id_book":"1","price":"10},{"id_book":"2","price":"15"},{"id_book":"3","price":"5"},{"id_book":"4","price":"7"},{"id_book":"5","price":"30"}]}

2 个答案:

答案 0 :(得分:7)

不能比这简单得多:

var total = 0;
myStore.each(function(r) {
   total += r.get('price');
})

答案 1 :(得分:1)

认为json中的第一个标签价格是"price":"10而不是