2012-01-27 78 views
2

我正在使用jquery tablesorter。我喜欢这里的一个表:jquery tablesorter添加标题/工具提示以显示升序/降序

http://tablesorter.com/docs/

除了我想要的列标题到每个具有当他们上空盘旋,用鼠标标题/提示。例如,名字的默认标题是“按名排序”,然后它将变为“按升序排名”或“按降序排名”等等。我怎么能做到这一点,因为图像是在标题图像的CSS,并没有设置在每一个?

table.tablesorter thead tr .headerSortDown {   background-image: url(desc.gif); } 
table.tablesorter thead tr .headerSortUp {   background-image: url(asc.gif); } 
table.tablesorter thead tr .header {   background-image: url(bg.gif);   background- repeat: no-repeat;   background-position: center right;   cursor: pointer; } 

回答

1

这是我会怎么做呢?建立一个数据标题与您要添加的文本的每个标题单元格。但不是说“是”,“升序”或“降序”,我们将使用名为“{}目录”可置换变量方向:

<table class="tablesorter"> 
    <thead> 
     <tr> 
      <th data-title="sort {dir} last name">Last</th> 
      <th data-title="sort {dir} first name">First</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr><td>Smith</td><td>John</td></tr> 
     <tr><td>Jones</td><td>Timothy</td></tr> 
     <tr><td>Wong</td><td>Jin</td></tr> 
     <tr><td>O'Brien</td><td>Shaun</td></tr> 
     <tr><td>Parker</td><td>Peter</td></tr>   
    </tbody> 
</table> 

然后,我们添加的功能要经过每个表头单元格,并根据它找到的类名更新标题。调用它,并确保在每个表排序完成后调用它。

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(); 

这是working demo

+0

你的例子很好用,有可能将所有这些添加到函数setHeadersCss里面的tablesorter.js?它看起来像是tablesorter正在添加和删除类 – 2012-01-28 15:53:53

+0

我不建议修改实际的插件代码,除非你可以维护它。如果将来有一天您决定更新插件,则需要记住您所做的更改以及为使代码重新运行所做的任何修改。 – Mottie 2012-01-28 18:17:42

+0

我感兴趣的只是一个简单的静态' ...'当我使用过滤器小部件时工作。无论如何,它似乎并没有出现在版本2.10.8中。有什么建议么?谢谢。 – Leonid 2014-03-05 05:33:25