Skip to content

Commit

Permalink
- Add .resizable class to header cell when ResizableColumns is true (#…
Browse files Browse the repository at this point in the history
…1606)

- Call enableColumnResizing when finished collecting columns
- Make enableColumnResizing callable from c# side
- Change SetTotalItemsCount to do both SetCurrentPageIndexAsync (if criteria met) and do regular work
  • Loading branch information
vnbaaij committed Mar 6, 2024
1 parent 42c61c9 commit 23a238f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/Core/Components/DataGrid/FluentDataGrid.razor
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
else
col.ShowSortIcon = false;

<FluentDataGridCell GridColumn=@(colIndex+1) CellType=DataGridCellType.ColumnHeader Class="@("column-header " + @ColumnHeaderClass(col))" Style="@col.Style" aria-sort="@AriaSortValue(col)" @key="@col" scope="col" TGridItem="TGridItem">
<FluentDataGridCell GridColumn=@(colIndex+1) CellType=DataGridCellType.ColumnHeader Class="@("column-header " + @ColumnHeaderClass(col) + (ResizableColumns ? " resizable" : ""))" Style="@col.Style" aria-sort="@AriaSortValue(col)" @key="@col" scope="col" TGridItem="TGridItem">
@col.HeaderContent
@if (col == _displayOptionsForColumn)
{
Expand Down
46 changes: 28 additions & 18 deletions src/Core/Components/DataGrid/FluentDataGrid.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,11 @@ private void StartCollectingColumns()
private void FinishCollectingColumns()
{
_collectingColumns = false;
_manualGrid = !_columns.Any();
_manualGrid = _columns.Count == 0;
if (ResizableColumns)
{
_ = Module?.InvokeVoidAsync("enableColumnResizing", _gridReference).AsTask();
}
}

/// <summary>
Expand Down Expand Up @@ -335,7 +339,6 @@ public async Task RefreshDataAsync()
public void SetLoadingState(bool loading)
{
Loading = loading;
StateHasChanged();
}

// Same as RefreshDataAsync, except without forcing a re-render. We use this from OnParametersSetAsync
Expand All @@ -358,17 +361,16 @@ private async Task RefreshDataCoreAsync()
{
// If we're not using Virtualize, we build and execute a request against the items provider directly
_lastRefreshedPaginationStateHash = Pagination?.GetHashCode();
int startIndex = Pagination is null ? 0 : (Pagination.CurrentPageIndex * Pagination.ItemsPerPage);
GridItemsProviderRequest<TGridItem> request = new GridItemsProviderRequest<TGridItem>(
var startIndex = Pagination is null ? 0 : (Pagination.CurrentPageIndex * Pagination.ItemsPerPage);
GridItemsProviderRequest<TGridItem> request = new(
startIndex, Pagination?.ItemsPerPage, _sortByColumn, _sortByAscending, thisLoadCts.Token);
GridItemsProviderResult<TGridItem> result = await ResolveItemsRequestAsync(request);
var result = await ResolveItemsRequestAsync(request);
if (!thisLoadCts.IsCancellationRequested)
{
_currentNonVirtualizedViewItems = result.Items;
_ariaBodyRowCount = _currentNonVirtualizedViewItems.Count;
Pagination?.SetTotalItemCountAsync(result.TotalItemCount);
_pendingDataLoadCancellationTokenSource = null;
Loading = false;
}
_internalGridContext.ResetRowIndexes(startIndex);
}
Expand All @@ -391,17 +393,17 @@ private async Task RefreshDataCoreAsync()
}

// Combine the query parameters from Virtualize with the ones from PaginationState
int startIndex = request.StartIndex;
int count = request.Count;
var startIndex = request.StartIndex;
var count = request.Count;
if (Pagination is not null)
{
startIndex += Pagination.CurrentPageIndex * Pagination.ItemsPerPage;
count = Math.Min(request.Count, Pagination.ItemsPerPage - request.StartIndex);
}

GridItemsProviderRequest<TGridItem> providerRequest = new GridItemsProviderRequest<TGridItem>(
GridItemsProviderRequest<TGridItem> providerRequest = new(
startIndex, count, _sortByColumn, _sortByAscending, request.CancellationToken);
GridItemsProviderResult<TGridItem> providerResult = await ResolveItemsRequestAsync(providerRequest);
var providerResult = await ResolveItemsRequestAsync(providerRequest);

if (!request.CancellationToken.IsCancellationRequested)
{
Expand Down Expand Up @@ -434,18 +436,23 @@ private async ValueTask<GridItemsProviderResult<TGridItem>> ResolveItemsRequestA
{
if (ItemsProvider is not null)
{
return await ItemsProvider(request);
var gipr = await ItemsProvider(request);
if (gipr.Items is not null)
{
Loading = false;
}
return gipr;
}
else if (Items is not null)
{
int totalItemCount = _asyncQueryExecutor is null ? Items.Count() : await _asyncQueryExecutor.CountAsync(Items);
var totalItemCount = _asyncQueryExecutor is null ? Items.Count() : await _asyncQueryExecutor.CountAsync(Items);
_ariaBodyRowCount = totalItemCount;
IQueryable<TGridItem>? result = request.ApplySorting(Items).Skip(request.StartIndex);
var result = request.ApplySorting(Items).Skip(request.StartIndex);
if (request.Count.HasValue)
{
result = result.Take(request.Count.Value);
}
TGridItem[]? resultArray = _asyncQueryExecutor is null ? result.ToArray() : await _asyncQueryExecutor.ToArrayAsync(result);
var resultArray = _asyncQueryExecutor is null ? [.. result] : await _asyncQueryExecutor.ToArrayAsync(result);
return GridItemsProviderResult.From(resultArray, totalItemCount);
}
else
Expand All @@ -466,7 +473,7 @@ private string AriaSortValue(ColumnBase<TGridItem> column)

private string? GridClass()
{
string? value = $"{Class} {(_pendingDataLoadCancellationTokenSource is null ? null : "loading")}".Trim();
var value = $"{Class} {(_pendingDataLoadCancellationTokenSource is null ? null : "loading")}".Trim();
if (string.IsNullOrEmpty(value))
{
return null;
Expand Down Expand Up @@ -518,10 +525,13 @@ private void CloseColumnOptions()

private async Task HandleOnRowFocusAsync(DataGridRowFocusEventArgs args)
{
string? rowId = args.RowId;
if (_internalGridContext.Rows.TryGetValue(rowId!, out FluentDataGridRow<TGridItem>? row))
var rowId = args.RowId;
if (_internalGridContext.Rows.TryGetValue(rowId!, out var row))
{
await OnRowFocus.InvokeAsync(row);
if (row != null && row.RowType == DataGridRowType.Default)
{
await OnRowFocus.InvokeAsync(row);
}
}
}
}
10 changes: 7 additions & 3 deletions src/Core/Components/DataGrid/FluentDataGrid.razor.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ export function checkColumnOptionsPosition(gridElement) {
}
}

function enableColumnResizing(gridElement) {
export function enableColumnResizing(gridElement) {
const columns = [];
let min = 50;
let headerBeingResized;
let resizeHandle;

gridElement.querySelectorAll('.column-header').forEach(header => {
gridElement.querySelectorAll('.column-header.resizable').forEach(header => {
columns.push({ header });
const onPointerMove = (e) => requestAnimationFrame(() => {
//console.log(`onPointerMove${headerBeingResized ? '' : ' [not resizing]'}`);
Expand All @@ -115,7 +115,11 @@ function enableColumnResizing(gridElement) {
// Set initial sizes
columns.forEach((column) => {
if (column.size === undefined) {
column.size = column.header.clientWidth + 'px';
if (column.header.clientWidth === undefined || column.header.clientWidth === 0) {
column.size = '50px';
} else {
column.size = column.header.clientWidth + 'px';
}
}
});

Expand Down
12 changes: 5 additions & 7 deletions src/Core/Components/Pagination/PaginationState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,11 @@ public Task SetTotalItemCountAsync(int totalItemCount)
{
// If the number of items has reduced such that the current page index is no longer valid, move
// automatically to the final valid page index and trigger a further data load.
return SetCurrentPageIndexAsync(LastPageIndex.Value);
}
else
{
// Under normal circumstances, we just want any associated pagination UI to update
TotalItemCountChanged?.Invoke(this, TotalItemCount);
return TotalItemCountChangedSubscribable.InvokeCallbacksAsync(this);
SetCurrentPageIndexAsync(LastPageIndex.Value);
}

// Under normal circumstances, we just want any associated pagination UI to update
TotalItemCountChanged?.Invoke(this, TotalItemCount);
return TotalItemCountChangedSubscribable.InvokeCallbacksAsync(this);
}
}

0 comments on commit 23a238f

Please sign in to comment.