Topic: Applying Sort Option and Disable Select Column

Heather Markman free asked 3 years ago


I am looking to disable sorting in 2 of my 8 columns as well as setting the default sorting column to be column 1 (using numbers 0-7 to count my columns). However, I cannot figure out how to do this. I keep getting errors everytime i try to combined the "order": [[ 1, "asc" ]] and columnDefs: [{ orderable: false, targets: 0 }] and if I add multiple numbers in the "targets" area it also breaks the sorting. Any help on this would be grand. Thanks!


MDBootstrap staff commented 3 years ago

Hi Heather, Does this sort of example work for you? $(document).ready(function() { $('#example').DataTable( { "order": [[ 3, "desc" ]] } ); } );


MDBootstrap staff answered 3 years ago


Hi,

Disabling of sorting a column can be achieved by at least 5 ways I can think of in a minute so I wanted to understand in which type of disabling you are interested. The multiple targets value has to be contained in array syntax like this [0, 5].

$('#dtOrderExample').DataTable({
    "columnDefs": [ {
       "targets": [ 0, 2 ],
       "orderable": false
    } ],
    "order":  [[ 1, "asc"]]
});

The code above will serve your needs. You probably just missed the proper syntax and coma after "columnDefs" assignment. Keep in mind that if you want to set different column definition to other columns you can simply put coma after the object definition {...} and declare another object with different "targets" and your customization definition.

Best Regards, Piotr


Heather Markman free answered 3 years ago


This is incredibly helpful and exactly what I was looking for. Yes I was missing the comma and the brackets. Thank you very much for this help.


Heather Markman free answered 3 years ago


No you didn't. I am unsure why the concept of disabling sort is unfamiliar to you. " turn off sort on column" I mean to disable the sort on a column. This seems like a pretty basic question and I a unsure why it isn't coming across clearly. I will try to be as clear as I can.

I am referring to: https://mdbootstrap.com/docs/jquery/tables/sort/#select-disable. I want to apply this to column's 0 & 5. Then I want to apply: https://mdbootstrap.com/docs/jquery/tables/sort/#sorting-options to column 1.

The problem is when I combined these two, I get errors:

$('#dtOrderExample').DataTable({
    columnDefs: [{
        orderable: false,
        targets: 3
    }]
    "order":  [[ 1, "asc"]]
});

I also get errors if I list them separately this:

$('#dtOrderExample').DataTable({
    columnDefs: [{
        orderable: false,
        targets: 3
    }]
});
$('#dtOrderExample').DataTable({
    "order": [[ 1, "asc" ]]
});

Also, when I try to add multiple values to:

columnDefs: [{
    orderable: false,
    targets: 0,5
}]

I get errors.

I want to disable or turn off sorting for 2 columns and set my default column to column 1 ascending. I don't see how I can make this any clearer.

I need to control both of these and I don't understand why I have to write multiple lines of CSS and add special classes when there is already a solution for this. I want to know how to implement both of these options, at the same time, in the same function.

Thanks.


MDBootstrap staff answered 3 years ago


Hi Heather,

What do you mean by 'turn of sort on column' ? You want to disable the possibility of sorting this column or simply turn off the sorting by default? You can always add styling to the given element:

.no-sort::after { display: none!important; }

.no-sort { 
    pointer-events: none !important;
    cursor: default !important;
}

<th class="no-sort">Heading</th>

If you want to make sorting default on column 1 ascending the line "order": [[1, 'asc']], will do the job.

Did I understand you correctly?

Best Regards, Piotr


Heather Markman free answered 3 years ago


No. Unfortunately this only takes into consideration a default table sort column/direction and doesn’t take into account disabling any columns.

To repeat my issue I need to accomplish 2 objectives, sort default and disabling. When I try and add both these individual solutions, I get errors. I need to 1) turn off sort on column 0, 5 2) set default for column 1, ascending


MDBootstrap staff answered 3 years ago


Hi Heather,

Does this sort of example work for you?

$(document).ready(function() {
     $('#example').DataTable({
        "order": [[ 3, "desc" ]]
    });
});


Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Resolved

Specification of the issue

  • User: Free
  • Premium support: No
  • Technology: MDB jQuery
  • MDB Version: 4.8.11
  • Device: computer
  • Browser: chrome
  • OS: mac
  • Provided sample code: No
  • Provided link: No