2016-02-15 85 views
4

我試圖使用tablesorting插件在表中的數據進行排序,但數據有逗號(,)作爲分隔符所以不正確排序。我認爲它正在把這個數字看作一個字符串。隨着谷歌的幫助下,我已經找到了一些代碼,但這些都沒有爲我工作。這是我到目前爲止所嘗試過的。Tablesorting用逗號分隔符不工作

$(document).ready(function(){ 
    jQuery.tablesorter.addParser({ 
     id: "fancyNumber", 
     is: function(s) { 
     return /^[0-9]?[0-9,\.]*$/.test(s); 
     }, 
     format: function(s) { 
     return jQuery.tablesorter.formatFloat(s.replace(/,/g,'')); 
     }, 
     type: "numeric" 
    }); 
    $("#myTable").tablesorter({ 
     widgets : ['zebra'] 
    });  
}); 

請告訴我,我做錯了。

我已經給<th width="62" class="{sorter: 'fancyNumber'}">column</th>班列也。

+0

編輯的問題。希望這可以幫助。 – Ashish

回答

0

如果設置這樣的類名分揀機:

<th width="62" class="{sorter: 'fancyNumber'}">column</th> 

確保您還加載在metadata addon原因是需要處理該格式。

或者,如果你不想使用該插件,您可以使用headers選項設置解析器:

$(function(){ 
    $('table').tablesorter({ 
    headers : { 
     0 : { sorter: 'fancyNumber' } 
    } 
    }); 
});