Is there any way to programmatically select rows?

Is there any way to programmatically select rows?

bigsipperbigsipper Posts: 31Questions: 2Answers: 0

I see an old post about this
http://www.datatables.net/forums/discussion/5074/programmatically-select-rows

but nothing in the current API. Is there a way to programmatically select rows?
Lets say I have 'Dept No' - when I click on that field I want to 'select' all persons (rows) that match that department number.
Seems like this should be easy... but.
Just changing class to 'DTTT_selected' highlights the row(s), but there must be something internal that needs to be done as well because I can see only the row I clicked on is returned by 'table.fnGetSelected()'

In my implementation, I want to select rows that match the 'Dept No' even if the row is not visible - like its on another page of the table (some of these tables are several hundred rows).

-AC

Replies

  • rsprinklersprinkle Posts: 3Questions: 0Answers: 0

    Ideally you need to do a few things:

    1. Using the API add a class to all rows of the table that match your criteria.
    2. To handle rows that may not exist, add a createdRow or rowDraw callback to mark rows as they are re-rendered.

    I believe this should work even with the scroll plugin.

  • bigsipperbigsipper Posts: 31Questions: 2Answers: 0

    That doesn't seem to work... Yes, I can find all the rows that match my criteria,
    then I call:

    // idx equals a row index that matches my criteria
                      var r = oTable.row( idx );
                         r.to$().addClass( 'DTTT_selected');
                         r.to$().addClass( 'selected');
    
    

    The rows are not highlighted, and fnGetSelected() returns 0;

  • allanallan Posts: 63,480Questions: 1Answers: 10,467 Site admin

    The TableTools fnSelect() method will programmatically select rows (assuming you are using TableTools).

    Allan

  • bigsipperbigsipper Posts: 31Questions: 2Answers: 0

    Hmm... so this?

    var oTable = $('#dept').DataTable();
    var oTT= TableTools.fnGetInstance('dept');
    oTT.fnSelect( oTable[1] );
    

    Should select row 1 .... but doesn't.

  • bigsipperbigsipper Posts: 31Questions: 2Answers: 0

    Ok... so what I am really not clear on, is how to tell fnSelect which row to select.
    This example is close... it finds all matching dept numbers in the table, but the correct rows are not highlighted in the table. I think due to sorting, the table row index 'idx' is not correct.
    Could use a clue on how to make it highlight the correct rows:

    Example:

         function selectdepts( table, dept ){
          var oTable = $('#'+table).DataTable();
          var oTT = TableTools.fnGetInstance(table);
          oTable.rows().indexes().each(
                function( idx ){
                   if ( oTable.row( idx ).data().dept === dept ){
                      oTT.fnSelect( $('#'+table+' tbody tr')[idx] );
                   }
                 }
           );
         }
    
    
  • bigsipperbigsipper Posts: 31Questions: 2Answers: 0

    And... that led me to this WORKING example:

       // Select all rows that match a specific dept
       function selectdepts( table, dept ){
          var oTable = $('#'+table).DataTable();
          var oTT = TableTools.fnGetInstance(table);
          // deselect everything.
          oTable.rows().indexes().each(
                function( idx ){
                   oTT.fnDeselect( oTable.rows(idx).nodes().to$() );
                }
          ); 
          oTable.rows().indexes().each(
                function( idx ){
                   if ( oTable.row( idx ).data().dept === dept ){
                      oTT.fnSelect( oTable.rows(idx).nodes().to$() );
                   }
                }
          ); 
          //var s = oTT.fnGetSelected();
          //console.log( "fngetselected length: " + s.length );
       }
    
    

    Woot!

  • allanallan Posts: 63,480Questions: 1Answers: 10,467 Site admin

    Yup - that :-). The fnSelect function takes the rows to be selected. oTable[1] doesn't work because it isn't a row node!

    Allan

This discussion has been closed.