如何使用千位分隔符对数字进行排序

时间:2015-07-08 23:19:45

标签: javascript c# asp.net datatables string-formatting

我尝试使用jQuery Datatables plug-in对数字进行排序,但不使用C#字符串数字格式。

我试过了:

decimal value= 12345678.00
value..ToString("#,##.00");
value.ToString("##,##.##");
value.ToString("0,0.00", CultureInfo.InvariantCulture)

但由于逗号没有运气。如果没有逗号可以正常工作,或者所有具有相同计数的数字也可以正常工作,即

  

01,121,372.01

     

02,002,009.22

     

11,222,222,33

如果如下,那么它无法正常工作

  

1,111,111.11

     

222,191.00

     

32,222.00

2 个答案:

答案 0 :(得分:1)

我这样做是为了克服这个问题。

"aoColumnDefs": [ {
                    "aTargets": [3,4,6],
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        var $currencyCell = $(nTd);
                        var commaValue = $currencyCell.text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
                        $currencyCell.text(commaValue);
                    }
                }]

答案 1 :(得分:0)

适用于DataTables 1.10

DataTables 1.10+内置了数字检测和排序功能,无需额外编码。

或者,您可以将columns.type设置为num-fmt以强制使用特定数据类型。

请参阅下面的示例进行演示。



$(document).ready(function() {
  $('#example').dataTable();

});



    

    

    

<!DOCTYPE html>
<html>

<head>
<meta charset="ISO-8859-1">

<link href="//cdn.datatables.net/1.10.7/css/jquery.dataTables.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="//cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js"></script>

  </head>
<body>
<table id="example" class="display" cellspacing="0" width="100%">

  <thead>
    <tr>
      <th>Name</th>
      <th>Position</th>
      <th>Office</th>
      <th>Age</th>
      <th>Start date</th>
      <th>Salary</th>
    </tr>
  </thead>

  <tfoot>
    <tr>
      <th>Name</th>
      <th>Position</th>
      <th>Office</th>
      <th>Age</th>
      <th>Start date</th>
      <th>Salary</th>
    </tr>
  </tfoot>

  <tbody>
    <tr>
      <td>Tiger Nixon</td>
      <td>System Architect</td>
      <td>Edinburgh</td>
      <td>61</td>
      <td>2011/04/25</td>
      <td>111,111.11</td>
    </tr>
    <tr>
      <td>Garrett Winters</td>
      <td>Accountant</td>
      <td>Tokyo</td>
      <td>63</td>
      <td>2011/07/25</td>
      <td>222,191.00</td>
    </tr>
    <tr>
      <td>Ashton Cox</td>
      <td>Junior Technical Author</td>
      <td>San Francisco</td>
      <td>66</td>
      <td>2009/01/12</td>
      <td>32,222.00</td>
    </tr>
  </tbody>
</table>
</body>
</html>
&#13;
&#13;
&#13;

适用于DataTables 1.9

对于较旧的DataTables 1.9,您可以使用Formatted numbers排序插件。

您只需要包含此JS文件://cdn.datatables.net/plug-ins/1.10.7/sorting/formatted-numbers.js并使用以下代码将数据类型设置为格式化数字。

$('#example').dataTable({
   columnDefs: [
     { type: 'formatted-num', targets: 0 }
   ]
});
相关问题