最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Custom sort function kendo grid - Stack Overflow

programmeradmin1浏览0评论

Can anyone please let me know how can we write our own function in javascript for sorting in kendo grid. And do we need to write two functions for asc and desc?

Any help.. greatly appreciated!

Can anyone please let me know how can we write our own function in javascript for sorting in kendo grid. And do we need to write two functions for asc and desc?

Any help.. greatly appreciated!

Share asked May 20, 2014 at 17:57 ak17ak17 591 gold badge2 silver badges8 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

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 pare function for the column field that you need a special algorithm.

Example:

Lets assume that we want to sort a grid by name (a string) and this is our data:

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 e before uppercase.

If we want to sort it first upper and then lowercase (ASCII order), we should define columns.sortable.pare for name as:

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

The pare function receives two items to pare.

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!

发布评论

评论列表(0)

  1. 暂无评论