在jQueryUI可排序的表之间移动一列,包括th

move a column ,including th, between tables in jQueryUI sortable

本文关键字:一列 th 包括 移动 jQueryUI 排序 之间      更新时间:2023-09-26

Fiddle示例

我有两个示例表,第一个单元格中有主题标题。

<table class='sort connect'>
    <thead>
        <tr>
            <th class='ui-state-disabled'></th>
            <th>Person 1</th>
            <th>Person 2</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td class='ui-state-disabled'>Age</td>
            <td>18</td>
            <td>23</td>
        </tr>
         <tr>
            <td class='ui-state-disabled'>Job</td>
            <td>Clerk</td>
            <td>Policeman</td>
        </tr>
    </tbody>
</table>

<table class='sort connect'>
    <thead>
        <tr>
            <th class='ui-state-disabled'></th>
            <th>Person 3</th>
            <th>Person 4</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td class='ui-state-disabled'>Age</td>
            <td>17</td>
            <td>46</td>
        </tr>
         <tr>
            <td class='ui-state-disabled'>Job</td>
            <td>Student</td>
            <td>Firefighter</td>
        </tr>
    </tbody>
</table>

我已经让thtd的第一个孩子无法启动,因为它们是标题。是否有任何方法可以使用jQueryUI sortable将其他列(一次一列(td:nth-child,th:nth-child))移动到另一个表?如何在changestart事件中使整列可排序?

这是我的预期输出:

<table class='sort connect'>
    <thead>
        <tr>
            <th class='ui-state-disabled'></th>
            <th>Person 1</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td class='ui-state-disabled'>Age</td>
            <td>18</td>
        </tr>
         <tr>
            <td class='ui-state-disabled'>Job</td>
            <td>Clerk</td>
        </tr>
    </tbody>
</table>

<table class='sort connect'>
    <thead>
        <tr>
            <th class='ui-state-disabled'></th>
            <th>Person 3</th>
            <th>Person 2</th> // sorted
            <th>Person 4</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td class='ui-state-disabled'>Age</td>
            <td>17</td>
            <td>23</td> //sorted
            <td>46</td>
        </tr>
         <tr>
            <td class='ui-state-disabled'>Job</td>
            <td>Student</td>
            <td>Policeman</td> //sorted
            <td>Firefighter</td>
        </tr>
    </tbody>
</table>

JS代码:

var fixHelperModified = function(e, tr) {
    var $originals = tr.children();
    var $helper = tr.clone();
    $helper.children().each(function(index)
    {
      $(this).width($originals.eq(index).width())
    });
    return $helper;
};
$(function() {
    $( ".sort" ).sortable({
    change: function( event, ui ) {
      var see = ui.item.index();
          console.log(see);
      $(this).find('td:nth-child(see),th:nth-child(see)')
    },
     helper: fixHelperModified, 
     cancel: ".ui-state-disabled",
     connectWith: ".connect"
    }).disableSelection();
  });

这样的东西怎么样?

这是一个解决你所问问题的方法,但它做的基本上是一样的,只是按照你想要的来修复样式、空间等

HTML

<div class="sortableContainer sort connect">
    <div>
        <table>
            <thead>
                <tr>
                    <td height="20px"></td>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Age</td>
                </tr>
                <tr>
                    <td>Job</td>
                </tr>
            </tbody>
        </table>
    </div>
    <div>
        <table>
            <thead>
                <tr>
                    <td>Person 1</td>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>18</td>
                </tr>
                <tr>
                    <td>Clerk</td>
                </tr>
            </tbody>
        </table>
    </div>
    <div>
        <table>
            <thead>
                <tr>
                    <td>Person 2</td>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>23</td>
                </tr>
                <tr>
                    <td>Policeman</td>
                </tr>
            </tbody>
        </table>
    </div>
</div>
<div class="sortableContainer sort connect">
    <div>
        <table>
            <thead>
                <tr>
                    <td height="20px"></td>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Age</td>
                </tr>
                <tr>
                    <td>Job</td>
                </tr>
            </tbody>
        </table>
    </div>
    <div>
        <table>
            <thead>
                <tr>
                    <td>Person 3</td>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>17</td>
                </tr>
                <tr>
                    <td>Student</td>
                </tr>
            </tbody>
        </table>
    </div>
    <div>
        <table>
            <thead>
                <tr>
                    <td>Person 4</td>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>46</td>
                </tr>
                <tr>
                    <td>Firefighter</td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

CSS

td, th {
    border:1px solid #222
}
.red {
    background:red
}
.ui-state-disabled {
    opacity:1
}
.sortableContainer>div {
    display:inline-block;
}
table {
    border-spacing:0px;
    border-collapse:collapse;
}

JS

$(function () {
    $(".sort").sortable({
        connectWith: ".connect"
    }).disableSelection();
});