在单击按钮时,我想从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>





请帮忙

最佳答案

尝试这个

    $('#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 );
    } );

10-07 17:28