如何将表格列过滤器与格式化列一起使用? [英] How to use a table column filter with formatted columns?
问题描述
我有一个 JSON 模型,其中包含一些存储为纪元值的日期值:
I have a JSON model which contains, among others, a few date values which are stored as epoch values:
var oData = [{
string : "SomeValue",
date : 1404172800000
}];
当我加载我的模型时,我使用以下方法将此时代转换为正确的 Javascript 日期对象:
When I load my model, I convert this epoch to a proper Javascript Date object using:
for (var i = 0; i < oData .length; i++) {
var dateLong = oData[i].date;
oData[i].date = new Date(dateLong);
}
在我的表格中,我然后使用格式化程序函数呈现列:
In my table, I then render the column using a formatter function:
var oDateColumn = new sap.ui.table.Column({
label: new sap.ui.commons.Label({
text: "A Date"
}),
template: (new sap.ui.commons.TextView({
text : {
parts : [date],
formatter : function(oValue) {
if (oValue != undefined) {
var yyyy = oValue.getFullYear().toString();
var mm = (oValue.getMonth()+1).toString(); // getMonth() is zero-based
var dd = oValue.getDate().toString();
return yyyy + '/' + (mm[1]?mm:"0"+mm[0]) + '/' + (dd[1]?dd:"0"+dd[0]);
} else return "";
}
},
textAlign : sap.ui.core.TextAlign.Right
})),
sortProperty : "date",
filterProperty : "date",
filterOperator : sap.ui.model.FilterOperator.EQ
});
这没问题,前一个纪元现在是一个日期,很好地呈现为2014/07/01"
This works ok, and the former epoch is now a date which is nicely rendered as '2014/07/01'
然而,过滤不是在格式化的日期上,而是在原始日期对象上——如果我在 '2014/07/01' 上过滤,我不会得到任何结果;如果我过滤 '1404172800000' 我得到过滤结果...
However, the filtering is not on the formatted date but on the original Date object -- if I filter on '2014/07/01' I get no results; if I filter on '1404172800000' I get the filtered results...
我尝试在 filterProperty 上使用格式化程序,但无法使其正常工作.
I tried using a formatter on the filterProperty but I wasn't able to get this to work.
有谁知道我如何让用户过滤格式化的日期?
Does anyone know how I can have users filter on the formatted date?
推荐答案
使用日期类型可能会解决这个问题
Using a date type might solve this issue
var dateType = new sap.ui.model.type.Date({
pattern: "yyyy/MM/dd"
});
...
sortProperty : "date",
filterProperty : "date",
filterType: dateType
看看这个示例对于生日列的简单用例
look at this example for a simple use case on birthday column
这篇关于如何将表格列过滤器与格式化列一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!