Is it possible to disable/enable column sorting programmatically?

Is it possible to disable/enable column sorting programmatically?

donniedonnie Posts: 6Questions: 0Answers: 0

Hi,

I made a plugin which allows to sort columns of a datatables, by dragging the border of a th element.
I took most of the code from this discussion: http://datatables.net/forums/discussion/2474/column-resizing/p3

I finally have something that works well, except that when I release the mouse button after the drag and drop, a sorting happens on the columns.

I would like to disable the sorting temporarily, so that there is no column sorting after I finish resizing a column.

I tried to find a function in the API, I tried to look in this forum, without success.

Is there a way to achieve it?

Thanks a lot.

Replies

  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin

    You might need to call event.stopProagation() to stop the click event bubbling up to DataTables. If you post an example, that might let us take a look and see what is happening.

    Allan

  • donniedonnie Posts: 6Questions: 0Answers: 0

    I tried but it did not work.

    Like in ColReorder plugin, my plugin binds a "mousedown" and a "mouseup" event on the <th> element.
    When I click on a th element to sort the appropriate column, it seems to me that the sorting happens in the mouseup callback. So I tried to do e.stopPropagation() from my _fnMouseUp() function, without success. Then I also tried to do it from the _fnMouseDown() function, but no success either.

    I cannot post the full example, but here is the idea:

    this._fnMouseListener = function(i, nTh) {
        $(nTh).on('mousedown.ColResize', function(e) {
            e.preventDefault();
            that._fnMouseDown.call(that, e, nTh);
        });
    };
    
    this._fnMouseDown = function(e, nTh) {
        // I tried here, without success
        e.stopPropagation();
    
        $(document).on('mousemove.ColResize', function (e) {
            that._fnMouseMove.call(that, e);
        }).on('mouseup.ColResize', function (e) {
            setTimeout(function () {
                that._fnMouseUp.call(that, e);
            }, 10);
        });
    };
    
    this._fnMouseUp = function(e) {
        // I also tried here, without success
        e.stopPropagation();
    };
    

    Wherever I put this e.stopPropagation(), the column sorting is still performed.

  • donniedonnie Posts: 6Questions: 0Answers: 0

    I don't understand in the ColReorder plugin, why the sorting is not performed when clicking on a column to reorder it?!

    I would need to do the same in my plugin, but cannot figure out how it's done in the ColReorder plugin.

  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin

    It might be this - although i'd need to experiment to be sure (I has been a while since I've worked on that bit of code).

    Allan

  • donniedonnie Posts: 6Questions: 0Answers: 0

    Hello allan,

    I haven't had time to try it yet, but I will soon and I will give a feedback here.

    Thank you!

This discussion has been closed.