Skip to content

Commit

Permalink
add DataLoader to DataGrid, revert loading change
Browse files Browse the repository at this point in the history
  • Loading branch information
pwelter34 committed Aug 19, 2024
1 parent 13c2951 commit e1e1146
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 45 deletions.
14 changes: 6 additions & 8 deletions samples/Sample.Core/Pages/DataGrid/Examples/Banks.razor
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<h3>Banks</h3>

<DataGrid Data="Data" class="table table-hover" @ref="DataGrid" RowAttributes="RowAttributes" Filterable="true" Groupable="true">
<DataGrid DataLoader="LoadData" class="table table-hover" @ref="DataGrid" RowAttributes="RowAttributes" Filterable="true" Groupable="true">
<DataColumns>
<DataColumn TItem="Bank" Property="p => p.BankName" Grouping="true" SortIndex="0" />
<DataColumn TItem="Bank" Property="p => p.Id" Width="70px" data-test="testing" CellAttributes="CellAttributes" />
Expand All @@ -23,15 +23,8 @@


@code {
public ICollection<Bank> Data { get; set; } = new List<Bank>();

private DataGrid<Bank> DataGrid { get; set; }

protected async override Task OnInitializedAsync()
{
Data = await RandomDataClient.GetBanks();
}

private Dictionary<string, object> CellAttributes(Bank bank)
{
return new Dictionary<string, object>
Expand All @@ -47,4 +40,9 @@
{ "data-row-" + bank.Id, "Test " + bank.BankName }
};
}

private async Task<IEnumerable<Bank>> LoadData()
{
return await RandomDataClient.GetBanks();
}
}
36 changes: 29 additions & 7 deletions src/LoreSoft.Blazor.Controls/Data/DataComponentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ public abstract class DataComponentBase<TItem> : ComponentBase, IDisposable
private DataProviderDelegate<TItem> _dataProvider;
private CancellationTokenSource _refreshCancellation;
private IEnumerable<TItem> _data;
private bool _isLoading;

[Parameter]
public IEnumerable<TItem> Data { get; set; }

[Parameter]
public DataProviderDelegate<TItem> DataProvider { get; set; }

[Parameter]
public Func<Task<IEnumerable<TItem>>> DataLoader { get; set; }


[Parameter]
public RenderFragment<DataComponentBase<TItem>> DataToolbar { get; set; }
Expand Down Expand Up @@ -43,7 +47,18 @@ public abstract class DataComponentBase<TItem> : ComponentBase, IDisposable
public int VirtualOverscan { get; set; } = 3;


public bool IsLoading { get; set; }
public bool IsLoading
{
get => _isLoading;
set
{
if (_isLoading == value)
return;

_isLoading = value;
StateHasChanged();
}
}

public DataPagerState Pager { get; } = new();

Expand Down Expand Up @@ -77,7 +92,7 @@ protected override async Task OnParametersSetAsync()
{
if (DataProvider != null)
{
if (Data != null)
if (Data != null || DataLoader != null)
{
throw new InvalidOperationException(
$"Component can only accept one item source from its parameters. " +
Expand All @@ -97,6 +112,10 @@ protected override async Task OnParametersSetAsync()
await RefreshAsync();
}
}
else if (DataLoader != null)
{
_dataProvider = DefaultProvider;
}
else
{
throw new InvalidOperationException(
Expand Down Expand Up @@ -177,12 +196,15 @@ public virtual DataRequest CreateDataRequest(CancellationToken cancellationToken
}

// used when Data is set directly
protected virtual ValueTask<DataResult<TItem>> DefaultProvider(DataRequest request)
protected virtual async ValueTask<DataResult<TItem>> DefaultProvider(DataRequest request)
{
if (Data == null || !Data.Any())
return ValueTask.FromResult(new DataResult<TItem>(0, Enumerable.Empty<TItem>()));
if (_data == null && DataLoader != null)
_data = await DataLoader();

if (_data == null || !_data.Any())
return new DataResult<TItem>(0, []);

var query = Data.AsQueryable();
var query = _data.AsQueryable();

query = FilterData(query, request);

Expand All @@ -191,7 +213,7 @@ protected virtual ValueTask<DataResult<TItem>> DefaultProvider(DataRequest reque
var sorted = SortData(query, request);
sorted = PageData(sorted, request);

return ValueTask.FromResult(new DataResult<TItem>(total, sorted));
return new DataResult<TItem>(total, sorted);
}

protected virtual IQueryable<TItem> PageData(IQueryable<TItem> queryable, DataRequest request)
Expand Down
60 changes: 30 additions & 30 deletions src/LoreSoft.Blazor.Controls/Data/DataGrid.razor
Original file line number Diff line number Diff line change
Expand Up @@ -147,40 +147,29 @@
</td>
</tr>
}
else if (View == null || IsLoading)
else if (View == null)
{
@if (LoadingTemplate != null)
for (int i = 0; i < 5; i++)
{
<tr>
<td colspan="@CellCount">
@LoadingTemplate
</td>
<tr @key="i">
@if (DetailTemplate != null || HasGrouping)
{
<td class="data-grid-hierarchy-cell">
</td>
}
@if (Selectable)
{
<td class="data-grid-selector-cell">
</td>
}
@foreach (var column in VisibleColumns)
{
<td @key="column" class="@column.ClassName">
<Skeleton />
</td>
}
</tr>
}
else
{
for (int i = 0; i < 5; i++)
{
<tr @key="i">
@if (DetailTemplate != null || HasGrouping)
{
<td class="data-grid-hierarchy-cell">
</td>
}
@if (Selectable)
{
<td class="data-grid-selector-cell">
</td>
}
@foreach (var column in VisibleColumns)
{
<td @key="column" class="@column.ClassName">
<Skeleton />
</td>
}
</tr>
}
}
}
else if (View.Count == 0)
{
Expand Down Expand Up @@ -292,6 +281,17 @@
</div>
</CascadingValue>
}
@if (IsLoading)
{
@if (LoadingTemplate == null)
{
<LoadingBlock IsLoading="true" />
}
else
{
@LoadingTemplate
}
}
</div>
</CascadingValue>

Expand Down

0 comments on commit e1e1146

Please sign in to comment.