0

我有一些表,例如:

<table class="table table-hover table-striped" id="mytable">
    <thead>
    <tr>
        <th>#</th>
        <th>Table heading 1</th>
        <th>Table heading 2</th>
        <th>Table heading 3</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td>1</td>
        <td>Table cell</td>
        <td>Table cell</td>
        <td>Table cell</td>
    </tr>
    </tbody>
</table>

然后我想制作可排序表的行标题。

$('#mytable thead tr').sortable({
    axis: "x",
    helper: "clone"
}).disableSelection();

问题:

当我开始拖放时,我有 6 个th-s 而不是 4 个:

<tr class="ui-sortable">
    <th>#</th>
    <th style="
        display: none;">Table heading 1</th>
    <th class="ui-sortable-placeholder" 
        style="
            visibility: hidden;"></th>
    <th>Table heading 2</th>
    <th>Table heading 3</th>
    <th style="
            display: table-cell; 
            width: 343px; 
            height: 37px; 
            position: absolute; 
            z-index: 1000; 
            left: 184px;" 
        class="ui-sortable-helper">Table heading 1</th>
</tr>

..所有标记开始变得非常不稳定和不确定:当我将th项目拖到表格上时,我看到所有行的大小都在跳跃。

很明显,这是因为计数th项目(不等于 中的td项目数tr)而发生的。

如何修复这个?

4

1 回答 1

1

每次开始拖动时,它都会创建两个新th元素。一个没有显示,所以它似乎没有任何影响。第二个是拖动原始元素时的占位符。这个新元素的宽度没有设置,所以它会自动调整为列的最大宽度,这似乎是导致它跳来跳去的原因。

为了解决这个问题,我将占位符元素的宽度更改为我们在 start 函数中拖动的元素的宽度。希望这可以帮助

start: function(event, ui){
    $(".ui-sortable-placeholder").css({width: $(ui.item).width()}); 

下面是代码,这是我的小提琴

$(function () {
$('#mytable thead tr').sortable({
    axis: "x",
    helper: "clone",
    start: function(event, ui){
        $(".ui-sortable-placeholder").css({width: $(ui.item).width()});    
    }
}).disableSelection();
});
于 2013-11-14T14:10:21.773 回答