我正在导入一个包含大量发票数据的 csv 文件。这些数据需要根据供应商 ID 组合在一起,以显示每个供应商的标题以及该供应商 ID 的所有发票的总和;然后在其下方显示每张单独的发票。该项目的目标是导入 CSV,向用户显示数据,允许通过选择框更改某些值,然后单击按钮导出到 XML。XML 的结构与 HTML 类似,其中每组发票首先显示某些公共数据,然后是汇款数据。为了实现这一点,我试图以某种方式构造对象,以帮助我使用此http://goessner.net/download/prj/jsonxml/将其正确转换为 XML 。
问题:后两个付款需要组合在一起,我希望为每行详细数据创建一个数组,然后将其添加到主对象的 PmtDetail 属性中。目前,此代码仅广告第一行而忽略其余部分。
CSV 片段(我不包括此处代码中使用的所有行)
ID CardCode payment_sum amount
1610165 BENV5271 100 100
1609026 BENV5635 509.85 287.33
1609025 BENV5635 509.85 222.52
JSON
[{"DocNum":"1610165","CardCode":"BENV5271","InvPayAmnt":"100.00","PmntDate":"2012-03-29","payment_sum":"100.00"},
{"DocNum":"1609026","CardCode":"BENV5635","InvPayAmnt":"287.33","PmntDate":"2012-03-29","payment_sum":"509.85"},
{"DocNum":"1609025","CardCode":"BENV5635","InvPayAmnt":"222.52","PmntDate":"2012-03-29","payment_sum":"509.85"}]
jQuery
$(document).ready(function() {
$.getJSON('CSV.php', function(data) {
var prevCardCode = '';
var newDiv; var NewDiv2;
var PaymentFormat;
$.each(data, function(index, element) { //looping once to display header info such as sum
XMLObject = []; // Creating a new object for each row
if (element['CardCode'] != prevCardCode) {
XMLObject.Vendor_Sum = element['payment_sum'];
XMLObject.Name1 = element['CardName'];
XMLObject.Addr1 = element['Address'];
console.log(XMLObject);
newDiv = $('<div/>').addClass('row').appendTo('#showdata');
$('<div class="sum_field">' + 'Total: ' + element['payment_sum'] + '</div>').appendTo(newDiv);
$('<div class="options">Payment Format: <select name="Payment_Format" id="Payment_Format"><option value="CTX" selected="selected">Company to Company</option><option value="PPD">Company to Person</option></select> </div><div id="Selected_Format"></div>').appendTo(newDiv);
XMLObject.paymentFormat = $('select#Payment_Format').val();;
$('select#Payment_Format').change(function(){
PaymentFormat = $(this).val();
XMLObject.paymentFormat = PaymentFormat;
});
}
newDiv2 = $('<div/>').addClass('sub_row').appendTo(newDiv);
prevCardCode = element['CardCode'];
$.each(element, function(key, value) { looping 2nd time to display the detail info
XMLObjectDetail = {}; // Creating an array for each row of detail info
XMLObjectDetail['TotalCurAmount'] = element['InvPayAmnt'];
XMLObjectDetail['NetCurAmount'] = element['InvPayAmnt'];
$('<div class="field">' + value + '</div>').appendTo(newDiv2);
XMLObject.PmtDetail = XMLObjectDetail;
});
});
});
});
PHP
<?php
if (($handle = fopen('upload/BEN-new.csv', "r")) === FALSE) {
die('Error opening file');
}
$headers = fgetcsv($handle, 1024, ',');
$complete = array();
while ($row = fgetcsv($handle, 1024, ",")) {
$complete[] = array_combine($headers, $row);
}
fclose($handle);
echo json_encode($complete);
?>