Declined
Last Updated: 08 Mar 2021 12:02 by ADMIN
Liero
Created on: 17 Feb 2021 15:28
Category: Grid
Type: Bug Report
0
Setting filter programatically using SetState does not highlight FilterMenu icon
---

ADMIN EDIT

This is not a bug but behavior that was documented during the investigation of this case. Review the thread for more details.

---



asynct Task ContextMenuClick(ContextMenuItem) {
var state = Grid.GetState(); state.FilterDescriptors.Add(new FilterDescriptor { Member = "Created", Operator = FilterOperator.IsEqualTo, Value = "Administrator" }); await Grid.SetState(state);
}


 

How it should look like:

 


6 comments
ADMIN
Marin Bratanov
Posted on: 08 Mar 2021 12:02

Hi Liero,

The grid state is a collection of objects. The way the grid works with these objects is documented and it is up to you to manage those collection to use and achieve the different behaviors that are possible. We have even provided some sample extensions methods, and you are free to write your own in order to do what you want to do. We can't anticipate every application need and write methods for it, manipulating collections and data is something the application should be able to do.

 

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/.

Liero
Posted on: 08 Mar 2021 08:05
Ok I replaced my code with yours and it seems to work, thanks.

I still suggest that you provide an API that will do this for me. I have a feeling that I must assume too much of the TelerikGrid's internals.

If it helps, I've refactored some one the extension methods and added new resuable one:

        public static string? GetFilterMember(this IFilterDescriptor filter)
        {
            var filterDescriptor = filter;

            while (filterDescriptor is CompositeFilterDescriptor)
            {
                filterDescriptor = ((CompositeFilterDescriptor)filterDescriptor).FilterDescriptors?.ElementAtOrDefault(0);
            }

            return (filterDescriptor as FilterDescriptor)?.Member; //added save casting in case someting else than filter parameter is something else than FilterDescriptor or CompositeFilterDescriptor
        }

        public static async Task SetFilterWithReplaceInCollection<TItem>(this TelerikGrid<TItem> grid, FilterDescriptor filterDescriptor)
        {

            var state = grid.GetState();

            foreach (var existingFilter in state.FilterDescriptors.GetFiltersForMember(filterDescriptor.Member).OfType<FilterDescriptorBase>().ToArray())
            {
                state.FilterDescriptors.Remove(existingFilter);
            }

            CompositeFilterDescriptor theFilterDescriptor = new CompositeFilterDescriptor()
            {
                FilterDescriptors = new FilterDescriptorCollection()
                {
                    filterDescriptor,
                    new FilterDescriptor()
                    {
                        Member = filterDescriptor.Member,
                        Operator = FilterOperator.IsEqualTo,
                        Value = null,
                    }
                },
                LogicalOperator = FilterCompositionLogicalOperator.Or
            };

            state.FilterDescriptors.Add(theFilterDescriptor);

            await grid.SetState(state);
        }
ADMIN
Marin Bratanov
Posted on: 05 Mar 2021 13:57

Hi Liero,

I made a sample for you that mimics this setup and seems to work fine for me so you can compare against it. It is attached to the end of this post. I'm also attaching a short video of the expected behavior.

 

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/.

Liero
Posted on: 04 Mar 2021 14:50

Hi Martin,

although I managed to set the filter menu from code, I still don't get the filter icon highlighted:

Attached Files:
Liero
Posted on: 19 Feb 2021 09:00
Perfect, thanks!
ADMIN
Marin Bratanov
Posted on: 18 Feb 2021 18:01

Hello Daniel,

The grid has default filters and so adding some filters like that is not going to affect the UI - the grid uses the first filter, and that's the default one, to affect the UI. You need to either new up the entire filters collection, or to replace the current filter with the new one you want to build.

I've updated the documentation to explain this (PR link with the diff) and you can find that sample live here (it is the second snippet, but I also advise you review the first one for the correct types): https://docs.telerik.com/blazor-ui/components/grid/filter/filter-menu#filter-from-code.

On another note - you need to make sure to use the correct type of filter and to set its member type as well, see the examples above for that.

With that said, I am marking this as "Declined" because it is not a bug in the component, but expected behavior that I had not documented. It is now documented and you can use the sample as base for further development.

 

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/.