How to Disable(false) sorting of one column from DataTables?

If you want to remove or hide or deleting sorting ascending and descending order arrows or disable sorting ascending and descending order on particular columns in datatables library also set “ordering”: false than you can do it using columnDefs. i can simple disable(datatable sorting false) ordering arrows from table in data tables js. even you are using with server side php, laravel, codeigniter, Angularjs, vue js etc. i can datatable sorting false the default sorting column sorting ascending and descending order like single column, first column, middle, last column, specific each index.

Enable Disable settings jQuery DataTables

in jquery datatable disable sorting on specific columns Example, Datatable library creates very easy pagination, sorting ascending and descending order as well as searching. Datatables by default provide all the function by default such as a ordering, pagination, listing searching for all table column. But if you want to disable ordering, search or visibility for particular column then you can use columnDefs.

here columnDefs support to set parameter allows you to assign particular jquery table options to columns in data tables.

Datatable disable sorting on particular column example

You can see sample code of columnDefs:

DataTables.Settings

'columnDefs': [ {
    'targets': [1,3], /* table column index */
    'orderable': false, /* here set the true or false */

 }]

DataTable sorting Settings

$(document).ready(function(){
   $('#product_list').DataTable({
     'processing': true,
     'serverSide': true,
     'serverMethod': 'POST',
     'ajax': {
       'url':'/ajax/products.php'
     },
     'columns': [
        { data: 'id' }, /* table index = 0 */
        { data: 'pname' }, /* table index = 1 */
        { data: 'pcode' }, /* table index = 2 */
        { data: 'price' }, /* table index = 3 */
        { data: 'desc' } /* table index = 4 */
     ],
     'columnDefs': [ {
        'targets': [1,2], /* table column index */
        'orderable': false, /* true or false */
     }]
   });
});

How to remove sorting option from DataTables?

To remove the sorting option from a DataTable, you can set the “ordering” option to false in the DataTables initialization code:

$(document).ready(function() {
    $('#example').DataTable({
        ordering: false
    });
});

This will prevent users from sorting the table by clicking on the column headers.

DataTable responsive Settings

$(document).ready(function() {
    $('#product_list').DataTable( {
        responsive: true
    } );
} );

Also Read: Server-side Processing DataTables using PHP with MySQL

remove sorting option from DataTables

var oTable = $('#product_list').dataTable( {
    "aoColumnDefs": [
        { "bSortable": false, "aTargets": [ 0, 1, 2, 3 ] }, 
        { "bSearchable": false, "aTargets": [ 0, 1, 2, 3 ] }
    ]
}); 

other Way

$('#example').DataTable({
    "ordering": false
});

How to remove sorting option from DataTables?

To remove the sorting option from DataTables, you can use the ordering option and set it to false. Here is an example:

$(document).ready(function() {
    $('#myTable').DataTable({
        ordering: false  // Disable sorting
    });
});

In this example, we initialize a DataTable on the table with an ID of myTable. We set the ordering option to false to disable sorting. This will remove the sorting arrows from the table headers and make the table unsortable.

If you want to disable sorting for only some of the columns, you can use the columnDefs option to specify which columns should be unsortable. Here is an example:

$(document).ready(function() {
    $('#myTable').DataTable({
        columnDefs: [{
            targets: [2, 3],  // Disable sorting for columns 2 and 3
            orderable: false
        }]
    });
});

In this example, we use the columnDefs option to specify that columns 2 and 3 should be unsortable. We set the targets property to an array of column indices (0-based), and the orderable property to false to disable sorting for those columns. The remaining columns will still be sortable.

Leave a Comment