【问题标题】:table.column(0).checkboxes.selected() returns all rows of datatabletable.column(0).checkboxes.selected() 返回数据表的所有行
【发布时间】:2025-12-07 00:30:01
【问题描述】:

我是数据表的新手,并且已经在数据表的每一行中实现了复选框。为此我关注了this sample

我只想返回选定行的数据,而是返回表单提交操作的所有数据。

这是截图

在上面的截图中,我只选择了第二行和第三行,但是 table.column(0).checkboxes.selected() 返回所有行。

Javascript 代码

  var paramedics;
    table;
    var i = 0;

    $(document).ready(function () {
       table = $("#example").DataTable({
            "ajax": {
                "url": "/Home/GetCaseAllocations",
                "type": "GET",
                "datatype": "json",
             },
             "select": {
                "style": "multi"
            },
            "order": [[1, "asc"]],
            "columns": [
                {
                    "data": null,
                    'targets': 0,
                    'checkboxes': {                        
                        'selectRow': true
                    }
                },
                { "data": "patientId" },
                { "data": "name" },
                { "data": "age" },
                { "data": "zipCode" },
                { "data": "status" },
                { "data": "currentAllocation" },
                {
                    "data": "paramedicsAvailable", width: "200px",
                    render: function (data, type, row) {
                        var $select = $("<select></select>",
                            {
                                id: "allocation" + i
                            });
                        $.each(data, function (k, v) {
                            var $option = $("<option></option>",
                                {
                                    text: v,
                                    value: k
                                });
                            if (row.owner === v) {
                                $option.attr("selected", "selected")
                            }
                            $select.append($option);
                        });
                        return $select.prop("outerHTML");
                        i++;
                    }
                },
            ],
         });

        $("#frm-example").on('submit', function (e) {

            var form = this;

            var rows_selected = table.column(0).checkboxes.selected();

            // Iterate over all selected checkboxes
            $.each(rows_selected, function (index, rowId) {
                // Create a hidden element

                console.log(table.row(index));

                $(form).append(
                    $('<input>')
                        .attr('type', 'hidden')
                        .attr('name', 'id[]')
                        .val(rowId)
                );
            });
        });
    });

【问题讨论】:

    标签: jquery datatables jquery-datatables-checkboxes


    【解决方案1】:

    jQuery DataTables Checkboxes 插件的当前限制之一是包含复选框的列必须具有唯一数据。

    使用columns.data 选项在包含唯一数据的响应中定义数据属性。例如:

    "columns": [
        {
            "data": "patientId",
            'targets': 0,
            'checkboxes': {                        
                'selectRow': true
            }
        },
        // ... skipped ...
    ],
    

    【讨论】:

    • 这解决了我的问题,需要 "data": "uniqe" 来获取数据
    【解决方案2】:

    检查以下

    // Get the selected
    var row = $("#example").column(0).checkboxes.selected();
    // Create variable for the ids array
    var selected_items = [];
    // Loop through to get the selected id
    $.each(row, function(index, rowId){
       //this add selected ID as object into array
       selected_items.push({id:rowId});
    });

    【讨论】: