javascript - 如何对具有多种类型值的列进行排序? [特兰德JQGrid 4.6]

标签 javascript jquery jqgrid

我在 demo website 中使用了 q jqgrid,'groupd header row config' 版本的 jqgrid 。

enter image description here

它生成网格。但假设 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 demothe 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 jqGridsortfunc 允许您实现您需要的任何自定义排序行为。请参阅the demothe issue 创建代码示例。

关于javascript - 如何对具有多种类型值的列进行排序? [特兰德JQGrid 4.6],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40199651/

相关文章:

jquery - jqGrid悬停文本可以用AddRowData定义吗?

javascript - 使用键数组过滤对象数组并提取其属性

c# - 使用模型值包括报价 ASP MVC4

javascript - 如何在按下按钮时触发警报功能?

jquery - 对不包含另一个范围的范围进行特殊搜索

javascript - 为什么我的 Jquery 元素大小调整不起作用?

javascript - webpack css-loader 无法按预期与 karma 一起工作

javascript - 我如何打开 Bootstrap 模态 onbeforeunload 功能,

javascript - 是否可以从 jqGrid header 中删除展开/折叠按钮?

jquery - 如何使jqGrid弹出模态窗口居中?