I have a HTML table, and i would like to give the last row (in <tfoot>
tag) only one cell expanding to all the table.
I was using colspan="0"
, then i saw that it only worked in Firefox.
I then tried colspan="100%"
.
It works fine, but not pass the w3c validator (Very important in my project).
Is there a working alternative ?
I saw people who use colspan="1000"
, not a bad idea but are there some performance problems with this ?
Thanks for advice.
You could use a grid like structure, using divs.
Description. number. Specifies the number of columns a cell should span. Note: colspan="0" tells the browser to span the cell to the last column of the column group (colgroup)
number. Specifies the number of rows a cell should span. Note: rowspan="0" tells the browser to span the cell to the last row of the table section (thead, tbody, or tfoot).
The colspan attribute in HTML specifies the number of columns a cell should span. It allows the single table cell to span the width of more than one cell or column.
My first answer is: refactor your code. If you need the total number of columns to build the table footer then the function you use to build the table body should return that number (and not only the HTML).
That said and only in case it's too complicated (or you don't have control about that code) you may simply count them by yourself, I would avoid any trick about colspan
because it's behavior isn't homogeneous (and it's not validated too).
You can easy count the number of cells using the first row (if the table is well formed all the rows have the same number of columns).
The first naive solution would be to split()
HTML tbody then to substr_count()
the <td/>
of the first row. Unfortunately this may work only in a very controlled situation (tables must be well formed, table may contain or not tbody
and it doesn't manage colspan
of that cells).
Better solution involves a small HTML parser (see this great post here on SO for a good and detailed list), when you have DOM then you can easily count TDs and check for their attributes (I say this in advance: no, you can't use regex to parse HTML).
To be honest I think refactoring is much more suitable...
Might be the following code I use could be useful too:
var len = document.getElementById("myTable").rows[0].cells.length;
document.getElementById("myTablelastrowfirstcell").colSpan = len;
the first line gets into the variable len the number of cells (td or th elements, doesn't matter, if all the table has the same number of column) contained into the first row of mytable;
The second row modifies the colspan property/attribute of the targeted cell positioned into the last row and sets it to the value previously got.
The following has the same code, shortened:
document.getElementById("myTablelastrowfirstcell").colSpan = document.getElementById("myTable").rows[0].cells.length;
Working example (click on run code example at the end of the code to run it and see how it operates):
function spanLastRowCols() {
var len = document.getElementById("myTable").rows[0].cells.length;
var len = document.getElementById("ext").colSpan = len;
info.innerText = len;
}
table, td {
border: 1px solid black;
}
<p>Click the button to change the content of the first table cell.</p>
<p>Then copy and paste some td cells for each rows forthe first and the second tr. Than run the snippet again and click the button to change the content of the first table cell to compare differences.</p>
<table id="myTable">
<tr>
<td>Row1 cell1</td>
<td>Row1 cell2</td>
<td>Row1 cell3</td>
<td>Row1 cell4</td>
<td>Row1 cell5</td>
</tr>
<tr>
<td>Row2 cell1</td>
<td>Row2 cell2</td>
<td>Row2 cell3</td>
<td>Row2 cell4</td>
<td>Row2 cell5</td>
</tr>
<tr>
<td id= "ext">Row3 cell1</td>
</tr>
</table>
<br>
<button onclick="spanLastRowCols()">Try it</button>
Last row colSpan attribute value: <span id="info"></span>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With