Understanding row-selector by column value
Understanding row-selector by column value
xaudi
Posts: 4Questions: 1Answers: 0
I populate a table with a JSON data like this:
{
'id' : 1,
'name': 'Jhon',
'category_id': 12,
},
{
'id' : 3,
'name': 'Max',
'category_id': 12,
},
{
'id' : 4,
'name': 'Bob',
'category_id': 23,
}
I can select a row this way
table.row({id:3})
But I can't use this for select many rows
table.rows({category_id:12})
This return ALL rows, without filter
category_id:12
I read the doc at https://datatables.net/reference/type/row-selector and this doesnt help.
I found a this comment and was a similar behavior, but I want know if this is overthinking and I'm doing something wrong.
var rowIndexes = []; table.rows( function ( idx, data, node ) { if(data.somefield === somevalue){ rowIndexes.push(idx); } return false; });
I use this.
let rowIndexes = [];
table.rows( function ( idx, data, node ) {
if(data.category_id === 12){
rowIndexes.push(idx);
}
return false;
});
let myrows = table.rows(rowIndexes);
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Try the
row-selector
function example. Replace.data()
with.indexes()
to get the indexes.Kevin
Did yor read the question? Wich
.data()
?Yes! Looks like you want to get the row indexes of rows where
category_id
== 12.Here is the example code in the function docs:
Replace the last line with
.indexes()
like this:If you don't want the indexes the return what you do want. If you just want the rows then do this:
Kevin
ok! Thanks
But the question is unclear, is more like:
Because, I can use
table.row({category_id:12})
but this only return 1 row. logicallyActually, I need data from selected rows for more operations.
You need to use
rows()
, instead ofrow()
, to return more than one row. As far as I know using{category_id:12}
as a row selector isn't going to find the rows based on the data. Did you verify the correct row was returned and not just the first row?The
row-selector
function docs specifically state this:Are you using the Select Extension? If so this example show how to get selected rows:
https://datatables.net/extensions/select/examples/api/get.html
Kevin
Also note the
row-selector
does not take an object, like{category_id:12}
, as a parameter. However aselector-modifier
is an object. What you are passing to therow()
API is aselector-modifier
. Since its not a valid one it is ignored.Effectively
table.row({category_id:12})
will result in the same row astable.row()
which is the first row.Kevin
I'm a 'edited' asshole, you're right.
At some point I worked with an external plugin that added this feature, when I tried it now it's just a fluke.
But now I wrote a function that works how I want.
(I have not tested if it is completely correct.)
Usage