数据表在表重绘后保存选定状态(datatables saving selected state after table redraw)

编程入门 行业动态 更新时间:2024-10-27 16:24:34
数据表在表重绘后保存选定状态(datatables saving selected state after table redraw)

使用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 ) {

更多推荐

本文发布于:2023-08-07 14:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464686.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据表   状态   datatables   saving   表重绘后

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!