Home:ALL Converter>Looping through the Json data store extjs

Looping through the Json data store extjs

Ask Time:2012-05-10T05:16:13         Author:Goran

Json Formatter

I need to looping through the Json data store and gather all the values. Finally, it should show the total amount of all price in alert window. I do not know how to looping through the Json data stoe, and I am interested if there's a quicker or easier way to do this. My idea is:

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 is:

{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"}]}

Author:Goran,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/10524199/looping-through-the-json-data-store-extjs
sha :

Can't get much simpler then this:\n\nvar total = 0;\nmyStore.each(function(r) {\n total += r.get('price');\n})\n",
2012-05-09T21:24:39
yy