2

This question is regarding datatables,

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...]
['test','test1']

What is wrong with this? I tried with below question, but i could not get array, jQuery DataTables Getting selected row values

Community
  • 1
  • 1
rjcode
  • 1,193
  • 1
  • 25
  • 56

2 Answers2

5

You can call toArray(), I could not find this in the docs - I got desperate, just tried it and it worked!

table.rows('.selected').data().toArray()
DaafVader
  • 1,735
  • 1
  • 14
  • 14
  • 1
    Thank you, this works great. Hours searching and this was the only correct answer I could find. Using the updated syntax: table.rows({selected:true}).data().toArray(); – Dave Davis May 09 '21 at 22:03
0

Have you considered using dataTables own every() iterator method? Here is an example of multi select, where the selected rows is consoled out as array of array of strings :

table.on('select.dt', function() {
   var array = [];
   table.rows('.selected').every(function(rowIdx) {
      array.push(table.row(rowIdx).data())
   })   
   console.log(array);
})

demo -> http://jsfiddle.net/0yvm41q7/

davidkonrad
  • 83,997
  • 17
  • 205
  • 265
  • When you unselect a checkbox, the row isnt't removed from the array until you carry out the next iteration. Have you a way to avoid this? – Blawless Jun 19 '17 at 15:50
  • @Blawless, there is no checkboxes used what so ever in the above, and `array` exists inside the function scope only. So you have lost me here :)... – davidkonrad Jun 19 '17 at 17:18