识别并比较两个不同表格中的两行

问题描述:

请参阅此jsfiddle
我的设置有以下两个表格:simTable = datatable-simf-simresTable = 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中确定一行的方式有关,因为这对我来说是最大的瓶颈。

那么我如何成功识别和比较两个不同表中的两行?
非常感谢。

+0

小提琴在Chrome中似乎对我很好。 – sjy

虽然小提琴似乎没什么问题,以删除该行更清洁的方法是:

// iterate over rows, not all cells 
$('#datatable-simf-res tr') 
    .filter(function() { 
    // check if the first column contains the unique ID 
    return $(this).find('td:first').text() === uniqueid 
    }) 
    .each(function() { 
    // 'this' is the current DOM element, inside .each() 
    resTable.fnDeleteRow(this) 
    }); 

(该DataTables APIfnDeleteRow可以接受的指标或<tr>元素。)

+0

谢谢。我原来的方法在Firefox中显示不一致的结果,你的工作很好,看起来更干净。谢谢! –