如何对仅在数据表中具有数字的列进行排序? [英] How to sort a column having numbers only in datatable?

查看:66
本文介绍了如何对仅在数据表中具有数字的列进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

排序仅在具有整数值的列中不起作用。它按数字比较数字。一个解决方案将不胜感激。定义列数字不起作用。是否需要更改其他内容?为了清楚起见,我在表中添加了一些值。

Sorting does not work in the columns that have only integer values. It compares number by number. A solution for that will be appreciated. Defining the column numeric doesn't work. Does it need to change something else? I have added the few values to the table for clarity.

附件图像按降序排列

HTML:

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

   

 <link href="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.21/css/jquery.dataTables.min.css" rel="stylesheet">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.21/js/jquery.dataTables.min.js"></script>

<table class="table display table-condensed table-striped table-hover locations-table" id="tblLocks">
  <thead>
    <tr>
      <th class="">Serial Number</th>
      <th class="name">Customer Name)</th>
      <th class="">Customer Code)</th>
      <th class="">Manufacture Date)</th>
    </tr>
  </thead>
  <tbody>

    <tr>
      <td>68</td>
      <td>A</td>
      <td>29944</td>
      <td></td>
    </tr>
    <tr>
      <td>98</td>
      <td></td>
      <td>28631003</td>
      <td></td>
    </tr>
   
    <tr>
      <td>88</td>
      <td>Z</td>
      <td>28631001</td>
      <td></td>
    </tr>

    <tr>
      <td>88</td>
      <td>Z</td>
      <td>28631068</td>
      <td></td>
    </tr>

    <tr>
      <td>56</td>
      <td>H</td>
      <td>2865</td>
      <td></td>
    </tr>

    <tr>
      <td>88</td>
      <td>Z</td>
      <td>28631034</td>
      <td></td>
    </tr>

    <tr>
      <td>88</td>
      <td>Z</td>
      <td>28631056</td>
      <td></td>
    </tr>
    
    <tr>
      <td>56</td>
      <td>H</td>
      <td>286</td>
      <td></td>
    </tr>
  
    <tr>
      <td>56</td>
      <td>H</td>
      <td>28</td>
      <td></td>
    </tr>
  
   <tr>
     <td>56</td>
     <td>H</td>
     <td>2811</td>
     <td></td>
    </tr>

</tbody>

</table>

JS: p>

JS:

var table = $('#tblLocks').DataTable({
 responsive: true,
            autoWidth: false,
            serverSide: true,
            processing: true,
            },



    ajax:{
    
url: "@Url.Action("LoadLocks", "Locks")",
                   
                    type: "POST",
                    dataType: "json",
                    dataSrc: function (json) {
                            return json.data["LockCommonViewModels"];
                        }
                    },
    
 

columns: [
                    { data: "SerialNumber", name: "SerialNumber", autoWidth: true },
                    { data: "CustomerName", name: "CustomerName", autoWidth: true },
                    { data: "CustomerCode", name: "CustomerCode", autoWidth: true },
                    { data: "ManufactureDate", name: "ManufactureDate", autoWidth: true },
            ],
    order: [[3, "desc"]]
            });


推荐答案

正如评论中所建议的那样 type 列是一个字符串。但是有一个设置( https://datatables.net/reference/option/columns.type )以覆盖默认解释,您可以在其中解释每一列中的数据类型(默认解释为空)。

As suggested in the comments, this is sorting as if the column type is a string. But there is a setting (https://datatables.net/reference/option/columns.type) to override the default interpretation where you can tell it what type of data is in each column (null for default interpretation).

您可以在第3列上看到不同的排序变体和下面的示例中的4取决于它是以字符串还是数字形式读取数据。如果您对 CustomerCode(数字)列进行排序,则可以按照您的建议进行工作,但是如果您对 CustomerCode(字符串)进行排序列,如您的示例所示进行排序。

You can see the different sorting variations on columns 3 and 4 in the example below depending on if it reads the data as a string or a num. If you sort on the CustomerCode (num) column this works as you are suggesting it should, but if you sort on the CustomerCode (string) column it's sorting as your example shows.

我建议为 CustomerCode 列显式设置此值,然后进行排序

I suggest explicitly setting this value for the CustomerCode column, and sorting should work as expected.

$(document).ready(function() {
  $('#tblLocks').dataTable({
    order: [
      [2, "desc"]
    ],
    "columns": [
      null,
      null,
      {
        "type": "string"
      },
      {
        "type": "num"
      },
      null
    ]
  });
});

<link href="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.21/css/jquery.dataTables.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.21/js/jquery.dataTables.min.js"></script>

<table class="table display table-condensed table-striped table-hover locations-table" id="tblLocks">
  <thead>
    <tr>
      <th class="">Serial Number</th>
      <th class="name">Customer Name</th>
      <th class="">Customer Code (string)</th>
      <th class="">Customer Code (num)</th>
      <th class="">Manufacture Date</th>
    </tr>
  </thead>
  <tbody>

    <tr>
      <td>68</td>
      <td>A</td>
      <td>29944</td>
      <td>29944</td>
      <td></td>
    </tr>
    <tr>
      <td>98</td>
      <td></td>
      <td>28631003</td>
      <td>28631003</td>
      <td></td>
    </tr>

    <tr>
      <td>88</td>
      <td>Z</td>
      <td>28631001</td>
      <td>28631001</td>
      <td></td>
    </tr>

    <tr>
      <td>88</td>
      <td>Z</td>
      <td>28631068</td>
      <td>28631068</td>
      <td></td>
    </tr>

    <tr>
      <td>56</td>
      <td>H</td>
      <td>2865</td>
      <td>2865</td>
      <td></td>
    </tr>

    <tr>
      <td>88</td>
      <td>Z</td>
      <td>28631034</td>
      <td>28631034</td>
      <td></td>
    </tr>

    <tr>
      <td>88</td>
      <td>Z</td>
      <td>28631056</td>
      <td>28631056</td>
      <td></td>
    </tr>

    <tr>
      <td>56</td>
      <td>H</td>
      <td>286</td>
      <td>286</td>
      <td></td>
    </tr>

    <tr>
      <td>56</td>
      <td>H</td>
      <td>28</td>
      <td>28</td>
      <td></td>
    </tr>

    <tr>
      <td>56</td>
      <td>H</td>
      <td>2811</td>
      <td>2811</td>
      <td></td>
    </tr>

  </tbody>

</table>

这篇关于如何对仅在数据表中具有数字的列进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆