简体   繁体   English

数据表-最后一列固定宽度

[英]DataTables - Last column fixed width

The idea is this : 这个想法是这样的:

I have one jQuery that loads the DataTables, and I know that I can set the "aoColumns" : "sWidth" parameter to make one column fixed width and it's working. 我有一个jQuery可以加载DataTables,并且我知道我可以设置“ aoColumns”:“ sWidth”参数以使一列固定宽度并且可以正常工作。

The problem that I have is that I have a lot of tables, variable numbers of columns, and I need the LAST one to be fixed size, no matter what number that column is. 我的问题是我有很多表,列的数量可变,并且无论列数是多少,我都需要最后一个固定大小。 Sometimes can be 3 rd , sometimes can be 8 th , sometimes can be 16 th , does not matter. 有时可以是3rd ,有时可以是8th ,有时可以是16th ,没关系。

Is even possible ? 可能吗?

No, I don't want to call datatable jQuery for each table, no I don't want to modify anything in the structure because it's working very well right now, just maybe add some class or parameters to the Javascript. 不,我不想为每个表调用数据表jQuery,不,我不想修改结构中的任何内容,因为它现在工作得非常好,只需向Javascript添加一些类或参数即可。

Structure your tables like this: 这样构造表:

<table class="tableClass">
    <colgroup>
    <col></col>
    <col></col>
    <col></col>
</colgroup>
<thead>
    <tr>
        <th>Column 1</th>
            <th>Column 2</th>
            <th>Column 3</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td>Cell 1</td>
        <td>Cell 2</td>
            <td>Cell 3</td>
    </tr>
</tbody>    
</table>

That way, your jQuery will just need to do this: 这样,您的jQuery只需要这样做:

$('.tableClass').each(function() {
    var self = this;
$(self).find('col:last').css('background-color', 'red'); 
});

That jQuery will find the last column in each table with that class, and do whatever you want with that last class on each one. 那个jQuery会在该类的每个表中找到最后一列,然后对每个类的最后一个类进行任何操作。 (I just used the background-color to test it out.) (我只是使用背景色对其进行了测试。)

If all you're doing is just setting the width, you might be able to get away with doing $(self).find('th:last') , but the <colgroup> and <col> is the "correct" way to go. 如果您只是设置宽度,则可以使用$(self).find('th:last')来摆脱$(self).find('th:last') ,但是<colgroup><col>是“正确”的方式去。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM