首页 > 解决方案 > JQuery Datatables 在所有可用页面上获取所有“选中”复选框

问题描述

希望你能帮我找到解决我遇到的这个问题的方法。重点是我有一个视图——带有数据表的 C# 和 JQuery——它绘制了一个包含数千行的数据表。数据表是分页的,它的第一列有一个复选框 dom 对象,用户可以检查所有这些对象,一个或特定的。当用户提交页面时,我只得到了用户访问的最后一页的选中项,而我需要的是数据表中所有可用页面的所有选中行。任何人都知道如何编写返回该代码的代码?

我已经拥有的代码是这个(只返回最后一页检查的行):

var rows_selected = $('input[type="checkbox"]:checked')
        var index = 0;
        rows_selected.each(function () {
            console.log('ListadoActores[' + index + ']'),
            $('<input>', {
                type: 'hidden',
                value: $(this).attr('id').replace('chk', ''),   
                id: $(this).attr('id').replace('chk', ''),
                name: 'ListadoActores[' + index + ']',
                }).appendTo('#GenFileForm');
            index = index + 1;
        });

        $("#GenFileForm").submit();

希望你能帮我解决这个问题。提前致谢。

标签: c#jquerydatatabledatatables

解决方案


正如@freedomn-m 所说,您需要访问 Datatables API 才能获取所有数据。

在提交时,放置一些如下代码:

var table = $('#example').DataTable();
var rows = []; 
table.rows().every( function () {
    var d = this.data(); //this will get the data for the row.
    rows.push(d); 
} );
//You can then POST the rows and work with them 

推荐阅读