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

2 Answers 2

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()
Sign up to request clarification or add additional context in comments.

1 Comment

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();
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/

2 Comments

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, 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 :)...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.