Make datatable row selected

Make datatable row selected

PareshBafnaPareshBafna Posts: 16Questions: 1Answers: 0

Hi,
Is there any way where we can preselect the datatable row according to user?

Replies

  • rf1234rf1234 Posts: 2,806Questions: 85Answers: 406

    Here is an example. You would need to add the logic regarding the respective user. E.g. by using a modified selector: https://datatables.net/reference/api/row()

    table
      .on ('init', function () {        
           table.row(':eq(0)', { page: 'current' }).select();
       });
    
  • PareshBafnaPareshBafna Posts: 16Questions: 1Answers: 0
    edited May 2020

    Is there any way to add a class to the datatable (tbody tr) using id and without click function?
    I made all the rows in the datatable selected using $('#example tbody tr').addClass('selected');
    How can i make a single row selected using id?
    Thanks.

  • rf1234rf1234 Posts: 2,806Questions: 85Answers: 406

    I would use the api and not native jQuery with a Data Table. Here is an unusual example. On "submitSuccess" I determine the id of a selected table row, then deselect it in order to select it again 200ms later.

    if (action === 'edit') {
        //after editing we need to refresh the child tables;
        //which table is displayed depends on the instrument type!!
        var selectedId = '#' + contractTable.row({selected: true}).id();
        contractTable.row(selectedId).deselect();
        //the new select needs to wait for 200 miliseconds - otherwise it does not work
        setTimeout(function () {
            contractTable.row(selectedId).select();
        }, 200);
    }
    

    Please read this https://datatables.net/manual/api
    and this https://datatables.net/reference/api/

This discussion has been closed.