I am working with node.js and using Jade as the template engine. I have JSON response that I am getting from the API.
Fairly standard. This is what it looks like:
Rows:
{ Row:
[ { Header:
{ ColData:
[ { value: 'GS & CO' },
{ value: '' },
{ value: '' },
{ value: '' },
{ value: '' },
{ value: '' },
{ value: '' } ] },
Rows:
{ Row:
[ { ColData:
[ { value: '01/31/2014' },
{ value: 'Bill' },
{ value: 'FY/2013-01/2014' },
{ value: '01/31/2014' },
{ value: '9963.14' },
{ value: '9963.14' },
{ value: '9963.14' } ],
type: 'Data' },
{ ColData:
[ { value: '02/28/2014' },
{ value: 'Bill' },
{ value: '02/2014' },
{ value: '02/28/2014' },
{ value: '6378.14' },
{ value: '6378.14' },
{ value: '16341.28' } ],
type: 'Data' },
{ ColData:
[ { value: '03/31/2014' },
{ value: 'Bill' },
{ value: '03/2014' },
{ value: '03/31/2014' },
{ value: '2556.0' },
{ value: '2556.0' },
{ value: '18897.28' } ],
type: 'Data' },
Summary:
{ ColData:
[ { value: 'Total for GS & CO' },
{ value: '' },
{ value: '' },
{ value: '' },
{ value: '27567.44' },
{ value: '27567.44' },
{ value: '' } ] },
type: 'Section' },
In my server (app.js), I pass the JSON to the view like so:
res.render('home.jade', {title: "hello",
reportname: report["Header"]['ReportName'],
daterange: "From:"+report["Header"]["StartPeriod"]+" to: "+report["Header"]["EndPeriod"],
alldata: report,
columns: report["Columns"],
rowsperclient: report["Rows"]["Row"]
});
In my view, I am trying to build a table using the columns and row data provided to me by the JSON. I am hitting a wall when I want to loop over just the Header parts of each Row.
// First loop returns total number of records
// for our API call
each row, idx in rowsperclient
each header, idx2 in row
h5= idx2 +" -> "+rowsperclient[idx]
This gives me the following HTML output:
Header -> [object Object]
Rows -> [object Object]
Summary -> [object Object]
What I am trying to do is display the first value of each header - this is the company name.
Then I want to loop over the ColData and display it accordingly.
I can't figure out how to just loop over the Header portion in each row.
You probably want to do something like
each cell, idx in rowsperclient.Header.ColData
h5= idx +" -> "+ cell.value
Or like
each row in rowsperclient.Rows.Row
each cell, idx in row.ColData
h5= rowsperclient.Header.ColData[idx].value +" -> "+ cell.value
By the way, the structure has really bad names (rows in rows, "row" (variable name without plural) for collection, "rows" for not-a-collection, ...). That makes development and maintainability a lot harder.