Add class to first row in initComplete

Add class to first row in initComplete

Ali AdnanAli Adnan Posts: 47Questions: 18Answers: 1

I am using DataTables I want to select First row of my table and add "DTTT_selected" class to first row .

I have successfully select the first row, but when I add class it adds "DTTT_selected" to all Rows instead of first selected row.

The Code is :-

table = $('#resultTable').DataTable({
aaSorting: [],
ajax: {...},
columnDefs: [...],
initComplete: function () {
    console.log('DataTables has finished its initialisation.');
    var api = this.api();
    var CurrentRow = api.row({ order: 'current' }, 0);
    CurrentRow.select(); //Working Fine
    CurrentRow.$('tr').addClass('DTTT_selected'); // Results all Rows get class DTTT_selected
    }
  }
});

Can Some one help me how can I do this ?

Answers

  • kthorngrenkthorngren Posts: 20,457Questions: 26Answers: 4,803

    Doing this should apply the DTTT_selected CSS only to the first row:

          row = $('#resultTable_wrapper tr:first-child');
          $(row).addClass('DTTT_selected');
    

    If the table is filtered or sorted that CSS will move with the row assigned. If you want the first row always to have the CSS then you may need to use the drawCallback instead and add code to remove the CSS from all rows before applying to the first row.

    Kevin

This discussion has been closed.