Declined
Last Updated: 07 Jan 2021 09:16 by ADMIN
René
Created on: 06 Jan 2021 12:57
Category: Grid
Type: Bug Report
1
Values of FilterMenu are not preserved in GridState.

Values of FilterMenu are not preserved in GridState. 

If FilterRow is used instead of FilterMenu then the values are preserved in GridState.

(I believe that Filtering was preserved with FilterMenu as well in some previous version but I could be wrong.)

Regards,

René

5 comments
ADMIN
Marin Bratanov
Posted on: 07 Jan 2021 09:16

Hi René,

Indeed, the System.Text.Json serializer is the only one we support, and that has been the case since it could work well - which is our 2.13.0 version which is mid-May 2020 (see more here).

That said, I am marking this as "Declined" because it seems it is not a bug in the components.

Regards,
Marin Bratanov
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/.

René
Posted on: 07 Jan 2021 09:11

For anyone having the same problem: You are probably using the following code

(which up to some point in time was provided at https://docs.telerik.com/blazor-ui/components/grid/state?_ga=2.133702286.1442066412.1587385045-1627434252.1580897464#information-in-the-grid-state

using Microsoft.JSInterop;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Threading.Tasks;
using Telerik.DataSource;

namespace Your.Namespace
{
     public class LocalStorageService : ILocalStorageService
    {
        protected IJSRuntime JsRuntimeInstance { get; set; }

        public LocalStorageService(IJSRuntime jsRuntime)
        {
            JsRuntimeInstance = jsRuntime;
        }

        public ValueTask SetItem(string key, object data)
        {
            return JsRuntimeInstance.InvokeVoidAsync("localStorage.setItem", new object[]
            {
                key,
                JsonConvert.SerializeObject(data)
            });
        }

        public async Task<T> GetItem<T>(string key)
        {
            var data = await JsRuntimeInstance.InvokeAsync<string>("localStorage.getItem", key);
            if (!string.IsNullOrEmpty(data))
            {
                return JsonConvert.DeserializeObject<T>(data, new JsonSerializerSettings()
                {
                    Converters = new JsonConverter[]
                    {
                        new FilterDescriptorJsonConverter()
                    },
                    NullValueHandling = NullValueHandling.Ignore
                });
            }

            return default;
        }

        public ValueTask RemoveItem(string key)
        {
            return JsRuntimeInstance.InvokeVoidAsync("localStorage.removeItem", key);
        }
    }

// to store the serialized grid state, we need to have a custom serialized
// based on Newtonsoft.Json serialization. In the future this may not be required
    public class FilterDescriptorJsonConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return objectType == typeof(FilterDescriptorBase);
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
            JsonSerializer serializer)
        {
            JObject filterDescriptor = JObject.Load(reader);

            return filterDescriptor.ToObject<FilterDescriptor>(serializer);
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
        }
    }
}

If you replace this code with the following, it will work again.

using Microsoft.JSInterop;
using System.Threading.Tasks;
using System.Text.Json;

namespace Your.Namespace
{
    public class LocalStorageService : ILocalStorageService
    {
        protected IJSRuntime JsRuntimeInstance { get; set; }

        public LocalStorageService(IJSRuntime jsRuntime)
        {
            JsRuntimeInstance = jsRuntime;
        }

        public ValueTask SetItem(string key, object data)
        {
            return JsRuntimeInstance.InvokeVoidAsync("localStorage.setItem", new object[]
            {
                key,
                JsonSerializer.Serialize(data)
            });
        }

        public async Task<T> GetItem<T>(string key)
        {
            var data = await JsRuntimeInstance.InvokeAsync<string>("localStorage.getItem", key);
            if (!string.IsNullOrEmpty(data))
            {
                return JsonSerializer.Deserialize<T>(data);
            }

            return default;
        }

        public ValueTask RemoveItem(string key)
        {
            return JsRuntimeInstance.InvokeVoidAsync("localStorage.removeItem", key);
        }
    }
}

Regards,

René

René
Posted on: 07 Jan 2021 08:57

Hello Marin,

I found the reason causing the problem.

My LocalStorageService (based on example code from Telerik) was using Newtonsoft.Json + Newtonsoft.Json.Linq + a FilterDescriptorJsonConverter based on JsonConverter.

Your provided code sample uses System.Text.Json instead and does not need the FilterDescriptorJsonConverter class anymore.

With those changes my project works as expected again.

With which release did the handling of the LocalStorage change?

Regards,

René

René
Posted on: 07 Jan 2021 07:14

Hello Marin,

upon watching your video, one obvious difference between your sample and our usecase is that all our tables where we use GridState have a column for expanding/closing the details for the entries.  Maybe the problem is connected to that?

I will try to reproduce the problem with your sample project, when I find the time.

Regards,

René

ADMIN
Marin Bratanov
Posted on: 06 Jan 2021 16:38

Hi René,

This seems to work fine for me. I am attaching a simple app I made to test this, and a short video of the correct behavior I get so you can compare against it and see what is the difference causing a problem, and whether I am missing something. If there is an issue in the Telerik components, could you modify my sample to showcase it and send it back to me so I can have a look?

Regards,
Marin Bratanov
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/.