无法在dataTable

时间:2015-06-15 12:44:19

标签: javascript jquery datatable

背景资料:

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

当最终用户点击“用户”的记录时,需要做一些事情:

  1. 突出显示刚刚在dataTable“users”中选择的行。
  2. 将行数据复制到“selected_users”
  3. 在“用户”中搜索具有匹配“pnumber”的任何其他行。
  4. 对于所有匹配的行,请将行数据复制到“selected_users”。
  5. 突出显示dataTable中的这些匹配行。
  6. 什么不起作用

    除了第5步之外的所有内容都有效。具有匹配的pnumber的其他行未在dataTable中突出显示。如果你查看代码,你会看到我测试“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
    

    我确信这是我错过的简单事。 任何建议将不胜感激。

1 个答案:

答案 0 :(得分:0)

我改变了这个:

    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');
            };
相关问题