javascript 生成 jqgrid 后如何对特定列实现自定义排序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5294535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 16:41:07  来源:igfitidea点击:

How do I implement custom sort to a specific column after jqgrid has been generated?

javascriptjqgrid

提问by bcm

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 后,是否有一种方法可以用于覆盖/插入 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.

但不起作用。

回答by Oleg

The usage of sorttypeas the function can be useful for any local datatype of jqGrid or in case of the usage loadonce:truejqGrid paremter with the "remote" datatypes 'json' or 'xml'. If it is needed you can change the sorttypeof any column dynamically.

的使用sorttype作为函数可能是有用的jqGrid为任何本地数据类型或在使用的情况下loadonce:true的jqGrid paremter与“远程”的数据类型“JSON”或“XML”。如果需要,您可以sorttype动态更改任何列的 。

I made the new demofor 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

我为您制作了新的演示来演示该功能。在开始时,网格将按“客户”列排序,包含的列将被解释为文本字符串。结果显示如下

enter image description here

在此处输入图片说明

Wenn we check the checkbox "Set custom sorttype function" the grid will be sorted as displayed on the next picture

我们选中复选框“设置自定义排序类型功能”网格将按显示在下一张图片上进行排序

enter image description here

在此处输入图片说明

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 gridis $("#list").

哪里grid$("#list")

If one click on the checkbox one more time the original sorting method with sorttype:"text"will be used.

如果再次单击复选框,sorttype:"text"将使用原始排序方法。