如何在jQuery DataTable中获取多个复选框检查值

时间:2019-02-13 06:36:25

标签: javascript jquery html checkbox datatables

在按钮上单击,我想从datatables获取多行选择的值。从下面的代码中,我仅获得所选的第一行值。

function AssignVendor() {
    var table = $(assignVendor).DataTable();
    var data = table
            .row({ selected: true })
            .data();
}
<table id="assignVender" class="mp myTable table table-striped table-bordered" cellspacing="0" width="100%" ui-jq="dataTable" ui-options="dataTableOpt">

                        <thead>
                            <tr>
                                <th class="select-checkbox"></th>
                                <th>MP Name</th>
                                <th>MP Code</th>
                                <th>Vendor Name</th>
                                <th>Vendor Code</th>
                                <th>From Date</th>
                                <th>To Date</th>
                                <th>Remarks</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr ng-repeat="mp in MaintenanceZones">
                                <td></td>
                                <td>{{mp.MP_NAME}}</td>
                                <td>{{mp.MP_CODE}}</td>
                                <td>{{mp.REMARK}}</td>
                                <td>{{mp.VENDORNAME}}</td>
                                <td>{{mp.VENDORCODE}}</td>
                                <td>{{mp.VFRDATE}}</td>
                                <td>{{mp.VTODATE}}</td>

                            </tr>

                        </tbody>
                    </table>

请帮助

1 个答案:

答案 0 :(得分:2)

尝试

    $('#assignVender').on( 'click', 'tr', function () {
        $(this).toggleClass('selected');
    } );

function AssignVendor() {
    var table = $(assignVendor).DataTable();
    var data = table.rows('.selected').data();
}

参考:https://datatables.net/examples/api/select_row.html

要遍历数据,请执行以下操作:

data = table.rows('.selected').data();
data.each( function ( value, index ) {
        console.log( 'Data in index: '+index+' is: '+value );
    } );