jquery tablesorter add title/tooltip to show ascending/descending

后端 未结 1 525
隐瞒了意图╮
隐瞒了意图╮ 2021-01-21 18:58

I am using jquery tablesorter. I have a table like the one here:

http://tablesorter.com/docs/

Except I want the column headers to each have a title/tooltip when

1条回答
  •  囚心锁ツ
    2021-01-21 19:36

    This is how I would do it... set up a data-title for each header cell with the text you want to add. But instead of saying "by", "ascending" or "descending", we'll use a replacable variable named "{dir}" for direction:

    Last First
    SmithJohn
    JonesTimothy
    WongJin
    O'BrienShaun
    ParkerPeter

    Then we add a function to go through each table header cell and update the title based on what class name it finds. Call it, and also make sure it is called after each table sort has completed.

    var table = $('table'),
    
        updateTitles = function(){
            var t, $this;
            table.find('thead th').each(function(){
                $this = $(this);
                t = "by";
                if ($this.hasClass('headerSortUp')) {
                    t = "ascending";
                } else if ($this.hasClass('headerSortDown')) {
                    t = "descending";
                }
                $this.attr('title', $this.attr('data-title').replace(/\{dir\}/, t) );
            });
        };
    
    table.tablesorter();
    
    // bind to sort events 
    table.bind("sortEnd",function() { 
        updateTitles(); 
    });
    // set up titles
    updateTitles();
    

    Here is aworking demo.

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