2012-02-16 63 views
5

我有一個表格,列中的數字大約在-10到10之間,有些空列。jquery tablesorter排序空表格單元格last

我希望tablesorter總是把空列放在表的底部。即使我排序上升或下降。

像這樣:

-1 
0 
2 
3 
<empty cell> 
<empty cell> 

或像這樣:

3 
2 
0 
-1 
<empty cell> 
<empty cell> 

請注意,我的問題是相同的,只是我想在底部空單元格都排列順序時下降的問題4792426和上升。

刪除行不是一個選項。

回答

7

我找到了解決我的問題的方法。

ssell提供的解決方案不起作用。表數據只被解析一次,所以你不能使用全局變量來控制它。不錯,儘管嘗試! :-)

這是需要的代碼。如您所見,我將formatFloat和formatInt函數過載,以使它們返回null而不是默認值:。 tablesorter裏面的排序功能對我來說很神奇。

 $(document).ready(function() { 
     $.tablesorter.formatInt = function (s) { 
      var i = parseInt(s); 
      return (isNaN(i)) ? null : i; 
     }; 
     $.tablesorter.formatFloat = function (s) { 
      var i = parseFloat(s); 
      return (isNaN(i)) ? null : i; 
     }; 
     $("#table").tablesorter(); 
    }); 

據我所知我的解決方案是無證的,但它工作正常。

+0

很高興它的工作的!刪除我的答案,因爲它不起作用。 – ssell 2012-02-17 17:21:01

+3

只是你知道,我已經在github上創建了一個[tablesorter](http://mottie.github.com/tablesorter/docs/)的副本,並且在最新版本中,它會自動將空表格單元排列到底部 - [演示](http://jsfiddle.net/Mottie/Z9wdn/13/) – Mottie 2012-02-20 02:25:59

2

使用更高版本的tablesorter jQuery插件,您可以在標題中添加不同類型的分揀機,以自動將空/空或字符串(例如,如果輸入「 - 」代替null)您想要的位置(頂部/底部)。

<th class="{'sorter': 'digit', 'string': 'bottom'}">Count</th> 

這是一種將它直接嵌入到html/templates中的方法。但是如果你的列數一致,你可以在最初的tablesorter函數中完成。

$('table').tablesorter(
    headers: { 
     1: { sorter: "digit", empty : "top" }, 
     2: { sorter: "digit", string: "bottom"} 
    } 
) 

無論哪種方式工作得很好,但它基本上不管你把作爲第二K勢力,頭對象訴優先於以正常的方式進行排序。

更多信息可在the tablesorter docs找到。

0

如果它可以幫助任何人,這裏是我如何做文本。

function emptyAtBottomTextSorter(a, b, direction, column, table) { 
    if (a == "") { 
     a = direction ? "ZZZZZ" : ""; 
    } 
    if (b == "") { 
     b = direction ? "ZZZZZ" : ""; 
    } 
    return ((a < b) ? -1 : ((a > b) ? 1 : 0)); 
} 

$("#myTable").tablesorter({ 
    headers: { 
     1 : {'sorter' : 'text'} 
    }, 
    textSorter: { 
     1 : emptyAtBottomTextSorter 
    } 
}); 

擔任的tablesorter v2.26.2