仅按日期过滤 Telerik MVC 网格(忽略时间值)

发布于 2024-12-02 17:15:46 字数 449 浏览 0 评论 0原文

我在 Telerik MVC Grid 中有一个 DateTime 类型的列。我只想使用它的日期部分来显示和过滤。因此,我使用了以下代码

@(Html.Telerik().Grid<State>()
      .Name("tlkStateGrid")
      .Columns(col => {
          col.Bound(m => m.CreatedOn).Title("Created On").Format("{0:dd/MM/yyyy}");

网格确实以指定的格式显示日期(CreatedOn)。但是,过滤并没有按预期工作。我认为在过滤时,网格也会考虑时间值。因此,如果两个日期相同但时间值不同,则它们不被视为“相等”。 如何配置网格以在过滤时忽略时间值并仅比较日期。

注意:我在 Telerik MVC 论坛中发布了类似的问题,但尚未得到答案。

I have a column in Telerik MVC Grid which is of type DateTime. I want to only use the Date part of it for display and filtering. As a result, I used the following code

@(Html.Telerik().Grid<State>()
      .Name("tlkStateGrid")
      .Columns(col => {
          col.Bound(m => m.CreatedOn).Title("Created On").Format("{0:dd/MM/yyyy}");

The Grid does display the Date (CreatedOn) in the format specified. However, the filtering doesn't work as expected. I think that while filtering, the time values are considered too by the Grid. So if two dates are same but having different time values, they are not considered 'equal'.
How do I configure grid to neglect the time values and compare only dates, when filtering.

Note: I have posted similar question in Telerik MVC forums but no answer as yet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

提笔书几行 2024-12-09 17:15:46

您可以修改 lambda 表达式以将 CreatedOn 属性显式修改为日期。您将仅获得日期时间对象的日期部分。

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)

You could modify lambda expression to explicitly modify the CreatedOn property to be just a date. You'll get just the date component of the datetime object.

col.Bound(m => m.CreatedOn.Date)

You new code will now be as below.

@(Html.Telerik().Grid<State>()
      .Name("tlkStateGrid")
      .Columns(col => {
          col.Bound(m => m.CreatedOn.Date).Title("Created On").Format({0:dd/MM/yyyy}");

if CreatedOn is a nullable datetime you'll need to access its Value property before getting the Date component. See below.

col.Bound(m => m.CreatedOn.HasValue? m.CreatedOn.Value.Date : m.CreatedOn)
指尖凝香 2024-12-09 17:15:46

我创建了以下代码,它改变了日期过滤器的定义方式:

/// <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);
}

I've created the following code which changes the way a date-filter is defined:

/// <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);
        }
    }
}

This can be used like:

protected void MapGridCommand(GridCommand command)
{
    this.MapDateFilter(new List<IFilterDescriptor>(command.FilterDescriptors).AsReadOnly(), command.FilterDescriptors);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文