Skip to content

Commit

Permalink
Add DataGrid ExpressionCompiler support for DataGridReadDataEventArgs…
Browse files Browse the repository at this point in the history
…<TItem>
  • Loading branch information
David-Moreira committed Nov 30, 2024
1 parent a1407b3 commit afd0bf4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 17 deletions.
12 changes: 2 additions & 10 deletions Demos/Blazorise.Demo/Pages/Tests/DataGrid/DataGridPage.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,21 +206,13 @@ private async Task OnReadData( DataGridReadDataEventArgs<Employee> e )

if ( !e.CancellationToken.IsCancellationRequested )
{
var query = dataModels.AsQueryable().ApplyDataGridSort( e.Columns ).ApplyDataGridSearch( e.Columns );
var query = dataModels.AsQueryable().ApplyDataGridSort( e ).ApplyDataGridSearch( e );

if ( dataGrid.CustomFilter is not null )
query = query.Where( item => item != null && dataGrid.CustomFilter( item ) );

var response = new List<Employee>();



if ( e.ReadDataMode is DataGridReadDataMode.Virtualize )
response = query.ApplyDataGridPaging( e.VirtualizeOffset + 1, e.VirtualizeCount ).ToList();
else if ( e.ReadDataMode is DataGridReadDataMode.Paging )
response = query.ApplyDataGridPaging( e.Page, e.PageSize ).ToList();
else
throw new Exception( "Unhandled ReadDataMode" );
response = query.ApplyDataGridPaging( e ).ToList();

await Task.Delay( random.Next( 100 ) );

Expand Down
67 changes: 60 additions & 7 deletions Source/Extensions/Blazorise.DataGrid/Utils/ExpressionCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public static IQueryable<TItem> ApplyDataGridFilters<TItem>( this IQueryable<TIt
/// Applies the search filter to the queryable data.
/// </summary>
/// <typeparam name="TItem"></typeparam>
/// <param name="data"></param>
/// <param name="dataGridColumns"></param>
/// <param name="data">The Data to be queried</param>
/// <param name="dataGridColumns">The DataGrid Columns Info</param>
/// <returns></returns>
public static IQueryable<TItem> ApplyDataGridSearch<TItem>( this IQueryable<TItem> data, IEnumerable<DataGridColumnInfo> dataGridColumns )
{
Expand Down Expand Up @@ -269,8 +269,8 @@ public static IQueryable<TItem> ApplyDataGridSearch<TItem>( this IQueryable<TIte
/// Applies the sort filter to the queryable data.
/// </summary>
/// <typeparam name="TItem"></typeparam>
/// <param name="data"></param>
/// <param name="dataGridColumns"></param>
/// <param name="data">The Data to be queried</param>
/// <param name="dataGridColumns">The DataGrid Columns Info</param>
/// <returns></returns>
public static IQueryable<TItem> ApplyDataGridSort<TItem>( this IQueryable<TItem> data, IEnumerable<DataGridColumnInfo> dataGridColumns )
{
Expand Down Expand Up @@ -311,9 +311,9 @@ public static IQueryable<TItem> ApplyDataGridSort<TItem>( this IQueryable<TItem>
/// Applies the paging filter to the queryable data.
/// </summary>
/// <typeparam name="TItem"></typeparam>
/// <param name="data"></param>
/// <param name="page"></param>
/// <param name="pageSize"></param>
/// <param name="data">The Data to be queried</param>
/// <param name="page">The current page</param>
/// <param name="pageSize">The page size</param>
/// <returns></returns>
public static IQueryable<TItem> ApplyDataGridPaging<TItem>( this IQueryable<TItem> data, int page, int pageSize )
{
Expand All @@ -325,6 +325,59 @@ public static IQueryable<TItem> ApplyDataGridPaging<TItem>( this IQueryable<TIte
return data;
}

/// <summary>
/// Applies all the DataGrid filters to the queryable data.
/// </summary>
/// <typeparam name="TItem">The TItem</typeparam>
/// <param name="data">The Data to be queried</param>
/// <param name="dataGridReadDataEventArgs">The DataGrid Read Data Event Arguments</param>
/// <returns></returns>
public static IQueryable<TItem> ApplyDataGridFilters<TItem>( this IQueryable<TItem> data, DataGridReadDataEventArgs<TItem> dataGridReadDataEventArgs )
{
return data.ApplyDataGridSort( dataGridReadDataEventArgs ).ApplyDataGridSearch( dataGridReadDataEventArgs ).ApplyDataGridPaging( dataGridReadDataEventArgs );
}

/// <summary>
/// Applies the search filter to the queryable data.
/// </summary>
/// <typeparam name="TItem"></typeparam>
/// <param name="data">The Data to be queried</param>
/// <param name="dataGridReadDataEventArgs">The DataGrid Read Data Event Arguments</param>
/// <returns></returns>
public static IQueryable<TItem> ApplyDataGridSearch<TItem>( this IQueryable<TItem> data, DataGridReadDataEventArgs<TItem> dataGridReadDataEventArgs )
{
return data.ApplyDataGridSearch( dataGridReadDataEventArgs.Columns );
}

/// <summary>
/// Applies the sort filter to the queryable data.
/// </summary>
/// <typeparam name="TItem"></typeparam>
/// <param name="data">The Data to be queried</param>
/// <param name="dataGridReadDataEventArgs">The DataGrid Read Data Event Arguments</param>
/// <returns></returns>
public static IQueryable<TItem> ApplyDataGridSort<TItem>( this IQueryable<TItem> data, DataGridReadDataEventArgs<TItem> dataGridReadDataEventArgs )
{
return data.ApplyDataGridSort( dataGridReadDataEventArgs.Columns );
}

/// <summary>
/// Applies the paging filter to the queryable data.
/// </summary>
/// <typeparam name="TItem"></typeparam>
/// <param name="data">The Data to be queried</param>
/// <param name="dataGridReadDataEventArgs">The DataGrid Read Data Event Arguments</param>
/// <returns></returns>
public static IQueryable<TItem> ApplyDataGridPaging<TItem>( this IQueryable<TItem> data, DataGridReadDataEventArgs<TItem> dataGridReadDataEventArgs )
{
if ( dataGridReadDataEventArgs.ReadDataMode is DataGridReadDataMode.Virtualize )
data = data.ApplyDataGridPaging( dataGridReadDataEventArgs.VirtualizeOffset + 1, dataGridReadDataEventArgs.VirtualizeCount );
else if ( dataGridReadDataEventArgs.ReadDataMode is DataGridReadDataMode.Paging )
data = data.ApplyDataGridPaging( dataGridReadDataEventArgs.Page, dataGridReadDataEventArgs.PageSize );

return data;
}

/// <summary>
/// Creates the lambda expression that is suitable for usage with Blazor <see cref="FieldIdentifier"/>.
/// </summary>
Expand Down

0 comments on commit afd0bf4

Please sign in to comment.