[英]How do I implement custom sort to a specific column after jqgrid has been generated?
Is there a method I can use to over-write/insert a custom function "sorttype" for a specific column in the colModel after it has been populated using javascript (jQuery)? 在使用javascript(jQuery)填充后,是否有一种方法可以用来为colModel中的特定列重写/插入自定义函数“sorttype”?
I've found an example here: http://www.ok-soft-gmbh.com/jqGrid/CustomSorttype1.htm , where sorttype is implemented with the initial settings, but what I need to change it after. 我在这里找到了一个例子: http ://www.ok-soft-gmbh.com/jqGrid/CustomSorttype1.htm,其中sorttype是用初始设置实现的,但我之后需要更改它。
Tried: 尝试:
var attName = grid.getGridParam("colModel")[1].name;
grid.setColProp(attName, { sorttype: function (cell) {
if (cell == '<div>x</div>') { return '0' } else { return '1' };
}
});
but doesn't work. 但不起作用。
The usage of sorttype
as the function can be useful for any local datatype of jqGrid or in case of the usage loadonce:true
jqGrid paremter with the "remote" datatypes 'json' or 'xml'. 使用sorttype
作为函数对于jqGrid的任何本地数据类型或者在使用loadonce:true
情况下都是loadonce:true
jqGrid paremter与“remote”数据类型'json'或'xml'。 If it is needed you can change the sorttype
of any column dynamically. 如果需要,您可以动态更改任何列的sorttype
。
I made the new demo for you to demonstrate the feature. 我为您制作了新演示以演示该功能。 At the begining the grid will be sorted by 'Client' column and the column contain will be interpret as the text string. 在开始时,网格将按“客户”列排序,列包含将被解释为文本字符串。 The results are displayed below 结果显示如下
Wenn we check the checkbox "Set custom sorttype function" the grid will be sorted as displayed on the next picture 我们检查复选框“设置自定义排序类型功能”,网格将按照下一张图片显示的顺序排序
To implement such sorting I defined the function 为了实现这种排序,我定义了这个功能
var myCustomSort = function(cell,rowObject) {
if (typeof cell === "string" && /^test(\d)+$/i.test(cell)) {
return parseInt(cell.substring(4),10);
} else {
return cell;
}
}
and the 'change' event handler on the checkbox 和复选框上的'更改'事件处理程序
$("#customsorttype").change(function() {
var isChecked = $(this).is(':checked');
if (isChecked) {
cm.sorttype = myCustomSort;
} else {
cm.sorttype = "text";
}
grid.trigger("reloadGrid");
});
where grid
is $("#list")
. grid
是$("#list")
。
If one click on the checkbox one more time the original sorting method with sorttype:"text"
will be used. 如果再次单击该复选框,将使用具有sorttype:"text"
的原始排序方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.