filamentgroup / tablesaw

A group of plugins for responsive tables.
MIT License
5.48k stars 434 forks source link

Colspans in columntoggle mode #221

Closed apdrsn closed 7 years ago

apdrsn commented 8 years ago

Hi, and thanks for your great work on tablesaw :)

Is it possible to get the table to behave correctly when using colspans? Here is an example:

<table data-tablesaw-mode="columntoggle">
    <thead>
        <tr>
            <th data-tablesaw-priority="persist">Name</th>
            <th data-tablesaw-priority="1">Budget</th>
            <th data-tablesaw-priority="2">Actual</th>
            <th data-tablesaw-priority="persist">Total</th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <td colspan="3">Total</td>
            <td>150</td>
        </tr>
    </tfoot>
    <tbody>
        <tr>
            <td>Some name</td>
            <td>10</td>
            <td>30</td>
            <td>120</td>
        </tr>
        <tr>
            <td>Some other name</td>
            <td>20</td>
            <td>15</td>
            <td>30</td>
        </tr>
    </tbody>
</table>

What will happen when I toggle one of the columns off, is that colspan will still occupy three cells, even though it "should" only take two.

I'm aware that it is a bit complex – because which colspan should you take one cell from in case there were several cells with colspans?

Well, just wanted to ask if this is something you have solved before?

jgibson commented 8 years ago

AFAICT tablesaw doesn't support rowspans or colspans in the data. Colspans in the headers are respected, however. Taking a look at initCells (as of 2.0.3) you can see that Tablesaw scans the table headers and then assigns the cells in the rows that correspond to each header to be toggled on or off later. Unfortunately the assignment algorithm ignores any colspans in the data. If you were to change your footer to look like this:

<tfoot>
        <tr>
            <td>Always here</td>
            <td colspan="2">Total</td>
            <td>150</td>
        </tr>
</tfoot>

Then I'd expect that toggling off the Budget column would hide the total column in the footer, but that the Actual column would have no effect on it (probably it would instead hide the 150 cell in the footer).

I adjusted the assignment algorithm to allow colspans in the data, but with the caveat that a cell with a colspan in the data can't cross multiple columns in the header. I'll submit a PR or something so that others can take advantage of it.

Your problem is thornier because instead of simply hiding or showing cells you'd have to adjust the colspan of your data cells on the fly. Taking a look at issue #48 you can see that other people have the reverse requirement (complex colspans and row spans in a multirow header). I can't see a way to provide the functionality that would satisfy both of you without a creating a comprehensive model of the table (as opposed to just assigning a group of cells to a single column header) and increasing the complexity of the toggling algorithm.

jgibson commented 8 years ago

I opened #225 for the simple case that I described above.

apdrsn commented 8 years ago

Thank you very much! I will take a deeper look when I return from vacation.

zachleat commented 7 years ago

225 has been merged and is available in the early 3.0 betas