此代码从正在做 json_ encode 的 php 文件中获取数据, 它正在从 csv 文件中提取信息 。 我希望在第二个循环中能够有一个父数据, 围绕每组数值。 我用新的 Div2 尝试过这一点, 但是它用. sub_ row 将每个. field 和. sub_ row 包在一起 。
代替此:
<div class="row"> // This one happens to only have one payment
<div class="sum_field">Total: 79.99</div>
<div class="field">BENV5239</div> // Details of payment
<div class="field">11111</div>
</div>
<div class="row"> // Based on Vendor ID
<div class="sum_field">Total: 2487.01</div> // Sums up all payments for that Vendor
<div class="field">BENV2137</div>
<div class="field">11111</div>
<div class="field">BENV2137</div>
<div class="field">111111</div>
</div>
<div class="row"></div> // Another Vendor ID
应当:
<div class="row">
<div class="sum_field">Total: 200.00</div>
<div class="sub_row"> // one payment
<div class="field">BENV5239</div> // Details of payment
<div class="field">11111</div>
</div>
</div>
<div class="row">
<div class="sum_field">Total: 2487.01</div>
<div class="sub_row"> // each payment that has the same vendor id is in it s own div
<div class="field">BENV2137</div>
<div class="field">11111</div>
</div>
<div class="sub_row">
<div class="field">BENV2137</div>
<div class="field">111111</div>
</div>
</div>
PHP 代码码
$(document).ready(function() {
$.getJSON( WF-XML.php , function(data) {
//JSON.stringify(data);
var prevCardCode = ;
var newDiv;
$.each(data, function(index, element) {
if (element[ CardCode ] != prevCardCode) {
newDiv = $( <div/> ).addClass( row ).appendTo( #showdata );
$( <div class="sum_field"> + Total: + element[ payment_sum ] + </div> ).appendTo(newDiv);
}
prevCardCode = element[ CardCode ];
$.each(element, function(key, value) {
switch (key) {
case InvKey :
case PostDate :
case City :
break;
default:
//newDiv2 = $( <div/> ).addClass( sub_row ).appendTo( .row );
$( <div class="field"> + value + </div> ).appendTo(newDiv);
break;
}
});
});
});
});
JSON - 我翻翻了两次,第一次我只打印了这笔金额( 正在SQL内部计算, 第二次打印了其余的信息 ) 。 您可以看到最后的2个对象是同一销售商的 Id 。 因此, 我希望这能给每个新销售商加上一个父付款项, 然后给每个个人付款。 我的代码已经将每组付款都用父付款项包起来, 但我无法找到如何用父付款项包起来 。
{"VendorID":"BENV5239","payment_sum":"79.99","Address":"525 Sapper St.","ZipCode":"19116"},
{"VendorID":"BENV2137","payment_sum":"2487.01","InvPayAmnt":"108.92","Address":"340 Middle Road","ZipCode":"19037"},
{"VendorID":"BENV2137","payment_sum":"2487.01","InvPayAmnt":"57.60","Address":"340 North Middle Road","ZipCode":"19037"}