Draw table in following format with colspan and rowspan in HTML? - html

I want to draw a table in following format:
But it's not displaying properly. It's inserting some rows after the last column.
How can I fix the HTML?
<tr>
<th rowspan="2">Client ID</th>
<th rowspan="2">Trade ID</th>
<th rowspan="2">Symbol</th>
<th rowspan="2">Average Price</th>
<th colspan="3">DTD</th>
<th colspan="3">MTD</th>
<th rowspan="2">Net YTD</th>
</tr>
<tr>
<td><core:out value="${orderBookData.clientID}" /><br></td>
<td><core:out value="${orderBookData.tradeID}" /></td>
<td><core:out value="${orderBookData.symbol}" /></td>
<td><core:out value="${orderBookData.averagePrice}" /></td>
<td><core:out value="${orderBookData.DTD}" /></td>
<td><core:out value="${orderBookData.MTD}" /></td>
<td><core:out value="${orderBookData.YTD}" /></td>
<td><core:out value="${orderBookData.DTD}" /></td>
<td><core:out value="${orderBookData.MTD}" /></td>
<td><core:out value="${orderBookData.YTD}" /></td>
<td><core:out value="${orderBookData.YTD}" /></td>
</tr>

Your header row is perfectly accurate. However, you need to account for the extra rows and columns with your subsequent <td> tags. Since columns 1-4 and 7 are rowspan="2", you need a second <tr> with two <td> tags to account for the extra cells needed in columns 5-6. Further, since you want three cells each under columns 5-6, you need to triple this number. Thus six would be needed:
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
All subsequent rows below that will need 11 <td> tags, since you must account for all seven columns plus the four extra cells.
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
JS Fiddle: http://jsfiddle.net/Aj5k7/2/
Tutorial on the rowspan and colspan attributes: http://www.tizag.com/htmlT/tables.php/

Related

How do you label the horizontal and vertical axes of a table?

Suppose you have a table where the vertical axis is height, the horizontal axis is weight, and the table cells are the corresponding BMI. How would you label the vertical axis "Height" and the horizontal axis "Weight"?
<table>
<thead>
<tr>
<th></th>
<th>140 lbs</th>
<th>150 lbs</th>
<th>160 lbs</th>
</tr>
</thead>
<tbody>
<tr>
<th>5'4</th>
<td>24</td>
<td>25.7</td>
<td>27.5</td>
</tr>
<tr>
<th>5'6</th>
<td>22.6</td>
<td>24.2</td>
<td>25.8</td>
</tr>
<tr>
<th>5'8</th>
<td>21.3</td>
<td>22.8</td>
<td>24.3</td>
</tr>
</tbody>
</table>
You want to use colspan and rowspan to merge cells. Practice drawing your table in excel if it helps you get an idea of what the final table would look like. In excel, you would have to merge cells to accomplish the same thing.
<table>
<thead>
<tr>
<th colspan="4">Weight</th>
</tr>
<tr>
<th></th>
<th>140 lbs</th>
<th>150 lbs</th>
<th>160 lbs</th>
</tr>
</thead>
<tbody>
<tr>
<th rowspan="4">Height</th>
</tr>
<tr>
<th>5'4</th>
<td>24</td>
<td>25.7</td>
<td>27.5</td>
</tr>
<tr>
<th>5'6</th>
<td>22.6</td>
<td>24.2</td>
<td>25.8</td>
</tr>
<tr>
<th>5'8</th>
<td>21.3</td>
<td>22.8</td>
<td>24.3</td>
</tr>
</tbody>
</table>

Is there a tag for grouping "td" or "th" tags?

Does a tag for grouping th or td elements exist?
I need something like this:
<table>
<thead>
<tr>
<th></th>
<th></th>
<GROUP>
<th></th>
<th></th>
</GROUP>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<GROUP>
<td></td>
<td></td>
</GROUP>
</tr>
</tbody>
</table>
No, col and colgroup (which must be inserted before the first element) are not a solution.
Does exists a tag for grouping th or td element?
Yes, it's called colgroup.
Don't want to use a colgroup? Sorry, that's how grouping table columns is done in HTML. Use the tools given to you, or don't.
for grouping cols (or rows ) in a html table you can use colspan (or rowspan) attribute
<table>
<caption>Test Caption </caption>
<tr> <th colspan="2">65</th>
<th colspan="2">40</th>
<th colspan="2">20</th>
</tr>
<tr>
<th>Men</th>
<th>Women</th>
<th>Men</th>
<th>Women</th>
<th>Men</th>
<th>Women</th>
</tr>
<tr>
<td>82</td>
<td>85</td>
<td>78</td>
<td>82</td>
<td>77</td>
<td>81</td>
</tr>
</table>
(or nested table)

Table inside another table squashed

Am having trouble nesting one table inside another:
I want the inner tables to span across the entire width of the outer table. Here is the generated HTML:
<table class="table table-bordered table-striped">
<tr><td align="center">4 laptops were not added because they lacked unique identifiers.</td></tr> <tr><td align="center">2 laptops were not added because they already exist in the database: </td></tr><tr><td>
<table><th>Hostname</th><th>Asset Tag</th><th>Serial</th>
<tr>
<td></td>
<td></td>
<td>4646466</td>
</tr>
<tr>
<td></td>
<td></td>
<td>4646467</td>
</tr>
</table></td></tr>
<tr><td align="center">2 laptops were added to the database: </td></tr><tr><td>
<table><th>Hostname</th><th>Asset Tag</th><th>Serial</th>
<tr>
<td></td>
<td></td>
<td>4646468</td>
</tr>
<tr>
<td></td>
<td></td>
<td>4646469</td>
</tr>
</table></td></tr>
</table>
Have a look
<table class="table table-bordered table-striped" border="1">
<tr><td align="center">4 laptops were not added because they lacked unique identifiers.</td></tr> <tr><td align="center">2 laptops were not added because they already exist in the database: </td></tr><tr><td>
<table border="1" style="width:100%;"><th>Hostname</th><th>Asset Tag</th><th>Serial</th>
<tr>
<td></td>
<td></td>
<td>4646466</td>
</tr>
<tr>
<td></td>
<td></td>
<td>4646467</td>
</tr>
</table></td></tr>
<tr><td align="center">2 laptops were added to the database: </td></tr><tr><td>
<table><th>Hostname</th><th>Asset Tag</th><th>Serial</th>
<tr>
<td></td>
<td></td>
<td>4646468</td>
</tr>
<tr>
<td></td>
<td></td>
<td>4646469</td>
</tr>
</table></td></tr>
</table>
Just add width attribute
width = '100%'
to the inner table. It will make it spread all over the width of the containing cell.
DEMO

Format HTML Table header

I am trying to format a table to look like this...
Basically i want the "Dates" row to have two columns inside it (to and from) both of them 50% the width of dates...but however when i try to format it. "To" takes all of date and "From" takes all of Name. they arent locked under "Dates"
Any help will be appreciated...Thank you
<th width="100%">Dates</th><th>Name</th><th>Age</th>
<tr>
<tr>
<td width="50%">To</td>
<td width="50%">From</td>
</tr>
</tr>
Change
<table border="1">
<tr class="heading"> <td colspan="6">Information</td> </tr >
<th width ="15" colspan="2">Dates</th><th> Name</th><th>Age</th>
<tr>
<tr>
<td width="2">From</td>
<td width="2">To</td>
<td></td>
<td></td>
</tr>
<tr>
<td width="5">
<input type="text" class="input" name="1fdate" /></td>
<td width="2">
<input type="text" class="input" name="1fdate" /></td>
</tr>
</tr>
</table>
I hope this is what you need. You use colspan and rowspan to merge the cells. When you set colspan to "2" in Date cell, it spans the row with two cells (or colums). And you set also rowspan of the cells next to Date to "2" so that they will span the rows taken by whole Date section.
<table width="600" border="0">
<tr>
<th width="200" colspan="2" scope="col">Date</th>
<th width="200" rowspan="2" scope="col">Name</th>
<th width="200" rowspan="2" scope="col">Age</th>
</tr>
<tr>
<th width="100">To</th>
<th width="100">From</th>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
Change
<th width="100%">Dates</th>
to have colspan value. Like
<th colspan="2">Dates</th>
Replace first line with below
<th width="100%" colspan="2">Dates</th><th>Name</th><th>Age</th>

Is it possible to lay out a table with 2 columns. The first column with many td's, the second one with one?

Is it possible to lay out a table with 2 columns. The first column with many td's, the second one with only one?
Yes, use colspan...or you might want rowspan (colspan's opposite :))
Directly from the article (with enclosing the attributes in quotes:
<TABLE BORDER="2" CELLPADDING="4">
<TR> <TH COLSPAN="2">Production</TH> </TR>
<TR> <TD>Raha Mutisya</TD> <TD>1493</TD> </TR>
<TR> <TD>Shalom Buraka</TD> <TD>3829</TD> </TR>
<TR> <TD>Brandy Davis</TD> <TD>0283</TD> </TR>
<TR> <TH COLSPAN="2">Sales</TH> </TR>
<TR> <TD>Claire Horne</TD> <TD>4827</TD> </TR>
<TR> <TD>Bruce Eckel</TD> <TD>7246</TD> </TR>
<TR> <TD>Danny Zeman</TD> <TD>5689</TD> </TR>
</TABLE>
Here is the W3 article
yes use rowspan or colspan to merge td. Example:
<table>
<tr>
<td></td><td></td><td></td>
</tr>
<tr>
<td rowspan="3"></td>
</tr>
</table>