循环访问Json数据存储extjs

问题描述:

我需要遍历Json数据存储并收集所有值。最后,它应该显示警报窗口中所有价格的总量。我不知道如何循环浏览Json数据库,如果有更快或更简单的方法来执行此操作,我很感兴趣。我的想法是:循环访问Json数据存储extjs

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

无法获得简单得多那么这个:

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

即将发布相同的答案。 +1 –

认为在JSON第一标签价格错误是代替"price":"10

+0

而不是什么? – 2012-09-28 07:00:08