jQuery 可对多个 tbody 进行排序

标签 jquery jquery-ui html-table jquery-ui-sortable tablerow

我有一个包含多个tbody的表。如何为每个tbody制作 header ?这是要怎么做呢?我使用 jquery ui 可排序。如何确定每个 tbody 的 header 不可排序?现在我正在触发排序 $("#sortable > tbody").sortable({});.

<table>
    <thead>
        <tr>
            <th>Col1</th>
            <th>Quantity</th>
        <tr>
    </thead>
    <tbody>
        <tr>
            <th scope="rowgroup">Col1</th>
            <th>100</th>
        <tr>
        <tr>
            <td>Col1</td>
            <td>20</td>
        <tr>
        <tr>
            <td>Col1</td>
            <td>30</td>
        <tr>
        <tr>
            <td>Col1</td>
            <td>50</td>
        <tr>
    </tbody>
    <tbody>
        <tr>
            <th scope="rowgroup">Col1</th>
            <th>100</th>
        <tr>
        <tr>
            <td>Col1</td>
            <td>20</td>
        <tr>
        <tr>
            <td>Col1</td>
            <td>30</td>
        <tr>
        <tr>
            <td>Col1</td>
            <td>50</td>
        <tr>
    </tbody>
</table>

最佳答案

你的结构很有趣。

我假设您希望这些行只能在它们自己的 <tbody> 内排序分别排除包含 <th> 的行元素

幸运的是,jQuery 有 :has()

$("#sortable > tbody").sortable({
    items: 'tr:has(td)'
});

tr:not(:has(th)) ,但似乎效率较低(只是猜测)

演示:http://jsfiddle.net/terryyounghk/Bs2jV/

引用:http://api.jquery.com/has-selector/

此外,您还可以添加connectWith: 'tbody' ,以防万一您想要行 droppable至任何<tbody>在您的表格中。

如:

$("#sortable > tbody").sortable({
    items: 'tr:has(td)',
    connectWith: 'tbody'
});

请评论您想要的结果。谢谢。

关于jQuery 可对多个 tbody 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16151800/

相关文章:

javascript - 使用指令将 jQueryUI 主题应用到带有 Angularjs 的按钮时出现 TypeError

php - jQuery UI TABS + AJAX + PHP 动态内容加载

html - 两个外部 CSS ID,但只有第一个有效

html - td 中的居中复选框(Bootstrap)

javascript - ajax 无法在 codeigniter 中工作,无法在 Controller 中发布或启动该方法

javascript - 输入选择的 jquery 历史记录(简单)

jquery - 在 jquery 中使用固定标题调整表列的大小

javascript - 如何使用强标签使字符串中的一个单词加粗

javascript - 内联(不是内联 block )行为元素的最小宽度

html - 使用 CSS 创建具有固定宽度列的 HTML 表格