当行具有或不具有 colspan 时,表中的排序不起作用
我有一个显示事件列表的表格,每个事件还有一个最初隐藏的详细信息行,通过单击按钮将变得可见。
标记看起来像这样:
<table>
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
</tr>
</thead>
<tbody>
<tr class="i-am-always-visible">
<td>Event date</td>
<td>Event name</td>
<td>Event location</td>
</tr>
<tr>
<td colspan="3" class="i-am-hidden-by-default">
Contact form
</td>
</tr>
</tbody>
</table>
这是我用于设置可排序表的 JS
$('#tableId').dataTable({
"bPaginate": false,
"bAutoWidth": false
});
没有棘手的部分:我想使用 jQuery 插件 dataTables 使表可排序,但它似乎无法处理带有 colspan 的行我找不到任何可以在初始化时传递给 dataTables 以正确处理这些行的配置选项。
有没有人遇到过同样的问题并解决了它,或者可以向我指出有关此问题的正确信息?
提前致谢!
I have a table that shows a list of events, each event also has a detail row that is initially hidden and will become visible by clicking a button.
The markup looks like this:
<table>
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
</tr>
</thead>
<tbody>
<tr class="i-am-always-visible">
<td>Event date</td>
<td>Event name</td>
<td>Event location</td>
</tr>
<tr>
<td colspan="3" class="i-am-hidden-by-default">
Contact form
</td>
</tr>
</tbody>
</table>
This is the JS I use for setting up my sortable table
$('#tableId').dataTable({
"bPaginate": false,
"bAutoWidth": false
});
No the tricky part comes in: I want to make the table sortable with the jQuery plugin dataTables and it seems like it can't handle rows with a colspan and I couldn't find any configuration option I could pass to dataTables on initialization to handle these rows properly.
Has anyone came across the same issue and solved it or can point me to the right place information regarding this issue is available?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,我在网上找到的任何库/脚本似乎都不可能做到这一点。看来这是一项相当艰巨的任务。我最终使用 knockout.js 来实现我需要的结果。它对 SEO 非常不友好,因此在使用它之前请三思;-) 在我的情况下,这是不需要的。
如果有人想做同样的事情,这里有一些基本的 Knockout.js 代码:
视图
它是如何工作的?
通常,对 HTML 表进行排序的脚本会对 HTML 节点进行排序,因为它们对数据源一无所知。使用knockout.js,您可以对数据源进行排序,然后它会负责重新绘制表行,无论您有多少列:-)
希望它也对其他人有帮助!
Actually it seems that this is not possible with any library/script I found on the web. Seems like it's a pretty hard task. I ended up using knockout.js to achieve the result I need. It's very SEO unfriendly, so think about twice before using it ;-) In my case this was not needed.
If anyone wants to do the same thing, here's some basic knockout.js code:
The view
How does it work?
Usually the scripts that sort HTML tables sort the HTML nodes as they don't know anything about the data source. With knockout.js you sort the data source and it then takes care of redrawing the table rows, no matter how much colspans you have :-)
Hope it also helps somebody else!