我知道我迟到了这个答案,但它仍然可能对某人有所帮助。
我猜您正在经历这种情况,因为您的服务器端DateTime
值也包含小数秒数据,并且等号运算符在比较时不会忽略它们。我发现提出服务器端解决方案比编写各种肮脏的 JS 变通办法更容易。
这个想法是,每当您在DataSourceRequest
对象中找到将过滤DateTime
属性的过滤器时,您手动将其替换为 a CompositeFilterDescriptor
,这会将值截断为所需的精度,将其设置为下限,然后添加所需精度的一个单位(秒、分钟、小时等)并将其设置为上限。
代码如下:
public static class KendoHelpers
{
public enum DateTimePrecision
{
Seconds = 1,
Minutes = 2,
Hours = 4
}
public static DataSourceRequest NormalizeDateFilters(this DataSourceRequest request, DateTimePrecision precision)
{
// TODO: Add parameter validation.
for (int i = 0; i < request.Filters.Count; ++i)
{
FilterDescriptor filter = request.Filters[i] as FilterDescriptor;
if (filter != null && filter.ConvertedValue is DateTime && filter.Operator == FilterOperator.IsEqualTo)
{
DateTime val = (DateTime)filter.ConvertedValue;
CompositeFilterDescriptor newFilter = new CompositeFilterDescriptor
{
LogicalOperator = FilterCompositionLogicalOperator.And
};
DateTime lowerBound;
DateTime upperBound;
if (precision == DateTimePrecision.Seconds)
{
lowerBound = val.TruncateToWholeSeconds();
upperBound = lowerBound.AddSeconds(1);
}
else if (precision == DateTimePrecision.Minutes)
{
lowerBound = val.TruncateToWholeMinutes();
upperBound = lowerBound.AddMinutes(1);
}
else if (precision == DateTimePrecision.Hours)
{
lowerBound = val.TruncateToWholeHours();
upperBound = lowerBound.AddHours(1);
}
else
{
// If someone would be stupid enough to supply Hours | Minutes
throw new ArgumentException("Not supported precision. Only Second, Minute, Hour values are supported.", "precision");
}
newFilter.FilterDescriptors.Add(new FilterDescriptor
{
Member = filter.Member,
MemberType = filter.MemberType,
Operator = FilterOperator.IsGreaterThanOrEqualTo,
Value = lowerBound
});
newFilter.FilterDescriptors.Add(new FilterDescriptor
{
Member = filter.Member,
MemberType = filter.MemberType,
Operator = FilterOperator.IsLessThan,
Value = upperBound
});
request.Filters[i] = newFilter;
}
}
return request;
}
}
评论:
DateTime
截断扩展基于此答案。
- 此方法仅在运算符为 时才会执行任何操作
equals
,因为如果您选择 Is later than 等,默认行为也将正常工作。
- 此方法不关心任何 present
CompositeFilterDescriptor
,因为表达式dateToSearch = 2016-11-21 11:22:00 AND dateToSearch = 2016-11-21 11:59:00
无论如何都没有意义。
- 可以为
DateTimeOffset
价值观做类似的事情。