2011-09-02 47 views
1

我在Telerik MVC Grid中有一個類型爲DateTime的列。我只想使用它的日期部分進行顯示和過濾。其結果是,我用下面的代碼僅在Date中過濾Telerik MVC Grid(忽略時間值)

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

網格並顯示在指定的格式的日期(CreatedOn)。但是,篩選不能按預期工作。我認爲,在過濾的同時,網格也會考慮時間值。因此,如果兩個日期相同但具有不同的時間值,則它們不被視爲「相等」。 如何配置網格以忽略時間值並僅比較過濾時的日期。

注意:我在Telerik MVC論壇發佈了類似的問題,但目前還沒有答案。

回答

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) 
+0

scartag,感謝答覆。 CreatedOn字段是可以爲空的日期時間,所以我使用了col.Bound(m => m.CreatedOn.Value.Date),正如您所建議的那樣。但是,當我嘗試運行該應用程序時,出現「System.InvalidOperationException:Nullable對象必須有值」。 – Jatin

+0

我正在編輯以處理場景。 – scartag

+0

@Nirvan我完成了編輯。你可以試試看。 – scartag

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