自定义排序功能剑道网格 [英] Custom sort function kendo grid
问题描述
任何人都可以让我知道如何在kendo网格中用javascript编写我们自己的函数.我们是否需要为asc和desc编写两个函数?
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!
推荐答案
您可以做到这一点,而不必编写两个不同的函数进行升序和降序,因为您唯一需要做的就是提供column的href ="http://docs.telerik.com/kendo-ui/api/web/grid#configuration-columns.sortable.compare" rel ="nofollow"> compare
函数您需要特殊算法的字段.
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.
示例:
让我们假设我们要按name
(a string
)排序网格,这是我们的数据:
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" }
],
,列定义为:
columns : [
{ field: "id", title: "id" },
{ field: "name", title: "Name"}
]
我们得到的是:
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.
如果我们要先对大写字母然后小写字母(ASCII顺序)进行排序,则应将name
的columns.sortable.compare
定义为:
If we want to sort it first upper and then lowercase (ASCII order), we should define columns.sortable.compare
for name
as:
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;
}
}
}
]
compare
函数接收两项进行比较.
The compare
function receives two items to compare.
现在,我们得到的是:
id Name
3 Jane
7 John
4 jack
2 jane
5 jane
6 janette
1 john
您可以在ASC和DESC上都尝试使用它.这里简单,整洁!
You can try it both for ASC and DESC here Simple and neat!
这篇关于自定义排序功能剑道网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!