9

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.

Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
FLX
  • 2,626
  • 4
  • 26
  • 57
  • You should use `colspan="3"`, where `3` is the total number of columns. – Danny Beckett Jun 27 '13 at 10:38
  • I forgot to say that it will be used for multiple tables, with differents column numbers, generated automatically. I will never know the number, and i don't want to use javascript... I just know that it must take all the table width. – FLX Jun 27 '13 at 10:39
  • 1
    Generated automatically with what? PHP? If it's being generated automatically, you can get the proper attribute. – Danny Beckett Jun 27 '13 at 10:41
  • You cant have a colspan over multiple tables – Patrick Evans Jun 27 '13 at 10:42
  • I call the tables with PHP, only one per page. Then i add the footer. The php function send me directly the html, i can't count columns... – FLX Jun 27 '13 at 10:46
  • I tried to validate colspan=1000, but it failed. We can't set a colspan with a number greater than the total cells number... Wtf, HTML – FLX Jun 27 '13 at 10:56
  • That's not WTF, it makes perfect sense, you cannot span more columns than you have in your table.... – Nick R Jun 27 '13 at 11:00
  • We can't, but it works. We can't put 100%, but it works too. We can only put 0, put it doesn't work. That why I say WTF. – FLX Jun 27 '13 at 11:03
  • 4
    Well if you need the number of columns then I would **refactor** your PHP code to return the number of columns too. To trick the browser about colspan isn't such good idea (especially if you want/need to be compliant). Many things are left to user agent implementation (=undefined) so these things work by case (and 100% works because it's understood as "100" so it breaks with higher number of columns). – Adriano Repetti Jun 27 '13 at 11:04
  • 1
    Yes, this is the only way you can do it, make sure you count the columns in your PHP code, and then when you `echo` out the table, you can echo the correct `colspan` number – Nick R Jun 27 '13 at 11:07
  • Please see this answer http://stackoverflow.com/a/1470950/942179, which proposes to use the caption element for this. Semantically this is what you really want, it works on all browsers and is HTML 5 conforming. And with a tiny little bit of CSS styling you can also make it look right. – Elmar Zander Mar 05 '15 at 20:58
  • This is a nice way, but now my tables can have multiple rows like that and the caption element is allowed only once. I made a table generating w̶h̶e̶e̶l̶ class setting the right colspan for me. – FLX Mar 06 '15 at 10:18

3 Answers3

5

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...

Community
  • 1
  • 1
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
  • Thanks, i'll do that. I'm still a bit shocked that this simple old thing is supported by only one browser. – FLX Jun 27 '13 at 12:16
  • @FC' well, it's not part of the standard but I wonder it has never been included, I have to say that it's not so uncommon and it would easy our life (but it may complicate UA calculation for complex layouts) – Adriano Repetti Jun 27 '13 at 13:18
  • 4
    I just check the w3c doc. They removed this for HTML5, but only for colspan. 0 is a valid value for rowspan only. ARE YOU JUST KIDDING ME WWW? – FLX Jun 27 '13 at 13:31
  • @FC' LOL well, a row doesn't need to contain ALL columns, it could be tricky to do the computation... – Adriano Repetti Jun 27 '13 at 13:57
1

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;

Notes:

  1. Sometimes the table contains a thead, a tbody and a tfoot. In such case it is needed to modify the code using the id of one of them that has the correct number of columns to return.
  2. The given examples, in some situations, might work only when rendered by the browser, if rendered for printing they might won't work anymore and table layout might be different.

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>
willy wonka
  • 1,440
  • 1
  • 18
  • 31
  • Note that you can also make a javascript function which is passed in a Node (the row-node) rather than the id-of-the-node, and then call that function from each `tr` passing it `this`. – not-just-yeti Aug 03 '18 at 19:11
  • @not-just-yeti, can you make an example, please? Thanks – willy wonka Sep 08 '18 at 09:51
0

The <caption> tag is well supported and will give you a result similar to <tfoot colspan="1000">:

table {
  background: #eee;
}

table caption {
  background: #e0e0e0;
}

td,
th {
  margin: 5px;
  padding: 5px;
}
<table>
  <thead>
    <th>Quote</th>
    <th>Author</th>
    <th>More</th>
    <tbody>
      <tr>
        <td>One cow at a time, set free the entire herd</td>
        <td colspan="2">Prosper Stupefy</td>
      </tr>
    </tbody>
    <caption style="caption-side:bottom">This will always span to the full width of the table</caption>
    <caption style="caption-side:top">A list of made-up quotes</caption>
</table>

Note that you will need to css style it.

Documentation: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/caption

oriadam
  • 7,747
  • 2
  • 50
  • 48