How to fix column in table accessible?

Asked

Viewed 4,360 times

2

I have a table with several columns and I need the first column to be fixed on the table. However, all the "ready" solutions I found create two tables, which is inaccessible.

I need the result exactly like this plugin http://www.michaelkeck.de/projects/jquery/tinytbl/ does. However, without creating a new HTML structure, let alone divide the content into two tables, which makes everything inaccessible.

I have also tried using position:Bsolute in the first column, but then the height of the lines is not equal.

Has anyone ever had that need or needed anything like it?

Table is a normal table:

<table>
    <thead>
        <tr>
            <th>Aluno</th>
            <th>Aula 1</th>
            <th>Aula 2</th>
            <th>Aula 3</th>
            <th>Aula 4</th>
            <th>Aula 5</th>
            <th>Aula 6</th>
            <th>...</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Fábio Rocha</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
        </tr>
    </tbody>
</table>
  • 1

    Can you post your code? I think it’s better to understand

  • Old man, put a fiddle for us to see: http://jsfiddle.net

  • It is a normal table. I updated the question with the code. I just need the FIRST column to be fixed, because I will have around 25 columns.

  • You can use a DIV (position: Absolute) instead of the TH tag.

  • I cannot break the semantics of the table, @Gabrielsantos. I need to maintain the web standard and accessibility.

2 answers

1


Here is a suggestion:

var posicoes = $('tr > th:first-of-type, tr > td:first-of-type').map(function () {
    return $(this).position()
});
var largura = $('tr > td:first').outerWidth();
$('table').css('margin-left', largura);
$('tr > th:first-of-type, tr > td:first-of-type').each(function (i) {
    this.style.position = 'fixed';
    this.style.left = posicoes[i].left;
    this.style.top = posicoes[i].top;
});

Example

  • This way the first column is fixed on the page. I need to avoid this.

  • @Fábiorocha, you wrote: "I need the first column to stay fixed". You can explain better then what you are looking for?

  • In the example of this plugin it is possible to realize exactly the result I need: http://www.michaelkeck.de/projects/jquery/tinytbl/. Under "Try it", uncheck all options and click "Create".

  • @Fábiorocha, this is what you want? http://jsfiddle.net/y9S8y/

  • The second column (Columna) is behind the fixed column. And you can’t use position:Fixed, because if you scroll vertically the first column is stationary. I’m testing on your code, if it works, put here.

  • Nothing, always gives some kind of break in the layout... the table floats to the left etc.

  • @Fábiorocha, I had only tested in Chrome, I saw after that IE and FF did not work. I think it will not be easy this... but and so is better?: http://jsfiddle.net/UJq6B/

  • Finally!!! I changed a few things in the JS: http://jsfiddle.net/fabiorochafg/nJSvS/2/

  • @Fábiorocha, boa! I think it will still take some work to insert in a real page, but the effect works well without breaking the code of the table! I’ll update the answer.

  • I put it on the real project, that version there was just to show you, hehe. Just missing a small detail to be perfect, but this one leaves for another question, hehehe. Thank you so much, man!

  • @Fábiorocha, optimo! If you think I helped you can mark the answer as right. I’m waiting for the other question :)

Show 6 more comments

0

Would this be?

HTML:

<table width=200 height=200 frameborder=1>
<tr class="fixed">
    <td>a</td>
    <td>b</td>
</tr>
<tr class="fixed">
    <td>c</td>
    <td>d</td>
</tr>    
</table>

CSS:

.fixed td {
  width: 50px;
  height: 20px;
  display: block;
  background-color: green;
  border: 1px solid blue;
}

Here is an Example in Jsfiddle

  • Friend, I updated the question, I think now I can better understand my problem.

  • @Fábiorocha guy really don’t know how to do this, but there’s a scheme <colgroup> which may be what you want, gives a search on it - but by q I saw have to use two <table> and you can’t do it with just one

Browser other questions tagged

You are not signed in. Login or sign up in order to post.