我在 demo website 中使用了 q jqgrid,'groupd header row config' 版本的 jqgrid 。
它生成网格。但假设 senario 其中 Notes
列可以包含数字和字符串,因为某些行中有数字,某些行中有字符串字符,排序会被关闭,并且它仅适用于字符串字符。
场景:假设表格有五行,Notes
列有以下五行数据:
this is good
123
number 123 number
>123.23
<=222.88
我已使用 sort:true 将排序选项添加到 colModel 中,但这仅在注释列具有所有行的字符串字符时才进行排序,但如果它具有数字行和字符串字符行的组合,则不会进行排序。我该如何解决这个问题?
最佳答案
jqGrid 包含多种自定义排序的可能性。首先值得一提的是,只有当您使用 datatype: "local"
或某些远程 datatype
("json"
) 时,以下所有可能性才有意义code>、"jsonp"
、"xml"
) 与 loadonce: true
结合使用。在最后一种情况下,第一次从服务器加载数据后,数据类型
将更改为“local”
。
要对列中的本地数据进行排序,jqGrid 必须比较值。它执行一些初步步骤。首先,如果用项目填充数组,它将内容从一列映射到 rowid。因此你可以定义
sorttype: function (cellValue, item) {
return cellValue; // another value as cellValue can be returned
}
它为您提供了第一种自定义排序方式。例如,您可以使用 RegEx 从 callValue
中提取信息的“数字”部分,并从 sorttype
返回它(您可以在 colModel 中定义)
列注释
)。请参阅the old demo为 the answer 创建了解更多详情。
第二种方式:在colModel
中定义sortfunc
回调。回调有原型(prototype)
sortfunc: function (a, b, direction) {
// should return 0, 1 or -1
}
在旧版本的 jqGrid 和
sortfunc: function (a, b, direction, aItem, bItem) {
// should return 0, 1 or -1
}
在 free jqGrid 。 sortfunc
允许您实现您需要的任何自定义排序行为。请参阅the demo为 the issue 创建代码示例。
关于javascript - 如何对具有多种类型值的列进行排序? [特兰德JQGrid 4.6],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40199651/