Traverse rows in sorted order
Traverse rows in sorted order
Hello,
I have a table where the first column is a checkbox, which enables you to select several rows. With these rows selected you are then able to do certain actions.
However i have troubles figuring out which rows have been selected, they dont seem to correspond to the viewing order.
var checkboxes = document.getElementsByClassName("chk");
for (i = 0; i < checkboxes.length; i++)
{
if(checkboxes[i].checked==true)
{
console.log(i);
}
}
Gives me the indexes of the selected rows in the viewing order, however when i try to delete one of them using this index:
table.rows(i).remove()
another row gets removed. I used this to see if the values corresponded:
var checkboxes = document.getElementsByClassName("chk");
for (i = 0; i < checkboxes.length; i++)
{
if(checkboxes[i].checked==true)
{
console.log(i);
console.log(table.row(i).data());
}
}
they did not
So my question is, how can I use the checkboxes to remove the correct rows?
This question has accepted answers - jump to:
Answers
Use the
selector-modifier
option which you can pass intorows()
:Allan
Thank you for your answer however this does not work for me, it gives me the same result as before
I tried the following code:
This prints the indexes of where the checkboxes have been checked, this seems to work correctly. But when using this index to get a row, it fetches the wrong row
That is correct and expected. When using an integer as the selector, it uses the data index (see
row-selector). The
-type selector modifier` effects the order that the rows are returned in, not how they are selected.For the case above I would suggest you simply the code:
Allan
Thank you so much for this, is greatly appreciated .
One thing i noticed though,
input[type=checkbox]:selected
should be
input[type=checkbox]:checked
Except for that it worked out of the box, again thank you
Yup - good point. Thanks for correcting that!
Allan