【发布时间】:2013-12-28 17:09:01
【问题描述】:
使用 jqGrid 4.5.2。在我的数据集中,我有一列可以包含整数或文本。我将sorttype 设置为text。
返回的数据将在网格中混合,并且可以包含字符或数字。它可能只包含字母、数字或两者的混合。如果我单击该列按降序对其进行排序,它会:
400
350
300
200
1100
1020
1010
1000
100
数字可以变化,字母也可以变化。有没有办法定义一个自定义的 sorttype 函数来正确地对同一列中的数字(如数字)和字符串(如字符串)进行排序?如果有,怎么做?
我找到了使用CASE 类型语句的示例,但由于单元格的内容不为人所知,所以我不能这样做。如有任何想法,将不胜感激。
编辑
根据@Oleg 的回答,我尝试了几种不同的方法来实现sorttype 作为一个函数。但是,我似乎无法让它着火。它将对数据进行排序,但似乎将所有内容都排序为字符串。我在代码中有一些 console.log 语句应该转储那里的任何值,但没有显示任何内容。
第一次尝试是在colModel 中使用该函数。
{name:"MySource",
index:"MySource",
width:40,
align:"left",
sorttype: function (cell, rowObject) {
if (typeof cell === "string" && /^test(\d) + $/i.test(cell)) {
console.log("inside if custom sort - cell = " + cell );
return parseInt(cell);
}
else {
console.log("else - cell = " + cell );
return cell;
}
},
第二次尝试是在查看您在类似问题上的另一个代码示例并创建一个函数然后从sorttype 调用该函数之后。
这里是colModel:
{name:"MySource",
index:"MySource",
width:40,
align:"left",
sorttype: function (cell) {
return myCustSort (cell) ;
}
}
}
和myCustSort 函数:
function myCustSort (myCell) {
if (typeof myCell === "string" && /^test(\d) + $/i.test(myCell)) {
console.log("inside if custom sort - cell = " + myCell );
return parseInt( myCell);
}
else {
console.log("else - cell = " + myCell );
return myCell;
}
} // end myCustSort
当我单击该列标题时,我可以在网格排序中看到数据,但是两个显示都没有在控制台日志中放置任何内容。当单击列标题对其进行排序时,它不应该触发并这样做,还是我错过了什么?
谢谢!
【问题讨论】:
标签: jquery sorting text jqgrid integer