我有一个html表,可以使用jquery ui进行排序。然后我添加了一个作为垃圾桶的桌子。出于美观的考虑,我只希望当用户拖动表行时垃圾桶可见。为此,我在活动事件上创建表行,并在停用事件中删除表行。然而,我注意到,它不再删除项目时,它被拖进垃圾桶。我试图通过使用settimeout来解决这个问题,以便在tableow消失之前有一个延迟,但是这不起作用。
我也想知道我可以修复它,以及如何使它下拉而不是立即弹出(示例),并向上移动而不是消失(示例的反向)。
我在这里设置了一个JSfiddle这是表格的html:
<table class="table" id="tableDevices">
<thead>
<tr>
<th>Name</th>
<th>Protocol</th>
<th>Systemcode</th>
<th>Unitcode</th>
<th>State</th>
<th></th>
</tr>
</thead>
<tbody class="ui-sortable">
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr style="display: table-row;"></tr>
<tr class="ui-sortable-handle" style="display: table-row;">
<td class="AllowEdit" contenteditable="true">BBQ</td>
<td class="AllowEdit" contenteditable="true">pollin</td>
<td class="AllowEdit" contenteditable="true">31</td>
<td class="AllowEdit" contenteditable="true">4</td>
<td class="AllowEdit" contenteditable="true">off</td>
<td><span class="ui-icon ui-icon-arrowthick-2-n-s"></span></td></tr>
<tr class="ui-sortable-handle" style="display: table-row;">
<td class="AllowEdit" contenteditable="true">Server</td>
<td class="AllowEdit" contenteditable="true">pollin</td>
<td class="AllowEdit" contenteditable="true">15</td>
<td class="AllowEdit" contenteditable="true">1</td>
<td class="AllowEdit" contenteditable="true">off</td>
<td><span class="ui-icon ui-icon-arrowthick-2-n-s"></span></td></tr>
<tr class="ui-sortable-handle" style="display: table-row;">
<td class="AllowEdit" contenteditable="true">Kitchen</td>
<td class="AllowEdit" contenteditable="true">pollin</td>
<td class="AllowEdit" contenteditable="true">31</td>
<td class="AllowEdit" contenteditable="true">1</td>
<td class="AllowEdit" contenteditable="true">off</td>
<td><span class="ui-icon ui-icon-arrowthick-2-n-s"></span></td>
</tr>
<tr style="display: table-row;"></tr>
</tbody>
</table>
这是我正在运行的javascript
//Code to drag and drop order table
var fixHelperModified = function (e, tr) {
var $originals = tr.children();
var $helper = tr.clone();
$helper.children().each(function (index) {
$(this).width($originals.eq(index).width())
});
return $helper;
};
$("#tableDevices tbody").sortable({
activate: function (event, ui) {
$('#tableDevices > tbody > tr:first').before('<tr><td colspan="6" id="trash" class="trash">Trash</td></tr>');
}
}, {
deactivate: function (event, ui) {
setTimeout(function () {
$("#trash").remove();
}, 500);
}
}, {cancel: '[contenteditable]'}, {connectWith: '#trash'}, {helper: fixHelperModified});
$("#trash").droppable({
accept: "#tableDevices tr",
hoverClass: "ui-state-hover",
drop: function (ev, ui) {
ui.draggable.remove();
}
});
我认为问题与事件触发有关。因为每次都要删除和添加元素,所以每次都必须绑定事件。你可以通过添加inline元素,然后更改css属性来防止这种情况。
JSFiddle
JS
$("#tableDevices tbody").sortable({
activate: function (event, ui) {
$('#trash').css('display', 'table-row');
}
}, {
deactivate: function (event, ui) {
$("#trash").css('display', 'none');
}
CSS #trash {
display: none;
}
HTML (tbody第一行)
<tr id="trash"><td colspan="6" class="trash">Trash</td></tr>
无需重复创建和删除元素,只需隐藏和显示它即可。将该行添加到表的HTML中,在页面加载时隐藏该行,然后使用
activate: function (event, ui) {
$('#trash').show();
}
和
deactivate: function (event, ui) {
$("#trash").hide();
}
JSFiddle .