請參閱此jsfiddle。
我的設置有以下兩個表格:simTable = datatable-simf-sim
和resTable = datatable-simf-res
。識別並比較兩個不同表格中的兩行
我試圖在resTable
中添加一行,每次用戶在simTable
的行中選擇(點擊),這個工作很好。但是,當用戶取消選擇該行時,應將其從simTable
中刪除。
resTable = $('#datatable-simf-res').dataTable({});
simTable = $('#datatable-simf-sim').dataTable({});
$('#datatable-simf-sim tr').click(function() {
if ($(this).hasClass('row_selected')) {
$(this).removeClass('row_selected');
var uniqueid = $(this).closest('tr').children('td:eq(0)').text();
$('#datatable-simf-res tr td').each(function() {
if ($(this).text() === uniqueid) {
var rowindex = $(this).closest('tr').index();
resTable.fnDeleteRow(rowindex);
}
});
} else {
$(this).addClass('row_selected');
var rows = [];
$(this).closest('tr').find("td").each(function (cell, v) {
rows.push($(this).text());
});
resTable.fnAddData(rows);
}
resTable.fnDraw();
});
當你用的jsfiddle玩(只需單擊行一堆倍),你會看到,它會留下行不動,或刪除了錯誤的行。我認爲這與我在resTable
中確定一行的方式有關,因爲這對我來說是最大的瓶頸。
那麼我如何成功識別和比較兩個不同表中的兩行?
非常感謝。
小提琴在Chrome中似乎對我很好。 – sjy