0

On button click, I want to get the multiple row selected value from the datatables. From my below code, I only get the first row selected value.

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>

How can I fix it?

1
  • It should be table.rows().data() - datatables.net/reference/api/rows().data() to select values from all rows. Your code table.row().data() is selecting single row. Commented Feb 13, 2019 at 6:39

1 Answer 1

4

Try this

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

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

Reference: https://datatables.net/examples/api/select_row.html

To Loop through the data use the following:

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

Sign up to request clarification or add additional context in comments.

2 Comments

when no checkbox is selected data = table.rows('.selected').data(); data.length should return 0 , you can put a condition on that
display alert if length is 0 else run the loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.