使用rowcallback在重绘后显示表中以前选定的行...没有发生任何事情。 我已在控制台中确认我的“选定”数组是正确的,但回调似乎没有做任何事情。
如果行值在该数组中,那么选择将tr的id值存储在其数组中......应该将类应用于它。
$('#sample_1').dataTable({ "processing": true, "serverSide": true, "ajax": "/custom/data-tables/process.php", // set timestamp as initial sorting "aaSorting": [[5,'desc']], // change index column "columnDefs": [ { "targets": [ 0 ], "searchable": false, "sortable": false, "render": function ( data, type, row ) { return '<input type="checkbox" class="checkboxes" value="'+data+'" />'; } } ], "fnDrawCallback": function() { $('#sample_1 .checkboxes, #sample_1 .group-checkable').uniform(); }, "rowCallback": function( row, data, displayIndex ) { if ( $.inArray(data.DT_RowId, selected) !== -1 ) { $(row).addClass('active selected'); } } }); var selected = []; $('#sample_1_wrapper').on('change', 'tbody tr .checkboxes', function () { var id = $(this).parents('tr').attr("id"); console.log('id '+id); var index = $.inArray(id, selected); console.log('index '+index); if ( index === -1 ) { selected.push( id ); } else { selected.splice( index, 1 ); } console.log('selected '+selected); // add checked status and var checked = $(this).is(":checked"); if (checked) { $(this).attr("checked", true); $(this).parents('tr').addClass("active selected"); } else { $(this).attr("checked", false); $(this).parents('tr').removeClass("active selected"); } });更多信息
在if语句返回以下值之前,使用rowCallback中的控制台日志:
的console.log(data.DT_RowId); 的console.log(选择的);
187413 [“187413”]
那么,187413是在选定的数组中,为什么它没有返回正确的响应?
Using rowcallback to show previously selected rows in the table after redraw... nothing is happening. I have confirmed in the console that my 'selected' array is correct, but the callback doesn't seem to be doing anything.
Selected stores the tr's id value in its array... on callback if the rows value is in that array the classes should be applied to it.
$('#sample_1').dataTable({ "processing": true, "serverSide": true, "ajax": "/custom/data-tables/process.php", // set timestamp as initial sorting "aaSorting": [[5,'desc']], // change index column "columnDefs": [ { "targets": [ 0 ], "searchable": false, "sortable": false, "render": function ( data, type, row ) { return '<input type="checkbox" class="checkboxes" value="'+data+'" />'; } } ], "fnDrawCallback": function() { $('#sample_1 .checkboxes, #sample_1 .group-checkable').uniform(); }, "rowCallback": function( row, data, displayIndex ) { if ( $.inArray(data.DT_RowId, selected) !== -1 ) { $(row).addClass('active selected'); } } }); var selected = []; $('#sample_1_wrapper').on('change', 'tbody tr .checkboxes', function () { var id = $(this).parents('tr').attr("id"); console.log('id '+id); var index = $.inArray(id, selected); console.log('index '+index); if ( index === -1 ) { selected.push( id ); } else { selected.splice( index, 1 ); } console.log('selected '+selected); // add checked status and var checked = $(this).is(":checked"); if (checked) { $(this).attr("checked", true); $(this).parents('tr').addClass("active selected"); } else { $(this).attr("checked", false); $(this).parents('tr').removeClass("active selected"); } });More info
Using the console log in the rowCallback before the if statement returns these values :
console.log(data.DT_RowId); console.log(selected);
187413 ["187413"]
So, 187413 is in the selected array so why is it not returning the correct response?
最满意答案
转换为字符串 - if($ .inArray((data.DT_RowId).toString(),selected)> -1){
Had t convert to a string - if ( $.inArray((data.DT_RowId).toString(), selected) > -1 ) {
更多推荐
发布评论