如何对具有多种类型值的列进行排序? [Trirand JQGrid 4.6]

时间:2016-10-23 04:52:45

标签: javascript jquery jqgrid

我在demo website中使用了q jqgrid,' groupd标题行配置'版本的jqgrid。

enter image description here

它生成网格。但假设senario中Notes列可以包含数字和字符串,因为在某些行中有数字,在某些行中有字符串字符,排序关闭,它只适用于字符串字符。

场景:假设该表有五行,Notes列包含五行中的以下数据:

this is good

123

number 123 number

>123.23

<=222.88

我已经将排序选项添加到colModel使用sort:true但是这只会在notes列具有所有行的字符串字符时排序,但如果它具有数字行和字符串char行的组合则不会。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

jqGrid包含多种自定义排序的可能性。首先,重要的是要提到以下所有可能性只有在您使用datatype: "local"或某些远程datatype"json""jsonp""xml"时才有意义与loadonce: true结合使用。在最后一种情况下,datatype将在第一次从服务器加载数据后更改为"local"

要对列中的本地数据进行排序,jqGrid必须比较这些值。它做了一些初步的步骤。首先,如果用数组填充数组,则将内容从一列映射到rowid。因此,您可以定义

sorttype: function (cellValue, item) {
    return cellValue; // another value as cellValue can be returned
}

它为您提供了自定义排序的第一种方式。例如,您可以使用RegEx从callValue中提取信息的“数字”部分,并将其从sorttype返回(您可以在colModel中为列{{ 1}})。有关详细信息,请参阅为the old demo创建的the answer

第二种方式:在notes中定义sortfunc回调。回调有原型

colModel

在旧版本的jqGrid和

sortfunc: function (a, b, direction) {
    // should return 0, 1 or -1
}
free jqGrid中的

sortfunc: function (a, b, direction, aItem, bItem) { // should return 0, 1 or -1 } 允许您实现所需的任何自定义排序行为。有关代码示例,请参阅为the demo创建的the issue