how to disable sorting click event on table header - Select2

how to disable sorting click event on table header - Select2

BryanSBryanS Posts: 2Questions: 1Answers: 0

Basically the same issue as here, but with Select2 dropdowns

The solution to add e.stopPropatation() works fine on a standard select, but doesn't work once changing it to Select2

What should I do to prevent sorting when the dropdown is clicked?

initComplete: function () {
this.api().columns(".canFilter").every(function () {

                        var column = this;
                        var select = $('<select class="select2" ><option value=""></option></select>')
                            .on('change', function () {
                                var val = $.fn.dataTable.util.escapeRegex(
                                    .search(val ? '^' + val + '$' : '', true, false)
                            }).on('click', function (e) { e.preventDefault();  e.stopPropagation(); }); <---neither works if select2
               (d, j) {
                            if (d!='')
                            select.append('<option value="' + d + '">' + d + '</option>')

                               dropdownAutoWidth: true,
                                 minimumResultsForSearch: Infinity,

This question has an accepted answers - jump to answer


  • kthorngrenkthorngren Posts: 16,790Questions: 25Answers: 3,979
    Answer ✓

    Maybe the tips in the SO thread will help.

    Another option is to use two header rows; one for sorting and the other for the search inputs. Like this example:

    If you still need help please provide a test case showing the issue.


  • BryanSBryanS Posts: 2Questions: 1Answers: 0

    I'd already looked at the SO thread and no joy.

    I went for the two header rows in the end, which actually looks better anyway. I had to mess about with the example, as we don't filter on all columns, so couldn't use the index, but eventually worked out how to get the column from the header iterator.

    Just need to work out how to fix dropdown widths now :)

Sign In or Register to comment.