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

标签 javascript jquery html jquery-ui jquery-ui-sortable

Fiddle Example

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

<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 的第一个 child 无法排序,因为它们是标题。有什么方法可以使用 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();
  });

最佳答案

this 这样的东西呢? ?

这是您所要求的解决方法,但它基本上做同样的事情,只是根据您的需要修复样式、空格等

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();
});

关于javascript - 在 jQueryUI 可排序表之间移动一列,包括 th,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25522573/

相关文章:

javascript - 如何在不使用长度函数的情况下创建测量字符串长度的长度函数

javascript - JQuery Mobile Listview 滑动菜单

javascript - 没有 jquery 的 Angular

python - 如何使用将服务器链接到在暴露的本地主机上运行的 flask 应用程序

javascript - 如何根据传递给函数的参数更改变量名称?

javascript - 从字符串中的数组中查找单词,仅限整个单词(带有希伯来语字符)

javascript - 如何使用nodegit按日期对标签进行排序?

php - Ajax PHP 插入 MySQL utf-8 字符不起作用

css - 使用 CSS 属性

html - 多次使用 base64 嵌入图像