我在Telerik MVC Grid中有一个类型为DateTime的列。我想只使用它的Date部分进行显示和过滤。结果,我使用了以下代码
@(Html.Telerik().Grid<State>()
.Name("tlkStateGrid")
.Columns(col => {
col.Bound(m => m.CreatedOn).Title("Created On").Format("{0:dd/MM/yyyy}");
网格确实以指定的格式显示日期(CreatedOn)。但是,过滤不能按预期工作。我认为在过滤时,网格也会考虑时间值。因此,如果两个日期相同但具有不同的时间值,则它们不被视为“相等”。 在过滤时,如何配置网格以忽略时间值并仅比较日期。
注意:我在Telerik MVC论坛上发布了类似的问题但尚无答案。
答案 0 :(得分:2)
您可以修改lambda表达式以将CreatedOn属性显式修改为日期。您将只获得datetime对象的日期组件。
col.Bound(m => m.CreatedOn.Date)
您现在的新代码如下所示。
@(Html.Telerik().Grid<State>()
.Name("tlkStateGrid")
.Columns(col => {
col.Bound(m => m.CreatedOn.Date).Title("Created On").Format({0:dd/MM/yyyy}");
如果CreatedOn是可以为空的日期时间,则需要在获取Date组件之前访问其Value属性。见下文。
col.Bound(m => m.CreatedOn.HasValue? m.CreatedOn.Value.Date : m.CreatedOn)
答案 1 :(得分:1)
我创建了以下代码,它改变了日期过滤器的定义方式:
/// <summary>
/// Maps DateTime related filters.
/// * IsEqualTo filter is converted to ('x-date' is GreaterThanOrEqualTo 'SearchDate') AND ('x-date' is LessThan 'SearchDate' + 1 Day)
/// * IsNotEqualTo filter is converted to ('x-date' LessThan 'SearchDate') OR ('x-date' is GreaterThan 'SearchDate' + 1 Day) OR ('x-date' is NULL)
/// * IsLessThanOrEqualTo filter is converted to ('x-date' is LessThan 'SearchDate' + 1 Day)
/// </summary>
/// <param name="copyOfOriginalFilters">A copy from the filters.</param>
/// <param name="newFilters">The new filters</param>
protected void MapDateFilter(ReadOnlyCollection<IFilterDescriptor> copyOfOriginalFilters, IList<IFilterDescriptor> newFilters)
{
newFilters.Clear();
foreach (var currentFilter in copyOfOriginalFilters)
{
Type t = currentFilter.GetType();
if (t == typeof(FilterDescriptor))
{
var filter = currentFilter as FilterDescriptor;
if (filter.ConvertedValue.GetType() == typeof(DateTime))
{
DateTime datePart = ((DateTime)filter.ConvertedValue).Date;
if (filter.Operator == FilterOperator.IsEqualTo)
{
var compositeFilter = new CompositeFilterDescriptor();
compositeFilter.FilterDescriptors.Add(new FilterDescriptor(filter.Member, FilterOperator.IsGreaterThanOrEqualTo, datePart));
compositeFilter.FilterDescriptors.Add(new FilterDescriptor(filter.Member, FilterOperator.IsLessThan, datePart.AddDays(1)));
compositeFilter.LogicalOperator = FilterCompositionLogicalOperator.And;
newFilters.Add(compositeFilter);
}
else if (filter.Operator == FilterOperator.IsNotEqualTo)
{
var compositeFilter = new CompositeFilterDescriptor();
compositeFilter.FilterDescriptors.Add(new FilterDescriptor(filter.Member, FilterOperator.IsLessThan, datePart));
compositeFilter.FilterDescriptors.Add(new FilterDescriptor(filter.Member, FilterOperator.IsGreaterThan, datePart.AddDays(1)));
compositeFilter.FilterDescriptors.Add(new FilterDescriptor(filter.Member, FilterOperator.IsEqualTo, null));
compositeFilter.LogicalOperator = FilterCompositionLogicalOperator.Or;
newFilters.Add(compositeFilter);
}
else if (filter.Operator == FilterOperator.IsLessThanOrEqualTo)
{
newFilters.Add(new FilterDescriptor(filter.Member, FilterOperator.IsLessThan, datePart.AddDays(1)));
}
else
{
newFilters.Add(filter);
}
}
else
{
newFilters.Add(filter);
}
}
else if (t == typeof(CompositeFilterDescriptor))
{
var filter = currentFilter as CompositeFilterDescriptor;
int numberOfDateFilters = filter.FilterDescriptors.Where(fd =>
fd is FilterDescriptor &&
(fd as FilterDescriptor).ConvertedValue != null &&
(fd as FilterDescriptor).ConvertedValue.GetType() == typeof(DateTime))
.Count();
if (numberOfDateFilters == 2)
{
var firstFilter = filter.FilterDescriptors[0] as FilterDescriptor;
firstFilter.Value = ((DateTime)firstFilter.ConvertedValue).Date;
var secondFilter = filter.FilterDescriptors[1] as FilterDescriptor;
secondFilter.Value = ((DateTime)secondFilter.ConvertedValue).Date;
}
else
{
MapDateFilter(new List<IFilterDescriptor>(filter.FilterDescriptors).AsReadOnly(), filter.FilterDescriptors);
}
newFilters.Add(filter);
}
}
}
这可以像:
一样使用protected void MapGridCommand(GridCommand command)
{
this.MapDateFilter(new List<IFilterDescriptor>(command.FilterDescriptors).AsReadOnly(), command.FilterDescriptors);
}