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

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

StefanBStefanB Posts: 2Questions: 2Answers: 0

Hello,

I'm new to DataTables and have two questions.

  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:

`var 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!

Answers

  • allanallan Posts: 63,230Questions: 1Answers: 10,416 Site admin

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

    dataTable.row(':eq(0)').select(); (row().select()).

    I want, that at least one row keeps selected.

    Not with Select's built in selection controls. However, you have two options:

    1. You could listen for deselect and if you find no rows are selected (dataTable.rows({selected:true}).any()), then reselect that row.
    2. Use your own click event listener and the API methods to select rows, rather than the built in options.

    Allan

This discussion has been closed.