无法选择数据表中的行

Unable to select rows in dataTable

本文关键字:数据表 选择      更新时间:2023-09-26

背景信息:

我有两个HTML表格。 表"selected_users"表示用户选择的记录,"用户"是可用用户的列表。"Users"是一个jquery dataTable。

当最终用户单击"用户"的记录时,需要发生一些事情:

  1. 突出显示刚刚在数据表"用户"中选择的行。
  2. 将行数据复制到"selected_users"
  3. 在"用户"中搜索具有匹配"pnumber"的任何其他行。
  4. 对于所有匹配的行,将行数据复制到"selected_users"。
  5. 突出显示数据表中的这些匹配行。

什么不起作用

除步骤 5 之外的所有内容都在工作。 具有匹配 pnumber 的其他行不会在数据表中突出显示。 如果您查看代码,您将看到我测试了"selected[]"js 数组的长度以确保大小增加......确实如此。它只是在 GUI 上,该行没有突出显示。

法典:

$(document).ready(function() {
    var selected = [];

$('#users tbody').on('click', 'tr', function () {
var id = this.id;
var tr;
tr=$('<tr/>');
var index = $.inArray(id, selected);
if ( index === -1 ) {
    selected.push( id ); //select/highlight in list of available users.
    // Find td's inside this tr and add to selected_users table
    var tds = $(this).find('td');
    tr.append("<td>" + tds.eq(0).text() + "</td>");
    tr.append("<td>" + tds.eq(1).text() + "</td>");
    tr.append("<td>" + tds.eq(2).text() + "</td>");
    tr.append("<td>" + tds.eq(3).text() + "</td>");
    tr.attr('id', id.substring(4));
    $('#selected_users').append(tr);
    //test start - find and add all other rows with matching pnumber
    var thisTR = $(this);
    thisTR.siblings().filter(function() {
    if ($('td',this).eq(2).text() == $('td', thisTR).eq(2).text() ) {
        add_to_selected_users($(this));
        if (($.inArray(temp, selected)) === -1) {
                alert('need to highlight:' + this.id);
                selected.push(this.id);
                alert(selected.length);
        };
    };
    //test stop
});
} else {
        selected.splice( index, 1 ); //deselect from list of avail users
        //remove matching record from selected_users table.
        var record_id = id.substring(4);
        var rowtodelete = document.getElementById(record_id);
        rowtodelete.parentNode.removeChild(rowtodelete);
}
$(this).toggleClass('selected');
 } ); //end function
 } ); //end document ready

我敢肯定这是我错过的简单事情。任何建议将不胜感激。

我改变了这个:

    if (($.inArray(temp, selected)) === -1) {
            alert('need to highlight:' + this.id);
            selected.push(this.id);
            alert(selected.length);
    };

对此:

          if (($.inArray(this.id, selected)) === -1) {
                    selected.push(this.id);
                    $(this).toggleClass('selected');
            };