How to set a row as selected based on index.

How to set a row as selected based on index.

zorgulzorgul Posts: 3Questions: 1Answers: 0

Hi!

I don't know how to set the selected state of a row based on an index in the table.

Here is my code:

  function processSelectedType(type) {
        
            var dataTable=  $('#commandtable').DataTable();
        var size = dataTable.columns(0).data().eq(0).length;
         
        
            for(var i=0;i<size;++i) {
                  var rowData = dataTable.rows().data();
                
                  if(rowData.data()[i].command.type===type){
                        dataTable.row(':eq('+i+')').toJQuery().toggleClass('selected');
                  } 
            }
        //  dataTable.redraw();
            
            return false;
    }

Answers

  • zorgulzorgul Posts: 3Questions: 1Answers: 0

    This solved my issue:
    function processSelectedType(type) {

            var dataTable=  $('#commandtable').DataTable();
        var size = dataTable.columns(0).data().eq(0).length;
    
    
            for(var i=0;i<size;++i) {
                  var rowData = dataTable.rows().data();
    
                  if(rowData.data()[i].command.type===type){
                        var id=dataTable.row(':eq('+i+')').node();
                        $(id).toggleClass('selected');
                  } 
            }
    
            return false;
    }
    
  • zorgulzorgul Posts: 3Questions: 1Answers: 0

    My final version:

        function processSelectedType(type) {
                var dataTable=  $('#commandtable').DataTable();
            
                dataTable.rows().every( function ( rowIdx, tableLoop, rowLoop ) {
                      if(this.data().command.type===type){
                            var node=this.node();
                            $(node).toggleClass('selected');
                      } 
                } );
                return false;
        }
    
This discussion has been closed.