Filter on selection

Can I filter on selection? 
I have an auto select functionality and would like to give the user the possibility to filter on the selected rows.

1 Reply 1 reply marked as answer

RN Rahul Narayanasamy Syncfusion Team May 13, 2021 01:38 PM UTC

Hi Iwan,                                                                           
 
Greetings from Syncfusion.  
 
Query: Filter on selection - would like to give the user the possibility to filter on the selected rows. 
 
We have validated your query and you want to apply filtering operation only for selected records. You can achieve your requirement by using Custom data binding. Here, we have perform filtering operation only for selected rows(if selected rows present) instead of all rows. If there is no selected records then we have perform filtering operation for all the records. Find the below code snippets and sample for your reference. 
 
 
<SfGrid @ref="Grid" TValue="Order" ID="Grid" AllowSorting="true" AllowSelection="true" AllowFiltering="true" AllowPaging="true"> 
    <SfDataManager AdaptorInstance="@typeof(CustomAdaptor)" Adaptor="Adaptors.CustomAdaptor"></SfDataManager> 
    <GridSelectionSettings Type="SelectionType.Multiple"></GridSelectionSettings> 
    <GridEvents OnActionBegin="ActionBeginHandler" TValue="Order"></GridEvents> 
    <GridPageSettings PageSize="8"></GridPageSettings> 
    <GridColumns> 
        <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="@TextAlign.Center" Width="140"></GridColumn> 
        <GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" Width="150"></GridColumn> 
        <GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" Width="150"></GridColumn> 
    </GridColumns> 
</SfGrid> 
 
@code{ 
 
    SfGrid<Order> Grid; 
    public static List<Order> Orders { get; set; } 
    static List<Order> Selected { get; set; } 
 
    . .  
    public async Task ActionBeginHandler(ActionEventArgs<Order> args) 
    { 
        // Here you can customize your code 
        if (args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.Filtering))     
        { 
            Selected = await Grid.GetSelectedRecords();     //get selected records 
        } 
    } 
 
 
    // Implementing custom adaptor by extending the DataAdaptor class 
    public class CustomAdaptor : DataAdaptor 
    { 
        // Performs data Read operation 
        public override object Read(DataManagerRequest dm, string key = null) 
        { 
            IEnumerable<Order> DataSource = Orders; 
            if (dm.Search != null && dm.Search.Count > 0) 
            { 
                // Searching 
                DataSource = DataOperations.PerformSearching(DataSource, dm.Search); 
            } 
            if (dm.Sorted != null && dm.Sorted.Count > 0) 
            { 
                // Sorting 
                DataSource = DataOperations.PerformSorting(DataSource, dm.Sorted); 
            } 
            if (dm.Where != null && dm.Where.Count > 0) 
            { 
                if(Selected.Count() > 0)    //check if any selected records present 
                { 
                    DataSource = DataOperations.PerformFiltering(Selected, dm.Where, dm.Where[0].Operator);   //perform filtering operation for selected data 
                } else 
                { 
                    DataSource = DataOperations.PerformFiltering(DataSource, dm.Where, dm.Where[0].Operator);    // else performed filtering operation for all records 
                } 
                // Filtering 
            } 
            . . . 
            return dm.RequiresCounts ? new DataResult() { Result = DataSource, Count = count } : (object)DataSource; 
        } 
    } 
} 
 
 
Reference: 
 
Please let us know if you have any concerns. 
 
Regards, 
Rahul 


Marked as answer
Loader.
Up arrow icon