我正在尝试使用jQuery的detach()方法删除/添加表行。我宁愿使用这种方法,也不愿使用jQuery的隐藏/显示来保持某些CSS样式。分离功能绑定到一个复选框,该复选框将隐藏任何包含“否”的行。
我遇到的问题是我的表具有可排序的标题,并且如果在使用此功能之前对任何列进行排序,则表行不会放回到正确的位置。
$(document).ready(function () {
var tablerows;
$('#indexTable tr').each(function(i) {
$(this).data('initial-index', i);
});
$('#customSwitch1').change(function () {
if (tablerows) {
$(tablerows).appendTo('#indexTable').each(function(){
var oldIndex = $(this).data('initial-index');
$('#indexTable tr').eq(oldIndex).before(this);
});
tablerows = null;
} else {
tablerows = $('#indexTable tr:contains(No)').detach();
}
});
});
如何将行放回分离前的位置,甚至放回第一次加载表时的位置?