需要使用JavaScript来对字母数字年龄的Kendo网格列进行帮助
在我们的用户数据网格中,我们为每个用户都有一个字段,其中包括其年龄为字符串,例如77年,6个月或5天。我们需要一种方法来通过该列对网格进行分类,并使年龄进行数字排序,其中一个30年,50年,5天,10年,4个月将以50年,30年,10年为单位,4个月零5天。我尝试了几种分类功能,这些函数以字母数为单位,但无法弄清楚如何迫使几个月不到几年,而天数要少于几个月。
这是我试图在堆栈溢出上发现的当前排序功能。
var a, b, a1, b1, rx=/(\d+)|(\D+)/g, rd=/\d+/;
function AlphaNumericCaseInsensitive(as, bs) {
a= String(as).toLowerCase().match(rx);
b= String(bs).toLowerCase().match(rx);
while(a.length && b.length){
a1= a.shift();
b1= b.shift();
if(rd.test(a1) || rd.test(b1)){
if(!rd.test(a1)) return 1;
if(!rd.test(b1)) return -1;
if(a1!= b1) return a1-b1;
}
else if(a1!= b1) return a1> b1? 1: -1;
}
return a.length- b.length;
}
var dataSource = new kendo.data.DataSource({
data: [
{ id: 1, age: "101 y" },
{ id: 2, age: "2 m" },
{ id: 3, age: "11 y" },
{ id: 4, age: "1 y" },
{ id: 5, age: "2 y" },
{ id: 6, age: "4 d" },
]
});
$("#grid").kendoGrid({
dataSource: dataSource,
sortable: true,
columns: [{
field: "age",
sortable: {
compare: function(a, b) {
return AlphaNumericCaseInsensitive(a.item, b.item);
}
}
}]
});
In our grid of user data, we have a field for each user that includes their age calculated as a string, such as 77 years, or 6 months, or 5 days. We need a way to be able to sort the grid by that column and have the ages sort numerically where a group such as 30 years, 50 years, 5 days, 10 years, 4 months would sort as 50 years, 30 years, 10 years, 4 months, 5 days. I have tried several sorting functions that sort alphanumerically but haven't been able to figure out how to force months to be less than years, and days to be less than months.
This is the current sorting function that I am trying to use that I found on stack overflow.
var a, b, a1, b1, rx=/(\d+)|(\D+)/g, rd=/\d+/;
function AlphaNumericCaseInsensitive(as, bs) {
a= String(as).toLowerCase().match(rx);
b= String(bs).toLowerCase().match(rx);
while(a.length && b.length){
a1= a.shift();
b1= b.shift();
if(rd.test(a1) || rd.test(b1)){
if(!rd.test(a1)) return 1;
if(!rd.test(b1)) return -1;
if(a1!= b1) return a1-b1;
}
else if(a1!= b1) return a1> b1? 1: -1;
}
return a.length- b.length;
}
var dataSource = new kendo.data.DataSource({
data: [
{ id: 1, age: "101 y" },
{ id: 2, age: "2 m" },
{ id: 3, age: "11 y" },
{ id: 4, age: "1 y" },
{ id: 5, age: "2 y" },
{ id: 6, age: "4 d" },
]
});
$("#grid").kendoGrid({
dataSource: dataSource,
sortable: true,
columns: [{
field: "age",
sortable: {
compare: function(a, b) {
return AlphaNumericCaseInsensitive(a.item, b.item);
}
}
}]
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的数据中有一个属性,可以将其施加到
date
类型,您可以在比较
方法:工作演示:
dojo
If you have a property in your data which you can be cast to
Date
type, you can use it in thecompare
method:Working demo:
Dojo