|
<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>
</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;
}
}
} |