How to apply a conditional persistent filter to Grid DataManager

I am using SyncFusion.Blazor v18.2.0.45

I am using SfGrid with the ODataV4Adapter for access to my remote OData API. My grid is linked to my Orders table in the OData API.
 
I have a scenario where if a logged in user is in a SysAdmin roll, I want to present the user with all data in the Order table. However, if the logged in user is not in the SysAdmin role, I want to force a persistent filter on the grid that limits the data to just orders that have an AccountId that matches the AccountId of the logged in user.

The AccountId is not actually displayed in the grid columns, currently, but is a part of the Orders table schema. The AccountID of the logged in user is held in a variable in the  *razor.cs

As an added requirement, the grid currently contains other columns that the user should be able to filter on regardless of the logged in user's role and when a user applies their filter to the grid, I display a "Clear Filter" button which, when clicked, calls the following method;.

        protected void HandleClearFiltersClick()
        {
            Grid.ClearFiltering();
            DateOfOrderFilter.Value = null;
            DateShippedFilter.Value = null;
            Grid.Query = new Query();
            ShowClearFilterButton = false;
        }

I have the logic to get the logged in user's role and AccountId. Those values are set in the *.cs file,  OnParametersSetAsync() method.

How can I force the grid data to be conditionally filtered by the logged in user's AccountId when the logged in user is not in the SysAdmin role and retain that filter if the user applies and/or clears their own filtering?


 

3 Replies 1 reply marked as answer

RS Renjith Singh Rajendran Syncfusion Team July 17, 2020 10:06 AM UTC

Hi David, 

Greetings from Syncfusion support. 

We suggest you to use the Query property of Grid. We have prepared a sample based on this scenario. Please download the sample form the link below, 
 
In the below code, we have used this Query property to pass the filter predicate to display the data in Grid based on your condition, when the log button is clicked. So now only the required data will only be shown in Grid.  

Now you can perform filtering for other Grid columns, with the values available in Grid. And on performing clear filtering the original filter predicate done using Query property will retain the original filter results in Grid. 

Please refer the codes below, 

<SfButton OnClick="onclick">Log as <b>Active</b> false</SfButton> 
<SfButton OnClick="ClearFilter">ClearFiltering</SfButton> 
 
<SfGrid @ref="Grid" AllowFiltering="true" TValue="CustomerViewModel" Query="@QueryData"> 
    <SfDataManager Url="http://localhost:64956/odata/books" Adaptor="Adaptors.ODataV4Adaptor"></SfDataManager> 
    <GridColumns> 
        ... 
        <GridColumn Field=@nameof(CustomerViewModel.Active) HeaderText="Active" Type="ColumnType.Boolean" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
    </GridColumns> 
</SfGrid> 
@code{ 
 
    public Query QueryData = new Query(); 
    SfGrid<CustomerViewModel> Grid { getset; }  
    public void onclick() 
    { 
        QueryData = new Query(); 
        var ColPre = new WhereFilter(); 
        List<WhereFilter> Predicate = new List<WhereFilter>(); 
        Predicate.Add(new WhereFilter() 
        { 
            Field = "Active", 
            value = false, 
            Operator = "equal", 
            IgnoreCase = true 
        }); 
        ColPre = WhereFilter.Or(Predicate); 
        QueryData = new Query().Where(ColPre); 
    } 
    public async Task ClearFilter() 
    { 
        await Grid.ClearFiltering(); 
    } 
    ... 
} 


Please get back to us if you need further assistance. 

Regards, 
Renjith Singh Rajendran 



DA David Adler July 17, 2020 02:10 PM UTC

The suggested solution solution is not working for me.

In my code, if the currently logged in user is not a member of the SysAdmin role, I get the AccountId claim of the current user and that AccountID value is what I want to filter the records in the grid on the initial display when the page is loaded and I want that filter to be retained regardless of if the user clicks the Clear Filter button. 

The AccountID is a GUID if that makes any difference. 

When I apply the code you suggested, I get no records in my grid even though I can see that the Value property of the WhereFilter contains the expected AccountID value and I have confirmed that in my remote data, there are 2 records with that AccountId value.

Since I want this to show on the initial list of records when the page is first loaded, I am applying the Query in the OnParametersSetAsync() method.

If I don't apply the query, I get all records displayed as before, so I know the grid is getting the data. 

Does the fact that I am trying to filter based upon an AccountID of the current logged in user (a GUID) against an AccountID value in my list of data that is also a GUID, using the "equal" operator?

Does it make a difference if I am applying the query in the OnParametersSetAsync method?

Your example project didn't have any actual data in it so when I ran it, so of course the list was empty regardless of the filter state so I couldn't actually see your example in action.

I have attached a zip of a text file that contains my current OnParameterSetAsync method, my SFGrid markup and my Order model for your review.

 






Attachment: SyncFusion_Files_7137f7b4.zip


RS Renjith Singh Rajendran Syncfusion Team July 22, 2020 08:40 AM UTC

Hi David, 

Thanks for your update. 

We suggest you to use the below code to Query the Grid’s data based on the Guid value. Please refer and use as like the below code, 

protected override async Task OnParametersSetAsync() 
{ 
    QueryData = new Query().Where("AccountId""equal"new Guid("9245fe4a-d402-451c-b9ed-9c1a04247281")); 
} 


We have modified the sample from our previous update based on your scenario. Please download the sample from the link below, 
 
In the above sample, based on the applied Query, Grid will be populated with only two records initially. We have used the Query inside the OnParametersSetAsync method.  

Query : Your example project didn't have any actual data in it so when I ran it, so of course the list was empty regardless of the filter state so I couldn't actually see your example in action. 
In our attached zip file we have included two project folders(BlazorApp1, ODataV4Service). Please run both the projects simultaneously to populate the Grid. One is the ODataV4 service and other one application has the Grid. So running both projects will only populate the data in Grid. 

Please get back to us if you need further assistance. 

Regards, 
Renjith Singh Rajendran 


Marked as answer
Loader.
Up arrow icon