2012-11-19 20 views
0

我有一個價格字段和總金額字段: 價格字段的值爲0.9999900或0.9000000總金額可以是10,000.00或1,000,000.00。我正在使用tablesorter庫來排序表。tablesorter格式化數字之間用逗號和數字之間的問題沒有他們

問題是,如果我設置就像下面(罰款總額排序作品)

s=s.replace(new RegExp(/[^0-9\/A-Za-z ]/g),""); 

如果我設置就像下面(價格排序工作正常)

s=s.replace(new RegExp(/[^0-9\/A-Za-z. ]/g),""); 

,但我不能同時獲得在同一時間到工作..我在想什麼:

ts.addParser({ 
    id: "digit", 
    is: function(s,table) { 
     var c = table.config; 
     s=s.replace(new RegExp(/[^0-9\/A-Za-z ]/g),""); 
     return $.tablesorter.isDigit(s,c); 
    }, 
    format: function(s) { 
     return $.tablesorter.formatFloat(s.replace(new RegExp(/[^0-9\/A-Za-z ]/g),"")); 
    }, 
    type: "numeric" 
}); 

回答

1

我找到了工作,醃肉nd不同的列:

1)我讓兩列指向tablesorter中的不同功能,這樣我可以使價格和總金額都以自己的方式工作。

在tablesorter下,我修改了標題,使總金額列和千位爲「總金額」列。

headers: {8: { sorter: 'digit' },13: { sorter: 'thousands' }} 

其中8,13是我的桌子上的列號。

2)對於 「合計金額」 的列編號8中,我使用ID:數字

3)爲 「價格」 列號13中,我使用的自定義解析器ID: 「成千上萬」

的tablesorter包括以下兩種碼

ID: 「數字」 - >存在於庫的tablesorter

ts.addParser({ 
    id: "digit", 
    is: function(s,table) { 
     var c = table.config; 
     s=s.replace(new RegExp(/[^0-9\/A-Za-z ]/g),""); 
     return $.tablesorter.isDigit(s,c); 
    }, 
    format: function(s) { 
     return $.tablesorter.formatFloat(s.replace(new RegExp(/[^0-9\/A-Za-z ]/g),"")); 
    }, 
    type: "numeric" 
}); 
代碼

id:'thousands' - >將自定義表格分類器添加到tablesorter庫中。

ts.addParser({ 
    id: 'thousands', 
    is: function(s) { 
     return false; 
    }, 
    format: function(s) { 
     return s.replace(new RegExp(/[^0-9\/A-Za-z. ]/g),""); 
    }, 
    type: 'numeric' 
}); 

$(function() { 
    $("table").tablesorter({ 
     headers: { 
      6: {//zero-based column index 
       sorter:'thousands' 
      } 
     } 
    }); 
}); 
相關問題