Kendo 网格排序不适用于字符和数字

Kendo grid sorting not working with characters and number

在此示例中,我使用了 Kendo 网格和可排序标志。它使用 dateTime 和所有字符对列进行排序。但是 Product name 列有字符和数字,排序不起作用。 ANA9 将始终位于顶部,但 ANA11 应位于顶部。任何帮助都会很棒。

https://dojo.telerik.com/@mcdevittnccn/aDUyicow

日期字段

    var datea = a.publishedDate.split('/');
    var newDatea = datea[1] + '/' + datea[0] + '/' + datea[2];
    var dateb = b.publishedDate.split('/');
    var newDateb = dateb[1] + '/' + dateb[0] + '/' + dateb[2];
    
   if (newDatea === newDateb) { return 0; }
   if (newDatea > newDateb) { return 1; }
   return -1;

您需要为 ProductName 列实现一个比较函数。此函数从您的数据源接收两个项目,您 return 如果您认为它们相等,则为 0;如果您认为第一个项目的排序应高于第二个,则为 1;如果排序较低,则为 -1。

这为您的 dojo 中的数据实现了您想要的结果:

field: "ProductName",
title: "Product Name",
sortable: {
    initialDirection: "asc",
    compare: function(a, b) {
        var inta = parseInt(a.ProductName.substring(3));
        var intb = parseInt(b.ProductName.substring(3));
        if (inta === intb) { return 0; }
        if (inta > intb) { return 1; }
        return -1;
     }
},
width: 300

这里我简单假设产品名称是3个字符后跟一个数字,并且是决定顺序的数字。因此我从字符串中提取数字,转换为 int 然后进行比较。

如果数字之前的字符数可以变化and/or您需要先按字符部分排序,然后再按数字部分排序,您的比较功能显然需要比我的更复杂。

查看 API 位于:https://docs.telerik.com/kendo-ui/api/javascript/ui/grid/configuration/columns.sortable