user1829716 user1829716 - 1 month ago 8x
jQuery Question

jquery datatables: Select Extension: How to select first row on init and how to get at least one row selected

I have two questions for jquery datatables :

  1. Is it possible to select the first row automatically when the DataTable is initialized?

  2. At the moment it is possible, that the user deselect an item by clicking on it. I want, that at least one row keeps selected. When the user clicks to the selected row, the selection should not be removed.

This is my initialization of my DataTable:

ar dataTableOption = { "pageLength" : 5,
"pagingType": "simple",
"info": false ,
"searching": false,
"select" : {
style: 'single'
"lengthChange": false,
"columnDefs": [
"targets": [ 0 ],
"visible": false,
"searchable": false

dataTable = $('#dataTable').DataTable(dataTableOption );

Thank you for your help in advance!


@ Harshul Pandav

Thank you for your post. Your proposed solution worked not 100% for me but it helped me a lot for finding a solution for my problems.

To select the first line worked this for me:

dataTable.row(':eq(0)', { page: 'current' }).select();

To get at least one line selected I use the user-select event, to remember my last selected column:

dataTable.on( 'user-select', function ( e, dt, type, indexes ) {
      lastSelectedRow = dt.rows( '.selected' )[0][0];

After this, the click event passed through, if no row is selected anymore, the row with index of last selected row is selected:

 dataTable.on( 'click', 'tr', function (evt) { 
if (dataTable.rows( {selected:true} ).any() == false) {

For finding out the last selected row the deselect (dt.on( 'deselect', function ( e, dt, type, indexes ) ) event can also be used.