3

我有一张这样的桌子:

<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 函数在拖放发生后移动偶数行,但拖动时视觉效果看起来不对;间隙出现在奇数行下,我希望它出现在偶数行下,因为它会在 hasChanged 函数完成后结束。

有人知道怎么做吗?

4

1 回答 1

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
}
于 2013-07-05T09:09:35.170 回答