使用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
当我单击该列标题时,我可以看到网格排序中的数据,但是这两个显示都没有在控制台日志中放置任何内容.当单击列标题对其进行排序,或者我遗漏了什么时,它不应该触发并执行此操作吗?
谢谢!