jqgrid 如何让 searchoptions.dataInit 像 formatter一样 每次执行
时间: 2024-03-03 13:48:40 浏览: 18
在 jqGrid 中,searchoptions.dataInit 是用于初始化搜索表单元素的值的。如果需要在每次搜索时都执行 searchoptions.dataInit,可以使用以下代码:
```
// 获取搜索表单元素
var searchElem = $("#grid").jqGrid("getGridParam", "search");
// 绑定搜索表单元素的 change 事件
searchElem.find("input[type=text]").change(function() {
// 执行 searchoptions.dataInit
var colModel = $("#grid").jqGrid("getGridParam", "colModel");
var colName = searchElem.find(".select[name=jqg_col]").val();
for (var i = 0; i < colModel.length; i++) {
if (colModel[i].name == colName && colModel[i].searchoptions && colModel[i].searchoptions.dataInit) {
colModel[i].searchoptions.dataInit.call(this, this);
break;
}
}
});
// 初始化搜索表单元素的值
$("#grid").jqGrid("setColProp", "columnName", {
searchoptions: {
dataInit: function(elem) {
// 初始化搜索表单元素的值
}
},
formatter: function(cellValue, options, rowObject) {
// 格式化单元格的值
}
});
```
这段代码会先获取搜索表单元素,然后绑定搜索表单元素的 change 事件。在 change 事件中,会执行 searchoptions.dataInit 方法。具体来说,会遍历表头中的每个列,如果找到了当前选择的列,并且该列有 searchoptions.dataInit 方法,就会执行该方法。
需要注意的是,每次搜索时都会执行 searchoptions.dataInit 方法,这可能会影响搜索性能。因此,如果搜索表单元素的值不需要每次搜索都更新,就不需要使用这种方法。