2014-10-03 74 views
2

我正在使用tablesorter來排序一組值,其格式因0123'到6.39 million,37.3 million,5.3 million等不同而異。目前,插件排序簡單地忽略了million,否則排序很好,但是因此您得到類似'530萬,639萬,3730萬,710,231,在tablesorter中排序'human'數字

+0

如果tablesorter是一個特定的插件或庫,您應該更加具體,並且可能使其成爲文檔的鏈接。此外,請提出問題。如果你不問問題,你就無法得到答案。 – 2014-10-03 02:08:56

+0

換句話說,tablesorter工作正確,數據混亂。 – 2014-10-03 02:38:49

+0

根據你所說的,它可能只是使用* parseFloat *。您首先需要一個將「人」號碼轉換爲實際號碼的功能(例如630萬至6300000),然後您可以根據需要對其進行排序。 – RobG 2014-10-03 02:39:15

回答

0

您需要添加一個解析器來替換其名稱值(demo):

// see big list here: http://www.unc.edu/~rowlett/units/large.html 
var numbers = { 
    'zero'  : 0, 
    'hundred'  : 100, 
    'thousand' : 1e3, 
    'million'  : 1e6, 
    'billion'  : 1e9, 
    'trillion' : 1e12, 
    'quadrillion' : 1e15, 
    'quintillion' : 1e18, 
    'sextillion' : 1e21, 
    'septillion' : 1e24, 
    'octillion' : 1e27, 
    'nonillion' : 1e30, 
    'decillion' : 1e33 
}; 

$.tablesorter.addParser({ 
    id: "namedNumbers", 
    is: function() { 
     return false; 
    }, 
    format: function (s, table) { 
     var v, 
      result = 1, 
      arry = (s || '').split(/[\-\s]+/), 
      len = arry.length; 
     while (len) { 
      v = $.tablesorter.formatFloat((arry[--len] || '').toLowerCase(), table); 
      if (numbers.hasOwnProperty(v)) { 
       result *= numbers[v]; 
      } else { 
       result *= parseFloat(v); 
      } 
     } 
     return result !== 1 ? result : s; 
    }, 
    type: "numeric" 
}); 

$(function() { 
    $('table').tablesorter({ 
     theme: 'blue', 
     headers : { 
      1 : { sorter : 'namedNumbers' } 
     } 
    }); 
});