I have the following simplified HTML Below.
<div id="coat">
<span class="Jan2011-Sales">10</span>
<span class="Feb2011-Sales">10</span>
<span class="Mar2011-Sales">10</span>
</div>
<div id="boot">
<span class="Jan2011-Sales">10</span>
<span class="Feb2011-Sales">10</span>
<span class="Mar2011-Sales">10</span>
</div>
<div id="hat">
<span class="Jan2011-Sales">10</span>
<span class="Feb-Sales">10</span>
<span class="Mar2011-Sales">10</span>
</div>
<div id="etc.">
</div>
EDITED:
What I’d like to do is create a table like below: (almost like a pivot table)
<th>
<td>Period</td>
<td>Coat</td>
<td>Boot</td>
<td>Hat</td>
</th>
<tr>
<td>Jan2011-Sales</td>
<td>10</td>
<td>10</td>
<td>10</td>
<tr>
<tr>
<td>Feb2011-Sales</td>
<td>10</td>
<td>10</td>
<td>10</td>
<tr>
etc.
My question is — how do I iterate over the original HTML?
e.g. my thinking is along the lines of iterating over the first elements to get the rows, then getting the parent’s sibling div so that I can find/match its child to get the columns.
Thoughts?
THank you.
Here’s a jsFiddle that takes your HTML data and make a table out of it: http://jsfiddle.net/jfriend00/RKBAj/.
Making these assumptions:
Then, here’s how you could iterate over the data and collect all the data into an organized data structure from which you could build a table.
The data is stored like this:
And, this is one way you could create your table from the data. The tricky part of generating the table is that if any product can have any number of months and all products don’t necessarily have the same months, then you have to make a row for every month that exists and fill in product data if it has data for that month.