This is a migrated thread and some comments may be shown as answers.

New Search panel and datetime

6 Answers 1052 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mbott
Top achievements
Rank 1
Mbott asked on 03 Oct 2019, 02:52 PM

Hello. I'm having some trouble getting the new grid search panel to work. The example on the demos is pretty basic and doesn't have much info.

I can get the control to work if I specify which field to search ie: Search(s => { s.Fields("myField"); })

However, my datasource has datetime column and I get an exception when trying to seach all columns.

I get this error: The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.

Can you provide a sample or update the demo to include how to handle datetime fields?

6 Answers, 1 is accepted

Sort by
0
Alex Hajigeorgieva
Telerik team
answered on 07 Oct 2019, 12:56 PM

Hello, Mike,

The new search panel can be used with all types only with client operations, i.e. -> ServerOperation(false). When using server operations (default) the non-string fields should be excluded. We have outlined this in the documentation important notice here:

https://docs.telerik.com/aspnet-core/html-helpers/data-management/grid/search-panel

If you want to filter non-string types on the server, you would need to modify the filters on the server side. An example how this could be done is available here:

https://www.telerik.com/forums/datasourcerequest-manipulating-out-of-box-filtering-(filterdescriptor-vs-compositefilterdescriptor)#il3J1QPj3U6wIVHh0VBQQQ

As an additional note, you would need to modify the operator and try to parse the filter value to a date.

Let me know in case you have further questions.

Kind Regards,
Alex Hajigeorgieva
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
John
Top achievements
Rank 1
Veteran
answered on 13 Dec 2019, 04:19 PM

Hi Alex,

So is it possible to filter dates or not?  I tried using the code provided in the 3 year old example you referenced, but just get errors and seems like any other non-string column has the same issue?  Is there an updated code sample or are there plans to support more than just string values in the near future?

Best,

John

0
Alex Hajigeorgieva
Telerik team
answered on 17 Dec 2019, 10:17 AM

Hello, John,

The search panel builds an expression which utilizes the Contains operator and Contains does not work on Integer and DateTime types in C# when server operations are used. This is specific to C# and LINQ. With this in mind, a grid data source with ServerOperations(), the search panel can filter only DateTime and Integer types which are converted to strings.

For the client operations, it is implemented and it works well with all types because we use JavaScript on the client and not C#.

When server filtering is needed we have two options:

  • the object model should hold a string representation of its value and you can configure the Search fields in the grid to utilize the string field and not touch any controller code
.Search( search=>{
   search.Field(f => f.DiaryCode);
   search.Field(f => f.DueDateAsString); //  this.DueDate.ToShortDateString();
   search.Field(f => f.AssignedUser);
   search.Field(f => f.Comments);
})
// grid columns
  columns.Bound(p => p.DiaryCode).Title("Code").Width(90);
  columns.Bound(p => p.DueDate).Title("Due Date").Format("{0:d}").Width(100);
  columns.Bound(p => p.AssignedUser).Title("Assigned").Width(100);
  columns.Bound(p => p.Comments).Title("Comments").Width(150);
  • the object model should hold a string representation of its value and you can then modify the filter member to use the string representation instead
// grid
columns.Bound(p => p.DueDate).Title("Due Date").Format("{0:d}").Width(100);

// view model
public DateTime DueDate { get; set; }

public string DueDateAsString
{
    get { return this.DueDate.ToShortDateString(); }
    set { this.DueDateAsString = value; }
}

// controller
public ActionResult Diaries_Read([DataSourceRequest]DataSourceRequest request)
{
            ModifyFilters(request.Filters);
            return Json(diaries.ToDataSourceResult(request));
}

private void ModifyFilters(IEnumerable<IFilterDescriptor> filters)
{
      if (filters.Any())
      {
          foreach (var filter in filters)
          {
               var descriptor = filter as FilterDescriptor;
               if (descriptor != null && descriptor.Member == "DueDate")
               {
                        
                   descriptor.Member = "DueDateAsString";
               }
               else if (filter is CompositeFilterDescriptor)
               {
                   ModifyFilters(((CompositeFilterDescriptor)filter).FilterDescriptors);
               }
          }
    }
}

I am attaching a project for your reference where you can test both options. Just add a reference to the Kendo.Mvc.dll so you can build and run it.

I hope this covers all the questions, should further ones arise, feel free to let me know.

Kind Regards,
Alex Hajigeorgieva
Progress Telerik

Get quickly onboarded and successful with your Telerik UI for ASP.NET MVC with the dedicated Virtual Classroom technical training, available to all active customers.
0
Aaron
Top achievements
Rank 1
Veteran
answered on 27 Aug 2020, 06:12 PM

Alex, thanks for the detailed explanation, complete with example code of two different ways to work around this :)

 

Sorry for bringing up a fairly old post, but I was wondering if you might know if this is something that might have built-in support in the future. We've run into the same issue where we're using server-side operations and the search panel doesn't work if the grid has non-string fields (and those fields aren't excluded). The search panel seems like a great idea as it provides quick and easy access to run a search across all columns/data in the grid, but this limitation has some usability concerns (ex. instead of being able to tell end-users that the search panel searches the data in all columns, having to explain client-side processing vs. server-side processing, and string vs. non-string [dates, numbers, booleans, etc.] data and that only string data is searched when using server-side processing).

 

It would be great to know if built-in support will be coming at some point and all we have to do is wait. Otherwise, we'll likely have to either look into implementing one of these workarounds or possibly not use the search panel at all.

0
Aaron
Top achievements
Rank 1
Veteran
answered on 27 Aug 2020, 08:31 PM
Update (apparently I can't edit my previous post)... I submitted a feature request to address this limitation. Feel free to vote for it here: https://feedback.telerik.com/aspnet-core-ui/1482424-add-support-for-non-string-fields-in-grid-s-search-panel-with-serveroperation-true
0
Alex Hajigeorgieva
Telerik team
answered on 31 Aug 2020, 01:33 PM

Hello, Aaron,

Thank you for submitting the feature request. I will discuss it with the team and update it shortly.

Regards,
Alex Hajigeorgieva
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Grid
Asked by
Mbott
Top achievements
Rank 1
Answers by
Alex Hajigeorgieva
Telerik team
John
Top achievements
Rank 1
Veteran
Aaron
Top achievements
Rank 1
Veteran
Share this question
or