Sorting multiple tables with tablesorter

前端 未结 1 1627
深忆病人
深忆病人 2021-01-20 07:18

I\'m using the jQuery tablesorter plugin to allow users to sort tables of data on our site. I recently came across an area where multiple tables using the tablesorter were

1条回答
  •  隐瞒了意图╮
    2021-01-20 07:40

    Try using the "sortEnd" event... I had to add a flag to prevent recursion. Hopefully I added enough comments so it all makes sense (demo).

    $('table').tablesorter();
    
    // using a flag that prevents recursion - repeatedly calling this same function,
    // because it will trigger the "sortEnd" event after sorting the other tables.
    var recursionFlag = false;
    
    // bind to table sort end event on ALL tables
    $("table").bind("sortEnd",function(e, table) {
    
        // ignore if the flag is set
        if (!recursionFlag) {
            recursionFlag = true;
    
            // find other tables to resort (but not the current one)
            // the current sort is stored in "table.config.sortList"
            $('table').not(this).trigger("sorton", [ table.config.sortList ]);
    
            // reset recursion flag after 1 second... so you can't sort faster
            // than that or it won't trigger the other tables
            // you can adjust this value; it all depends on how much data needs
            // to be sorted in the tables. Run the tablesorter with "debug:true"
            // to find out the times needed to sort (but do it in IE as it is
            // the slowest browser)
            setTimeout(function(){ recursionFlag = false; }, 1000);
    
        }
    });
    

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