【问题标题】:How to move multiple table rows with jQuery Sortable如何使用 jQuery Sortable 移动多个表格行
【发布时间】:2013-07-05 08:33:35
【问题描述】:

我有一张这样的桌子:

<table id="sortable">
  <tr id="d1" class="mainrow"><td class="handle">O</td><td>Item 1</td></tr>
  <tr id="d1b"><td>&nbsp;</td><td>blah</td></tr>
  <tr id="d2" class="mainrow"><td class="handle">O</td><td>Item 2</td></tr>
  <tr id="d2b"><td>&nbsp;</td><td>blah</td></tr>
  <tr id="d3" class="mainrow"><td class="handle">O</td><td>Item 3</td></tr>
  <tr id="d3b"><td>&nbsp;</td><td>blah</td></tr>
  <tr id="d4" class="mainrow"><td class="handle">O</td><td>Item 4</td></tr>
  <tr id="d4b"><td>&nbsp;</td><td>blah</td></tr>
</table>

和以下 javascript 来设置可排序的属性:

$( "#sortable tbody" ).sortable({
  stop: hasChanged,
  handle: '.handle',
  cursor: 'move',
  items: ".mainrow"
});

当我移动奇数行时,我想将奇数行和偶数行保持在一起(偶数行没有手柄,因此无法拾取)。

我的 hasChanged 函数在 drop 发生后移动偶数行,但拖动时视觉效果看起来不对;间隙出现在奇数行下,我希望它出现在偶数行下,因为它会在 hasChanged 函数完成后结束。

有人知道怎么做吗?

【问题讨论】:

    标签: jquery jquery-ui jquery-ui-sortable


    【解决方案1】:

    没关系,想通了:

    $( "#sortable tbody" ).sortable({
      stop: hasChanged,
      over: movePlaceholder,
      handle: '.handle',
      cursor: 'move'
    });
    

    我只是在 movePlaceholder 函数中更改占位符的位置:

    function movePlaceholder(e, ui)
    {
      if (ui.placeholder.prev().attr("id").length == 2)
        ui.placeholder.insertAfter(ui.placeholder.next());
    }
    

    根据要求,hasChanged 函数:

    function hasChanged(e, ui)
    {
        var newPosition = ui.item.index();
        var id = ui.item.attr("id");
    
        // whatever you need to do goes here
    }
    

    【讨论】:

    • 可以添加hasChanged函数吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-08
    • 2010-12-06
    • 2018-11-17
    • 2011-04-16
    相关资源
    最近更新 更多