【问题标题】:jquery-ui sortable: how not to add cells on dragging?jquery-ui sortable:如何在拖动时不添加单元格?
【发布时间】:2013-11-27 13:16:30
【问题描述】:

我有一些桌子,例如:

<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 项目(不等于 tr 中的 td 项目数)。

如何修复?

【问题讨论】:

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


【解决方案1】:

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

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

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

下面是代码,这是我的fiddle

$(function () {
$('#mytable thead tr').sortable({
    axis: "x",
    helper: "clone",
    start: function(event, ui){
        $(".ui-sortable-placeholder").css({width: $(ui.item).width()});    
    }
}).disableSelection();
});

【讨论】:

  • 这让生活更美好。但仍然不是解决方案:看起来 width() 在项目被拾取后执行,占位符的宽度取决于 tbody 行的内容宽度。例如,第一行变得非常小。顺便说一句,您的 jsfiddle 链接链接到没有任何代码的页面。
  • 呃。在未平的桌子上它运行得很好。但是如果添加到 bootstrap css 的链接,这个拖放操作会非常奇怪。
  • 你能设置一下你所拥有的东西,这样我就可以看到它在做什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-27
  • 1970-01-01
  • 2013-05-29
  • 1970-01-01
  • 1970-01-01
  • 2011-12-30
  • 2017-08-07
相关资源
最近更新 更多