Getting rows values as array

Getting rows values as array

jigar311982jigar311982 Posts: 70Questions: 31Answers: 0

Hello,

I am trying to get multiple selected rows values as an array but unfortunately it comes as object only,

    console.log(table.rows({'.selected').data());

     var ids = jQuery.map(table.rows('.selected').data(), function (item) {
        return item[3];
    });

    console.log(ids);

by using this code i am getting

[object, object, object,context:array[1]....]

but i could not get like...

[array, array, array...]

What is wrong with this?

Thanks, if you need test page link let me know.

Answers

  • jigar311982jigar311982 Posts: 70Questions: 31Answers: 0

    I got my answer, thanks

  • dennisotienodennisotieno Posts: 1Questions: 0Answers: 0

    I am having the same problem can someone help please

  • allanallan Posts: 63,382Questions: 1Answers: 10,449 Site admin

    Use the toArray() method to convert a DataTables API instance to an array.

    Allan

  • sirrotvicosirrotvico Posts: 1Questions: 0Answers: 0
    edited July 2017

    theArray=[];
    var myValues= $('#miTabla').DataTable();

                    $('#miTabla tbody').on('click', 'tr', function () {
                        $(this).toggleClass('selected');
                    });
    
    
                    var ids = $.map(myValues.rows('.selected').data(), function (item) {
                        theArray.push(item.yourColumn);
    
                    });
    
  • allanallan Posts: 63,382Questions: 1Answers: 10,449 Site admin

    A slightly easier way:

    var ids = myValues.rows( { selected: true } ).data().pluck( 'yourColumn' ).toArray();
    

    That uses the selector-modifier to get the selected rows and then pluck() to get the data from the specific data point needed.

    Allan

  • sachithasachitha Posts: 1Questions: 0Answers: 0

    Hello,

    I am having same issue but I need is an entire row as array. Is that possible ? anything like table.rows('.selected').data().toArray() ? I am new to DataTables . Please help

  • kthorngrenkthorngren Posts: 21,262Questions: 26Answers: 4,934

    Yes there is: toArray().

    Kevin

This discussion has been closed.