我在免费的 jqGrid 中引入了custom filtering 功能,可以轻松实现青春等场景。 The answer 提供了这种实现的示例。
在您的情况下,例如可以在短名称"deq" 下定义新的Date only "equal"compare 操作,在短名称dne 下定义比较操作Date only "not equal"。 customSortOperations 选项的代码如下:
customSortOperations: {
deq: {
operand: "==",
text: "Date only \"equal\"",
filter: function (options) {
var p = this.p, iCol = p.iColByName[options.cmName], cm = p.colModel[iCol],
newformat = cm.formatoptions != null && cm.formatoptions.newformat ?
cm.formatoptions.newformat :
$(this).jqGrid("getGridRes", "formatter.date.newformat"),
srcformat = cm.formatoptions != null && cm.formatoptions.srcformat ?
cm.formatoptions.srcformat :
$(this).jqGrid("getGridRes", "formatter.date.srcformat"),
fieldData = $.jgrid.parseDate.call(this, srcformat, options.item[options.cmName]),
searchValue = $.jgrid.parseDate.call(this, newformat, options.searchValue);
return fieldData.getFullYear() === searchValue.getFullYear() &&
fieldData.getMonth() === searchValue.getMonth() &&
fieldData.getDate() === searchValue.getDate();
}
},
dne: {
operand: "!=",
text: "Date only \"not equal\"",
filter: function (options) {
var p = this.p, iCol = p.iColByName[options.cmName], cm = p.colModel[iCol],
newformat = cm.formatoptions != null && cm.formatoptions.newformat ?
cm.formatoptions.newformat :
$(this).jqGrid("getGridRes", "formatter.date.newformat"),
srcformat = cm.formatoptions != null && cm.formatoptions.srcformat ?
cm.formatoptions.srcformat :
$(this).jqGrid("getGridRes", "formatter.date.srcformat"),
fieldData = $.jgrid.parseDate.call(this, srcformat, options.item[options.cmName]),
searchValue = $.jgrid.parseDate.call(this, newformat, options.searchValue);
return fieldData.getFullYear() !== searchValue.getFullYear() ||
fieldData.getMonth() !== searchValue.getMonth() ||
fieldData.getDate() !== searchValue.getDate();
}
}
}
为了能够使用新的"deq" 和"dne" 操作,您应该在searchoptions 的sopt 中包含日期。
The demo 使用上面的代码。输入数据包含3个日期:"2015-04-15T15:31:49.357"、"2015-04-15T21:15:40.123"、"2015-04-15":
var mydata = [
{ id: "10", invdate: "2015-04-15T15:31:49.357", name: "test1",... },
{ id: "20", invdate: "2015-04-15T21:15:40.123", name: "test2",... },
{ id: "30", invdate: "2015-04-15", name: "test3", ...},
...
]
15-Apr-2015 的过滤显示所有三行:
Another demo 使用几乎相同的代码,但以完整的日期/时间格式显示日期。尽管如此,过滤工作。请注意,我在演示中使用了来自 GitHub 的最新免费 jqGrid 源。确实需要它,因为我在parseDate 的代码中做了一些small changes 来使演示工作。