2012-01-27 17 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