Unable to use cells( rowSelector, columnSelector [, modifier ] ) to get selected cell count

Unable to use cells( rowSelector, columnSelector [, modifier ] ) to get selected cell count

kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,951
edited April 2018 in Select

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:

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    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:

    // the permitted ones
    table.cells(2,'.select-me').deselect()
    
    // all cells in the row
    table.cells(2, '').deselect()
    

    Hope that's what you're after!

    Cheers,

    Colin

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    Though I do think there's a bug in there: table.cells(2, [0,1,2,3,4,5], {selected:true}).count() and table.cells(2, '', {selected:true}) both return 0, even when cells are selected on that row... I'll check up on it tomorrow...

  • kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,951

    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:

    var rowCount = dt.cells(row, '.select-me', { selected: true }).count();
    
    if (rowCount === 0) {
      dt.cells(row, '.select-me').select();
    } else {
      dt.cells(row, '.select-me').deselect();
    }
    

    This is just a learning exercise. Aside from the potential bug is there a more elegant way to do this?

    Kevin

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    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 using cells() with three arguments, then the middle one needs to be the column selector.

    C

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    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 to rows() and columns() 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:

    var a = dt.cells(row, '.select-me').indexes();
    var rowCount = dt.cells(indexes, { selected: true }).count();
    

    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

  • kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,951

    I like finding edge case bugs :smile: The issue doesn't impact me, I was just messing around. Your code works with one small correction:

    var indexes = dt.cells(row, '.select-me').indexes();
    var rowCount = dt.cells(indexes, { selected: true }).count();
    

    Changed var a = to var indexes =.

    Kevin

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    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

  • kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,951

    rabbit warren

    Hadn't heard that one before.... learn something new everyday :smile:

    Kevin

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    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.

This discussion has been closed.