jQuery sortable with multiple tbody

泪湿孤枕 提交于 2019-12-12 12:13:39

问题


I have a table with multiple tbody. How can I make headers for each tbody? Is this how to do it? I use jquery ui sortable. How can I be sure the header of each tbody isn't sortable? Right now I'm triggering sortable with $("#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>

回答1:


Interesting structure you've got there.

I assume you want the rows sortable only within their own <tbody> respectively, excluding the row(s) that contain <th> elements.

Luckily, jQuery has :has()

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

or tr:not(:has(th)), but seems less efficient (just a guess)

Demo: http://jsfiddle.net/terryyounghk/Bs2jV/

Reference: http://api.jquery.com/has-selector/

Also, you can add connectWith: 'tbody', just in case you want the rows droppable to any <tbody> within your table.

As in:

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

Please comment on which result you are after. Thanks.



来源:https://stackoverflow.com/questions/16151800/jquery-sortable-with-multiple-tbody

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!