Disable automatic sorting on the first column when using jQuery DataTables

前端 未结 7 2149
遥遥无期
遥遥无期 2021-01-30 06:16

I\'m using jQuery DataTables and I would like to know if there\'s possible to disable automatic sorting on the first column of the table?

My code looks like this:

<
相关标签:
7条回答
  • 2021-01-30 06:43
    this.dtOptions = {
        order: [],
        columnDefs: [ {
            'targets': [0], /* column index [0,1,2,3]*/
            'orderable': false, /* true or false */
        }],
        ........ rest all stuff .....
    }
    

    The above worked fine for me.

    (I am using Angular version 7, angular-datatables version 6.0.0 and bootstrap version 4)

    0 讨论(0)
  • 2021-01-30 06:44
    var table;
    
    $(document).ready(function() {
    
        //datatables
        table = $('#userTable').DataTable({ 
    
            "processing": true, //Feature control the processing indicator.
            "serverSide": true, //Feature control DataTables' server-side processing mode.
            "order": [], //Initial no order.
             "aaSorting": [],
            // Load data for the table's content from an Ajax source
            "ajax": {
                "url": "<?php echo base_url().'admin/ajax_list';?>",
                "type": "POST"
            },
    
            //Set column definition initialisation properties.
            "columnDefs": [
            { 
                "targets": [ ], //first column / numbering column
                "orderable": false, //set not orderable
            },
            ],
    
        });
    
    });
    

    set

    "targets": [0]
    

    to

     "targets": [ ]
    
    0 讨论(0)
  • 2021-01-30 06:44

    Use this simple code for DataTables custom sorting. Its 100% work

    <script>
        $(document).ready(function() {
            $('#myTable').DataTable( {
                "order": [[ 0, "desc" ]] // "0" means First column and "desc" is order type; 
            } );
        } );
    </script>
    


    See in Datatables website

    https://datatables.net/examples/basic_init/table_sorting.html

    0 讨论(0)
  • 2021-01-30 06:45

    If any of other solution doesn't fix it, try to override the styles to hide the sort togglers:

    .sorting_asc:after, .sorting_desc:after {
      content: "";
    }
    
    0 讨论(0)
  • 2021-01-30 06:50

    Add

    "aaSorting": []
    

    And check if default value is not null only set sortable column then

    if ($('#table').DataTable().order().length == 1) {
        d.SortColumn = $('#table').DataTable().order()[0][0];
        d.SortOrder = $('#table').DataTable().order()[0][1];
    }
    
    0 讨论(0)
  • 2021-01-30 06:57

    Set the aaSorting option to an empty array. It will disable initial sorting, whilst still allowing manual sorting when you click on a column.

    "aaSorting": []
    

    The aaSorting array should contain an array for each column to be sorted initially containing the column's index and a direction string ('asc' or 'desc').

    0 讨论(0)
提交回复
热议问题