【发布时间】:2020-02-06 09:23:38
【问题描述】:
我有一张包含parent 和child 的表格,如下所示。 parent 是可排序的。当我尝试对任何父级进行排序时,我必须将其删除为下一个 child 并将其插入到新位置之后。
<table>
<tr class="parent"><td>Parent1</td></tr>
<tr class="child nosort"><td>Child1</td></tr>
<tr class="parent"><td>Parent2</td></tr>
<tr class="child nosort"><td>Child2</td></tr>
<tr class="parent"><td>Parent3</td></tr>
<tr class="child nosort"><td>Child3</td></tr>
</table>
所以如果我将 Parent1 移到 Parent2 之后,它会是这样的:
<table>
<tr class="parent"><td>Parent2</td></tr>
<tr class="child nosort"><td>Child2</td></tr>
<tr class="parent"><td>Parent1</td></tr>
<tr class="child nosort"><td>Child1</td></tr>
<tr class="parent"><td>Parent3</td></tr>
<tr class="child nosort"><td>Child3</td></tr>
</table>
我已经在sortupdate 之后插入了子行,但无法弄清楚如何在排序之前删除子行。下面如果代码写在start上。
$('table').sortable({
items: 'tr:not(.nosort)',
start: function( event, ui ) {
// Removal code of child before sort
var objChild = ui.item.next();
if( objChild.hasClass('child') ) {
objChild.remove();
}
},
update: function( event, ui ) {
// insertion code of child
}
});
【问题讨论】:
-
你为什么不在你的
<td>中使用两个<div>?.. -
您可以将“父母”放在另一个父母之前或之后,所以您的问题(我认为您非常清楚)不仅仅是“如何删除或移动 i> it's Child”,以及如何移动与拖动元素无关的子项。 (即:在 Parent2 和 Child2 之间移动 Parent1 ... Child2 应该发生什么?)