Deserialize Grid Filter into object

4 posts, 0 answers
  1. Jim
    Jim avatar
    6 posts
    Member since:
    Apr 2007

    Posted 28 Jun Link to this post

    Each user is able to store grid filters in the database as a JSON string for use later.

    We now have to iterate through these filters on the server to do some analytics with what the user is filtering the grid on. Is there a way in C# using Kendo.Mvc namespace to deserialize the JSON string stored into the database back into an IList<IFilterDescriptor> Filters? 

  2. Danail Vasilev
    Admin
    Danail Vasilev avatar
    1494 posts

    Posted 30 Jun Link to this post

    Hi Jim,

    You can use the approach from the Binding to a Web ApiController code-library to parse them. For example:
    Copy Code
    public DataSourceRequest ParseRequest(int? page, int? pageSize, string sort, stringgroup, string filterstring aggregates)
    {
        DataSourceRequest request = new DataSourceRequest();
      
        if (page.HasValue)
        {
            request.Page = page.Value;
        }
      
        if (pageSize.HasValue)
        {
            request.PageSize = pageSize.Value;
        }
      
        if (!string.IsNullOrEmpty(sort))
        {
            request.Sorts = GridDescriptorSerializer.Deserialize<SortDescriptor>(sort);
        }
      
        if (!string.IsNullOrEmpty(filter))
        {
            request.Filters = FilterDescriptorFactory.Create(filter);
        }
      
        if (!string.IsNullOrEmpty(group))
        {
            request.Groups = GridDescriptorSerializer.Deserialize<GroupDescriptor>(group);
        }
      
        if (!string.IsNullOrEmpty(aggregates))
        {
            request.Aggregates = GridDescriptorSerializer.Deserialize<AggregateDescriptor>(aggregates);
        }
      
        return request;


    Regards,
    Danail Vasilev
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Jim
    Jim avatar
    6 posts
    Member since:
    Apr 2007

    Posted 30 Jun Link to this post

    The only problem with that solution is that the filter that comes through using the .net wrapper for kendo is structured this way

        UnitsInStock~eq~3~and~(ProductName~eq~'chai'~and~ProductName~eq~'123')

    and using the JavaScript library the filter is structured and stored in the database this way

        "{\"filters\":[{\"value\":\"Accident Response Fee Laws\",\"operator\":\"eq\",\"field\":\"subTopic\"}],\"logic\":\"or\"}"

        "{\"filters\":[{\"logic\":\"or\",\"filters\":[{\"value\":\"Accident Response Fee Laws\",\"operator\":\"eq\",\"field\":\"subTopic\"}]},{\"value\":\"Auto Insurance\",\"operator\":\"eq\",\"field\":\"topic\"},{\"field\":\"dateLastReviewed\",\"operator\":\"gt\",\"value\":\"2010-06-28T04:00:00.000Z\"}],\"logic\":\"and\"}"

    Using the FilterDescriptorFactory on the json strings above, results in a FilterParserException: Expected token. Both of theses filters are passed in javascript to the controller by JSON.stringify(grid.dataSource.filter()); to be stored in the database for saved presets of the grid. Is there anyway to deserialize the above JSON into a list of FilterDescriptors or will I have to write a custom JsonConverter

  5. Danail Vasilev
    Admin
    Danail Vasilev avatar
    1494 posts

    Posted 04 Jul Link to this post

    Hi Jim,

    You can also use the grid's build-in parameterMap function to format correctly the needed options from the DataSource - http://www.telerik.com/forums/send-datasourcerequest-with-javascript-function#uvDmkKokjUeqIexSx5LnQg

    Regards,
    Danail Vasilev
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
UI for ASP.NET MVC is VS 2017 Ready