【问题标题】:How to sort a number with thousands separator如何使用千位分隔符对数字进行排序
【发布时间】:2015-09-27 02:54:38
【问题描述】:

我尝试使用 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

【问题讨论】:

  • 然后试着把字符串变成数字。 C# 对字符串中的数字进行排序没有任何特殊规则,它只是根据字母数字字符对其进行排序。获得准确排序的唯一方法是将其作为数字真正存储在数据表中,然后对其进行排序并在排序后显示字符串值。
  • 是 JQuery 数据表吗?
  • 是的,它是 JQuery 数据表

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


【解决方案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);
                    }
                }]

【讨论】:

    【解决方案2】:

    适用于 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>

    适用于 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 }
       ]
    });
    

    【讨论】:

    • @Juan,我相信有一个困惑。问题是关于 jQuery DataTables 插件,如 cmets 中的 OP 所述,而不是 .NET 类 DataTable。虽然格式化问题也可以在服务器端用 C# 解决,但也有缺点,因为数字仍然需要在客户端格式化。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-15
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 2020-03-14
    相关资源
    最近更新 更多