2010-01-09 94 views
1

我使用Tablesorter 2.0.3的幾個問題。Tablesorter 2.0.3 colspan問題

第一個問題是當我在桌子上放置一個colspan。基本上,當我這樣做時,它不會對各個列進行排序。

<table> 
<thead> 
     <th colspan="3"></th> 
     <th>Four</th> 
     <th>Five</th> 
     <th>Six</th> 
     <th>Seven</th> 
</thead> 
<tbody> 
<tr> 
     <td>1</td> 
     <td>2</td> 
     <td>3</td> 
     <td>4</td> 
     <td>5</td> 
     <td>6</td> 
     <td>7</td> 
</tr> 
</tbody> 
</table> 

第二個問題我有是,無論我在tablesorter.js設置sortInitialOrder(「降序」或「ASC」),它沒有任何效果,使升序或降序當它被點擊時。

請任何人都可以幫忙嗎?

回答

3

要解決問題之一,您可以創建自己的標題到數據列的映射。這就是我所做的以我想要的方式獲得功能。

我已經添加下面的方法來tablesorter.js:

function findInMap(map, index, reverse) { 
    for(var i = 0; i < map.length; ++i) { 
     if(map[i][reverse ? 1 : 0] == index) return map[i][reverse ? 0 : 1]; 
    } 

    return index; 
} 

而且還增加了以下內容this.defaults

customMap: [] 

的,更改以下方法:

function setHeadersCss(table,$headers, list, css) { 
    $headers.removeClass(css[0]).removeClass(css[1]); 

    var h = []; 
    $headers.each(function(offset) { 
     if(!this.sortDisabled) { 
      h[this.column] = $(this);     
     } 
    }); 

    var l = list.length; 

    for(var i=0; i < l; i++) { 
     var header = list[i]; 
     var headerIndex = findInMap(table.config.customMap, header[0], true); 
     h[headerIndex].addClass(css[header[1]]); 
    } 
} 

最後但並非最不重要的,添加/更改以下內容$headers.click(function(e) {

地址:

var ia = findInMap(config.customMap, i, false); 

變化: config.sortList.push([i,this.order]);config.sortList.push([ia,this.order]);

當你初始化的的tablesorter傳遞customMap[[header_col_id, data_col_id]]你的情況,這將是:

customMap[ 
    [2, 4], 
    [3, 5], 
    ... 
] 
2

如果您點擊跨越三列的標題,您會發生什麼?你期望它列出哪一列?一般來說,爲了tablesorter的工作,你需要爲每一列有一個頭。

sortInitialOrder似乎沒有提及在tablesorter的網頁上,但它確實存在於代碼中。我以前只使用過這個:

sortList: [[5, 0]] 

最初將使用第5列進行升序排序。

0

我已經有點解決了這個問題,因爲我也只希望表格只能按升序排序,所以我將第536行修改爲this.order = this.count++ % 0;,這樣就可以按我的意願進行排序。

就colspan而言,我不得不求助於使用空白來排序相應的列。這可能不是正確的標記,但我沒有看到解決這個問題的其他方法。

0

我已經解決對於我自己而言,在tablesorter代碼本身中有一個類似的問題:

Index: jquery.tablesorter.js 
=================================================================== 
--- jquery.tablesorter.js (.../original) (revision x) 
+++ jquery.tablesorter.js (.../mine) (revision y) 
@@ -278,7 +278,10 @@ 
        cache.row.push(c); 

        for (var j = 0; j < totalCells; ++j) { 
-      cols.push(parsers[j].format(getElementText(table.config, c[0].cells[j]), table, c[0].cells[j])); 
+      var cSpan = c[0].cells[j].colSpan || 1; 
+      while (cSpan--) { 
+       cols.push(parsers[j].format(getElementText(table.config, c[0].cells[j]), table, c[0].cells[j])); 
+      } 
       } 

雖然我的問題被顛倒過來了,我有2列跨越1個collspanned細胞。這將修復spanned列仍可排序後的所有列。