2012-05-09 45 views
1

我需要遍歷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"}]} 

回答

6

無法獲得簡單得多那麼這個:

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

即將發佈相同的答案。 +1 –

1

認爲在JSON第一標籤價格錯誤是代替"price":"10

+0

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