Unable to use cells( rowSelector, columnSelector [, modifier ] ) to get selected cell count
Unable to use cells( rowSelector, columnSelector [, modifier ] ) to get selected cell count
kthorngren
Posts: 21,336Questions: 26Answers: 4,951
I'm using this to select certain cells in the row, which is working:
var cells = dt.cells(row, '.select-me').select();
However I'm not able to get the number of selected cells:
var rowCount = dt.cells(row, '.select-me', { selected: true }).count(); //not working as expected - always returns 0
var classCount = dt.cells(row, '.select-me').count(); //returns 3 for the 3 cells with this class
var totalCount = dt.cells('.select-me', { selected: true }).count(); //returns the total number of cells select - for example: 6 if 2 rows are selected
Not sure if I'm using the combo of all three parameters incorrectly. Here is an example:
http://live.datatables.net/sotemiru/1/edit
Essentially I just want to find out if there are cells selected in the row and if so deselect those cells. Let me know if there is an easier way.
Kevin
This question has accepted answers - jump to:
This discussion has been closed.
Answers
Hey Kevin,
If I understand correctly and you don't care about how many cells are selected, you just want all cells deselected in that row, you could just use:
Hope that's what you're after!
Cheers,
Colin
Though I do think there's a bug in there:
table.cells(2, [0,1,2,3,4,5], {selected:true}).count()
andtable.cells(2, '', {selected:true})
both return 0, even when cells are selected on that row... I'll check up on it tomorrow...Actually what I was looking for is a way to determine if cells in the row are selected to toggle the selection. Something like this:
This is just a learning exercise. Aside from the potential bug is there a more elegant way to do this?
Kevin
Spoke to the man, he agreed my two should work so there is a bug in there!
Your
rowCount
above wouldn't work, since if you're usingcells()
with three arguments, then the middle one needs to be the column selector.C
This is a bit of a cracker that you've found Kevin! What is happening is that when you pass in a row and column selector to
cells()
it does actually do calls torows()
andcolumns()
and takes the intersection.The upshot is that the
{selected:true}
for the cells isn't used - DataTables will instead attempt to get the selected columns and rows, and if just a single cell selected, there is no column or cell selected. The result is what you are seeing - no cells selected.As a workaround - if you use a cell selector it works:
I think the "fix" for this is going to have to be to do basically that internally - get the intersection of the cells from the rows and columns and then pass it through the cell selector if options are passed in. Redundant in the majority of cases but the only way I can see this working.
Allan
I like finding edge case bugs The issue doesn't impact me, I was just messing around. Your code works with one small correction:
Changed
var a =
tovar indexes =
.Kevin
Doh - thanks. That was from my own testing.
I was getting ready to commit the fix for that when I stumbled over a different bug (although a bit of an edge case). This one is a rabbit warren...
Allan
Hadn't heard that one before.... learn something new everyday
Kevin
The rabbit warren has been traversed - this is fixed now - I retested locally so it'll be in nightly builds from today, and the next release.