2013-03-10 69 views
2

我想使用tablesorter來排序一列表格單元格中有許多美元符號。美元符號的金額將從一美元到五美元不等,並且應該以1爲最低,5爲最高。注意:我需要這些字符串完全匹配,因爲會有很多美元符號被拋出,所以$$$需要與$$和$區分。Tablesorter排序多個美元符號

這是我目前使用的代碼,當我有美元符號時它不工作,但是當我用「b」和「a」代替它們時,它完美地起作用。我有2個美元符號代表1,因爲我讀到這是代表正則表達式中的美元符號所必需的。我也試過\$。這可能是由於美元符號在HTML表格中的輸入方式造成的嗎?我已經使用標準「$」和「$」,都無法正常工作。

// add parser through the tablesorter addParser method 
    $.tablesorter.addParser({ 
     // set a unique id 
     id: 'pricerange', 
     is: function(s) { 
      // return false so this parser is not auto detected 
      return false; 
     }, 
     format: function(s) { 
      // format your data for normalization 
      return s.toLowerCase().replace(/\bd\b/g,3).replace(/\bc\b/g,2).replace(/\b$$$$\b/g,1).replace(/\b$$\b/g,0); 
     }, 
     // set type, either numeric or text 
     type: 'numeric' 
    }); 

    $(function() { 
     $("myTable").tablesorter({ 
      headers: { 
       3: { 
        sorter:'pricerange' 
       } 
      } 
     }); 
    }); 

回答

1

嘗試

return s.match(/\$/g).length; 
+0

我想這會工作,但遺憾的是它沒有。我也試過:'s.replace(/ \ $/g,0)'和's.replace(/ \ b \ $ \ b/g,0)'和's.replace(/ $$/g,0 )'以防萬一。沒有任何工作,我爲什麼很困惑。 – Tasoli 2013-03-10 10:26:41

+0

它們是真正的「$」符號還是「$」字符實體?他們是連續的(兩者之間沒有空白)?你是否將解析器應用於正確的列? – 2013-03-10 10:42:30

+0

您還需要調整'$(「myTable」)'中的選擇器,例如'$(「#myTable」)'。 – 2013-03-10 10:47:11