I am importing a csv file which has a lot of invoice data. This data needs to be grouped together based on Vendor ID to display a heading for each Vendor with a sum of all invo
Some thoughts:
$.each(data, ... //looping once to display header info such as sum
You're not creating a sum anywhere. Also, this is the one and only loop that iterates over all data
rows.XMLObject = []; // Creating a new object for each row
- No, you're creating an Array. You really should create an Object ({}
), because you use it as an object.XMLObject =...
. You're lacking a var
keyword here. You create a new object each iteration, but assign every of them to the same global variable. This is the reason the the #Payment_Format
change handler will only change the format of the last created object - the one currently referenced by "XMLObject
".$('...
You are creating a select with an id each iteration. How do you think that id will be unique? That's also why ('select#Payment_Format')
will select not the element created in the current iteration.$.each(element... looping 2nd time to display the detail info
No. This is not a second loop, but a loop which will iterate over the properties of the current element
- the loop will be applied on each of the iterated data
-rows.XMLObjectDetail = {}; // Creating an array for each row of detail info
- No, you're creating an object. Again, you're missing the var
keyword.XMLObject.PmtDetail = XMLObjectDetail;
- you overwrite the "PmtDetail" property each iteration of elements in here. Shouldn't that be an array you append to?XMLObject
, containing data about the current row. Don't you want to do something with it, for example push it onto an array of row-objects?OK, I think now I'm getting what you try to achieve. An appropriate structure might be:
[ {
"CardCode":"BENV5271"
"details": [ {
"DocNum": "1610165",
"InvPayAmnt": "100.00",
"PmntDate": "2012-03-29"
} ],
"payment_sum": "100.00"
}, {
"CardCode": "BENV5635",
"details": [ {
"DocNum": "1609026"
"InvPayAmnt": "287.33",
"PmntDate": "2012-03-29"
}, {
"DocNum": "1609025",
"InvPayAmnt": "222.52",
"PmntDate": "2012-03-29"
} ],
"payment_sum": "509.85"
} ]
or the same, just as an Object (key-value-map) by id:
{
"BENV5271" : {
"details": {
"1610165": {
"InvPayAmnt": "100.00",
"PmntDate": "2012-03-29"
}
}
"payment_sum": "100.00"
},
"BENV5635": {
"details": {
"1609026": {
"InvPayAmnt": "287.33",
"PmntDate": "2012-03-29"
},
"1609025": {
"InvPayAmnt": "222.52",
"PmntDate": "2012-03-29"
}
},
"payment_sum": "509.85"
}
}
You also might use the number type of JSON for number values, instead of string :-) You should create that structure right away from your database. As you have seen, CSV is not the best representation for it, and as JSON and XML are capable of representing it you should not go SQL->CSV->JSON(table)->JSON(structured)->XML but SQL->JSON(structured)->XML or even better right away SQL->XML. You can easily read XML with Ajax and change its DOM in JavaScript.
If you really need to do the restructuring from malformed Objects to a nice structure in JS, this would be the code:
var cards = {}; // an object indexed by the CardCodes;
for (var i=0; i