Unexpected cell-selector behavior

Unexpected cell-selector behavior

kthorngrenkthorngren Posts: 21,315Questions: 26Answers: 4,948

When using the form of cell( row-selector, cell-selector ) with a classname I expected the cell() API to search the row-selector results for the class. However it seems to only find the class if its defined in the thead. Test Case:
https://live.datatables.net/kosukanu/1/edit

console.log( 'Class name defined in thead th', table.cells( 0, '.name').count() );
console.log( table.cells( 0, '.name').data() );
console.log( 'Class position defined in tbody td', table.cells( 0, '.position').count() );
console.log( table.cells( 0, '.position').data() );
        <thead>
          <tr>
            <th class="name">Name</th>
            <th>Position</th>
            <th>Office</th>
            <th>Age</th>
            <th>Start date</th>
            <th>Salary</th>
          </tr>
        </thead>
....
          <tr>
            <td>Tiger Nixon</td>
            <td class="position">System Architect</td>
            <td>Edinburgh</td>
            <td>61</td>
            <td>2011/04/25</td>
            <td>$3,120</td>
          </tr>

I expected the cell with class position to be found not the cell where the th has the class name.

The [jQuery selector` docs state this:

jQuery instance can be given as a cell selector, with any nodes which are selected by jQuery and match those available in the table's tbody selected.

Kevin

Replies

  • kthorngrenkthorngren Posts: 21,315Questions: 26Answers: 4,948
    edited November 19

    Also using table.cells( 0, 'td') doesn't seem to work but table.cells( 0, 'th') does work. Update test case:
    https://live.datatables.net/bofederi/1/edit

    Using a jQuery object like this does work:

    table.cells( $('td', table.row().node()) )
    

    Kevin

  • allanallan Posts: 63,483Questions: 1Answers: 10,467 Site admin
    table.cells( 0, '.name')
    

    In this form it is is .cells( rowSelector, columnSelector ). Or in other words, from row index 0, find columns which have a name class. The column class selector operates on the header (which also explains why th works but td doesn't).

    If you want to select a cell based on the cell's own class you need to use the cell selector overload:

    table.cells('.position')
    

    If you want a position class cell from a specific row:

    $('.position', table.row(0).node())
    
    // or
    
    table.row(0).node().querySelector('.position')
    

    Does that make sense?

    Allan

  • kthorngrenkthorngren Posts: 21,315Questions: 26Answers: 4,948

    Guess I need to look closer at the docs. As you said its .cells( rowSelector, columnSelector ) not .cells( rowSelector, cellSelector ) :smile:

    Kevin

Sign In or Register to comment.