I have two tables and can move the table to each other. There is a issue . After all rows remove from table1 to table 2, it cannot move row from table2 to table1. Would someone tell me how to fix it. Thanks in advance.
There is the code:
$(function() {
$('.draggable_tr').draggable({
helper: 'clone',
revert: 'invalid'
});
$('.childgrid tbody').droppable({
drop: function(event, ui) {
var draggableRow = ui.draggable;
var draggableTable = draggableRow.closest('.childgrid');
var droppableTable = $(this).closest('.childgrid');
var droppableRowIndex = $(this).children().index(ui.helper);
var draggableRowIndex = draggableTable.children().index(draggableRow);
if (droppableTable.attr('id') !== draggableTable.attr('id')) {
draggableRow.remove();
if (droppableRowIndex === 0) {
$(this).prepend(draggableRow);
} else if (droppableRowIndex === droppableTable.children().length - 1) {
$(this).append(draggableRow);
} else {
$(this).children().eq(droppableRowIndex).before(draggableRow);
}
} else if (droppableRowIndex !== draggableRowIndex) {
if (droppableRowIndex === 0) {
$(this).prepend(draggableRow);
} else if (droppableRowIndex === droppableTable.children().length - 1) {
$(this).append(draggableRow);
} else if (droppableRowIndex > draggableRowIndex) {
$(this).children().eq(droppableRowIndex).after(draggableRow);
} else {
$(this).children().eq(droppableRowIndex).before(draggableRow);
}
}
re
}
});
<div id="table1" class="bitacoratable">
<table id="table1" class="childgrid">
<tbody>
<tr class="draggable_tr">
<td>1</td>
<td>Student 1</td>
</tr>
<tr class="draggable_tr">
<td>2</td>
<td>Student 2</td>
</tr>
<tr class="draggable_tr">
<td>3</td>
<td>Student 3</td>
</tr>
</tbody>
</table>
</div>
<div id="table2" class="bitacoratable">
<table id="table2" class="childgrid">
<tbody>
<tr class="draggable_tr">
<td>6</td>
<td>Student 6</td>
</tr>
<tr class="draggable_tr">
<td>7</td>
<td>Student 7</td>
</tr>
<tr class="draggable_tr">
<td>8</td>
<td>Student 8</td>
</tr>
</<tbody>
</table>
</div>
First, consider the following.
When all rows are removed from the table body, there is nothing to render. The table and body essentially have a height of 0 and cannot be targeted for Drop.
As suggested, you need to allow it to be rendered in some way.
Here you can see a Placeholder is created and items are re-initialized so they can be dragged after being dropped.