jQuery tablesorter - loss of functionality after AJAX call

后端 未结 8 1004
庸人自扰
庸人自扰 2021-02-14 06:00

I have recently been experimenting with the tablesorter plugin for jQuery. I have successfully got it up and running in once instance, and am very impressed. However, I have tri

8条回答
  •  情话喂你
    2021-02-14 06:25

    I've come across this problem recently. elwyn's solution didn't work for me. But this topic suggested that the problem is in synchronization of functions on "update" and "sorton" events.

    If you look in the code of jQuery Tablesorter 2.0 (ver. 2.0.5b) source code, you'll see something like:

    $this.bind("update", function () {
        var me = this;
        setTimeout(function () {
            // rebuild parsers.
            me.config.parsers = buildParserCache(me, $headers);
            // rebuild the cache map
            cache = buildCache(me);
        }, 1);
    }).bind("sorton", function (e, list) {
        $(this).trigger("sortStart");
        config.sortList = list;
        // update and store the sortlist
        var sortList = config.sortList;
        // update header count index
        updateHeaderSortCount(this, sortList);
        // set css for headers
        setHeadersCss(this, $headers, sortList, sortCSS);
        // sort the table and append it to the dom
        appendToTable(this, multisort(this, sortList, cache));
    });
    

    So the problem is that setTimeout makes it less probable that update will be finished before sorting. My solution was to implement yet another event "updateSort", so that above code would become:

    var updateFunc = function (me) {
        me.config.parsers = buildParserCache(me, $headers);
        cache = buildCache(me);
    };
    var sortFunc = function (me, e, list) {
        $(me).trigger("sortStart");
        config.sortList = list;
        var sortList = config.sortList;
        updateHeaderSortCount(me, sortList);
        setHeadersCss(me, $headers, sortList, sortCSS);
        appendToTable(me, multisort(me, sortList, cache));
    };
    $this.bind("update", function () {
        var me = this;
        setTimeout(updateFunc(me), 1);
    }).bind("sorton", function(e, list) {
        var me = this;
        sortFunc(me, e, list);
    }).bind("updateSort", function (e, list) {
        var me = this;
        updateFunc(me);
        sortFunc(me, e, list);
    });
    

    After updating data in your table you would have to trigger only this new event:

    var sorting = [[0,0]]; 
    $("#your-table").trigger("updateSort",[sorting]);
    

提交回复
热议问题