-1
我的代碼在下面分組數據並創建元素。但是缺少最後一組。我認爲我的循環不好。按json的特定字段對數據進行分組,然後創建元素
data:
[
{"GROUPID":"A","QTY":3,"STORE":"NYC"},
{"GROUPID":"A","QTY":4,"STORE":"CT"},
{"GROUPID":"B","QTY":2,"STORE":"NJ"},
{"GROUPID":"B","QTY":5,"STORE":"NJ"},
{"GROUPID":"C","QTY":1,"STORE":"MA"}
]
所以我的分組會像
甲NYC-3 NJ-4
乙MA-1 CT - 4
ÇNJ-5
到目前爲止我的循環正在創建組A和B,但也沒有創造C組 ..from AJAX
success: function(msg) {
var c = jQuery.parseJSON(msg.d);
var myGroupid = c[0].GROUPID;
var mList = "";
var sum = "";
for (var i = 0; i < c.length; i++) {
currentGroup = c[i].GROUPID;
if ((currentGroup !== myGroupid) {
//compare if current record's group id is different than previous record.
//if so, then means a new group. Send to plugin.
//call groupPlugin(myGroupid,mList)
}
mList += '<li'
" data-group=" ' + c[i].GROUPID + '" data-store="' + c[i].STORE +'"</li>'
}
}
請正確,均勻地縮進代碼。它會幫助你和其他人診斷問題。 –
@MikeC,謝謝你的提示。我試着清理了一下。我的循環遍歷每一行,基本上將當前GroupID與最後一個進行比較。如果不同,則創建一個組並添加行。但是不會爲最後一行創建組。 – causita