简体   繁体   English

自定义排序功能kendo网格

[英]Custom sort function kendo grid

Can anyone please let me know how can we write our own function in javascript for sorting in kendo grid. 任何人都可以让我知道如何在javascript中编写我们自己的功能,以便在kendo网格中进行排序。 And do we need to write two functions for asc and desc? 我们需要为asc和desc编写两个函数吗?

Any help.. greatly appreciated! 任何帮助..非常感谢!

You can do it and you don't have to write two different function for ascending and descending since the only thing that you need to do is providing a compare function for the column field that you need a special algorithm. 您可以这样做而且您不必为升序和降序编写两个不同的函数,因为您需要做的唯一事情是为column字段提供需要特殊算法的compare函数。

Example: 例:

Lets assume that we want to sort a grid by name (a string ) and this is our data: 让我们假设我们想要按namestring )对网格进行排序,这是我们的数据:

data    : [
    { id : 1, name : "john" },
    { id : 2, name : "jane" },
    { id : 3, name : "Jane" },
    { id : 4, name : "jack" },
    { id : 5, name : "jane" },
    { id : 6, name : "janette" },
    { id : 7, name : "John" }
],

and the columns definition as: 并将列定义为:

columns   : [
    { field: "id", title: "id" },
    { field: "name", title: "Name"}
]

What we get is: 我们得到的是:

id   Name
4    jack
2    jane
5    jane
3    Jane
6    janette
1    john
7    John

As we see we get it sorted alphabetically mixing lower and uppercase but lowercase always come before uppercase. 正如我们所看到的那样,我们按字母顺序将它按大小写混合排序,但小写字母总是在大写之前。

If we want to sort it first upper and then lowercase (ASCII order), we should define columns.sortable.compare for name as: 如果我们想先将它排序为大写然后小写(ASCII顺序),我们应该将columns.sortable.compare定义为name

columns   : [
    { field: "id", title: "id" },
    { 
        field: "name", 
        title: "Name",
        sortable: {
            compare: function (a, b) {
                return a.name === b.name ? 0 : (a.name > b.name) ? 1 : -1;
            }
        }
    }
]

The compare function receives two items to compare. compare函数接收两个要比较的项目。

Now, what we get is: 现在,我们得到的是:

id   Name
3    Jane
7    John
4    jack
2    jane
5    jane
6    janette
1    john

You can try it both for ASC and DESC here Simple and neat! 您可以在这里尝试ASC和DESC简单而整洁!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM