Filtering Strings with IsEmpty and IsNull

13 posts, 0 answers
  1. Dan
    Dan avatar
    8 posts
    Member since:
    Sep 2010

    Posted 05 Dec 2014 Link to this post

    To most users a null string and an empty string is the same thing.  How can I reflect that in the grid filters?  Right now I have null values that aren't filtered when they select IsEmpty and vice versa. 

    Is there a nice way to have all my string columns have a filter IsNullOrWhitespace ?
  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 10 Dec 2014 Link to this post

    Hi,

    In order to achieve the desired result, you can apply two separate field filter conditions the data to be filtered on. You could define the Filtering criteria as explained in the Programmatic Filtering article. I am afraid there is not an IsNullOrWhitespace operator to suggest though.

    Regards,
    Dimitrina
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. Dan
    Dan avatar
    8 posts
    Member since:
    Sep 2010

    Posted 19 Dec 2014 in reply to Dimitrina Link to this post

    Thanks for the reply Dimitrina, the one problem I still find is intercepting the IsEmpty filter and properly replacing it with a composite filter.  I've tried catching the Filtering event and removing the IsEmpty and replacing it, but that didn't seem to work.  Tried the same in the Filtered event, didn't seem to work either. 

    So how I can intercept the users filter request and modify it?
  4. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 22 Dec 2014 Link to this post

    Hello,

    You cannot change the available IsEmpty option directly, what you can do is to build a condition to filter by both criteria. You can clear the current filtering criteria already defined or keep it and extend it also configuring new criteria for the respective ColumnFilterDescriptor (as illustrated in the article).

    Would you please share what have you tried? Would you please also add an image of what the final result you wish to achieve is? 

    Regards,
    Dimitrina
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  5. Dan
    Dan avatar
    8 posts
    Member since:
    Sep 2010

    Posted 06 Jan 2015 in reply to Dimitrina Link to this post

    So the closest solution I have found so far is on grid.Fitlering, set e.Cancel = true and add a composite filter to the grid.  This actually works, but has a number of drawbacks, one is that any other filters they applied (all be it, stupidly) will disappear. Also, when they open the Filter popup again it looks like no filters are applied and clicking Clear Filters doesn't do anything. To get around that I'll need to implement my own filter control to handle the clear properly.

    On Filtering I can catch that the user has requested an IsEmpty filter, and I can add the composite, but there seems to be no way to then remove just the IsEmpty FilterDescriptor.  The collections of Added and Removed don't actually do anything if you modify them, and since the FilterDescriptor is actually just a copy from the backing viewmodel, changing it doesn't actually change the filter. 
    So I'm left with "IsEmpty and (IsEmpty or Null)" as the filter criteria on that column.

    I understand how to create a composite filter to do what I want, my question is, how do I add that composite filter to the grid in such a way that it is seamless to the user when they click the Filter button.
  6. Dan
    Dan avatar
    8 posts
    Member since:
    Sep 2010

    Posted 06 Jan 2015 in reply to Dimitrina Link to this post

    I understand how to create the composite filter, my question is, how do I added it to the grid in a seamless manner when they click the Filter button?

    I've tried catching the Filtering event, looking for IsEmpty and then generating a composite filter, and that almost works.  However the IsEmpty fitler is still there and there seems to be no good way to remove it.  I can add my composite and set e.Cancel = true.  However if they selected "IsEmpty OR IsEqual to 'Blah'" then I'll have to piece that back together by hand which sounds risky. 

    I've also played around with inheriting from FilteringControl and injecting myself in the middle of the filter, but it always seems to fall apart with an internal method, or a property with no public setter.
  7. Dan
    Dan avatar
    8 posts
    Member since:
    Sep 2010

    Posted 06 Jan 2015 in reply to Dan Link to this post

    Sorry for the double post, the first one said it failed.
  8. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 07 Jan 2015 Link to this post

    Hello,

    You can always attach to the DistinctValuesLoading event of RadGridView and modify the ones that our data engine has calculated. You can also check the Display All Distinct Values article in our documentation for some sample code on using this event. As to defining a custom filtering control, you can find the Custom Filtering Controls article.

    In case this doesn't help, may I ask you to try isolating the issue in a demo project illustrating your exact setup and send it to us in a new support ticket? You can also take a look at this blog post for a reference on how to isolate an issue.  

    Regards,
    Dimitrina
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  9. Dan
    Dan avatar
    8 posts
    Member since:
    Sep 2010

    Posted 07 Jan 2015 in reply to Dimitrina Link to this post

    Little confused by your response about DistinctValuesLoading, I'm not concerned with DistinctValues.

    My question is, when the user presses Filter with the operator of IsEmpty, how do I have that actually filter by IsEmpty Or IsNull for strings.

    I have tried the code below, but I find it hard to see how I remove the existing FilterDescriptor that started the filtering in the first place.  Can you give me some guidance on that?

     void grid_Filtering(object sender, GridViewFilteringEventArgs e)
            {
                grid.FilterDescriptors.SuspendNotifications();
                foreach (var filter in e.Added)
                {
                    FilterDescriptor fd = (FilterDescriptor)filter;
                    if (fd.Operator == FilterOperator.IsEmpty)
                    {
                        MyFilterDescriptor composite = new MyFilterDescriptor(e.ColumnFilterDescriptor.Column);
                        composite.FilterDescriptors.Add(new FilterDescriptor { Operator = FilterOperator.IsEmpty, Member = fd.Member, MemberType = fd.MemberType, Value = fd.Value });
                        composite.FilterDescriptors.Add(new FilterDescriptor { Operator = FilterOperator.IsNull, Member = fd.Member, MemberType = fd.MemberType, Value = null });
                        composite.LogicalOperator = FilterCompositionLogicalOperator.Or;
                        grid.FilterDescriptors.Add(composite);
                        e.Cancel = true; //FALLS APART - If they have any other criteria selected along with the IsEmpty, I lose it. Help.
                    }
                }
                grid.FilterDescriptors.ResumeNotifications();
            }
  10. Dan
    Dan avatar
    8 posts
    Member since:
    Sep 2010

    Posted 19 Jan 2015 in reply to Dan Link to this post

    Any more advice or is this just entirely unsupported?
  11. Nick
    Admin
    Nick avatar
    593 posts

    Posted 22 Jan 2015 Link to this post

    Hello Dan,

    The approach that Dimitrina suggested is one of the ways to go with this. An alternative is to ensure that the null values are represented as string.Empty as well. There are a couple of ways you can do that. The first is to modify the get section in the string property to return string.Empty when the string is null. The second is to use a converter in the Binding which will display string.Empty if a null value is converted. 

    Hope this makes sense. 

    Regards,
    Nick
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  12. Dan
    Dan avatar
    8 posts
    Member since:
    Sep 2010

    Posted 22 Jan 2015 in reply to Nick Link to this post

    Thanks for the reply.  Our app has many grids, we have a behaviour that controls most of our logic and setup generically.  Was hoping for a solution that I could run out of it instead of modifying all my models. 

    The converter approach sounded promising but does not appear to work.  It messes up the distinct filters where it shows empty twice, and when I filter by IsEmpty from the drop down, it still doesn't include the one that was null.
  13. Boris
    Admin
    Boris avatar
    276 posts

    Posted 27 Jan 2015 Link to this post

    Hello Dan,

    I am sorry to say that there is no other alternative that we can offer, aside from Nick's suggestions.

    Regards,
    Boris
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top