Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataGrid: Mixes CurrentPage and PageChanged into single two-way bindable param #5896

Merged
merged 8 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions Demos/Blazorise.Demo/Pages/Tests/DataGrid/DataGridPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,8 @@
Responsive
ValidationsSummaryLabel="Following error occurs..."
CustomFilter="@OnCustomFilter"
PageSize="5"
CurrentPage="currentPage"
PageChanged="(e) => currentPage = e.Page"
@bind-PageSize="currentPageSize"
@bind-Page="currentPage"
FilteredDataChanged="@OnFilteredDataChanged"
UseValidation
SortChanged="@OnSortChanged"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public partial class DataGridPage

private DataGrid<Employee> dataGrid;
public int currentPage { get; set; } = 1;
public int currentPageSize { get; set; } = 5;

private bool editable = true;
private bool fixedHeader = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,23 @@
</UnorderedListItem>
</UnorderedList>
</OrderedListItem>
<OrderedListItem>
<Paragraph>
<Strong>DataGrid</Strong>
</Paragraph>
<UnorderedList>
<UnorderedListItem>
<Paragraph>
<Code>CurrentPage</Code> becomes <Code>Page</Code>, and this parameter supports two-way binding using <Code>@@bind-Page="currentPage"</Code>.
</Paragraph>
</UnorderedListItem>
<UnorderedListItem>
<Paragraph>
<Code>DataGridPageChangedEventArgs</Code> has been removed as it served no purpose. Now, the <Code>PageChanged</Code> event returns a simple <Code>int</Code> representing the current page number.
</Paragraph>
</UnorderedListItem>
</UnorderedList>
</OrderedListItem>
</OrderedList>
</Paragraph>

Expand All @@ -95,6 +112,15 @@
You may now allow multiple values to be selected in the <Code>DataGridSelectColumn</Code>. Set the new <Code>Multiple</Code> parameter to <Code>true</Code> to enable this feature. Please bind the corresponding array to successfully bind the multiple values.
</Paragraph>

<Heading Size="HeadingSize.Is4">
Two-Way Binding Support for the Page Parameter
</Heading>

<Paragraph>
The <Code>Page</Code> parameter now supports two-way binding, allowing you to synchronize it with a variable using <Code>@@bind-Page="currentPage"</Code>.
</Paragraph>


<NewsPageSubtitle>
Final Notes
</NewsPageSubtitle>
Expand Down
64 changes: 32 additions & 32 deletions Source/Extensions/Blazorise.DataGrid/DataGrid.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public async Task LoadState( DataGridState<TItem> dataGridState )
}

PageSize = dataGridState.PageSize;
CurrentPage = dataGridState.CurrentPage;
Page = dataGridState.CurrentPage;

if ( !dataGridState.ColumnDisplayingStates.IsNullOrEmpty() )
{
Expand Down Expand Up @@ -266,7 +266,7 @@ public Task<DataGridState<TItem>> GetState()
{
var dataGridState = new DataGridState<TItem>()
{
CurrentPage = CurrentPage,
CurrentPage = Page,
PageSize = PageSize,
EditState = EditState,
EditItem = editState == DataGridEditState.None ? default : editItem,
Expand Down Expand Up @@ -1031,7 +1031,7 @@ private async void OnPageChanged( int currentPage )
paginationContext.CancellationTokenSource?.Cancel();
paginationContext.CancellationTokenSource = new();

await InvokeAsync( () => PageChanged.InvokeAsync( new( currentPage, PageSize ) ) );
await InvokeAsync( () => PageChanged.InvokeAsync( currentPage ) );

await ReloadInternal( paginationContext.CancellationTokenSource.Token );
}
Expand Down Expand Up @@ -1139,9 +1139,9 @@ public async Task Delete( TItem item )

// When deleting and the page becomes empty and we aren't the first page:
// go to the previous page
if ( ManualReadMode && ShowPager && CurrentPage > paginationContext.FirstVisiblePage && !Data.Any() )
if ( ManualReadMode && ShowPager && Page > paginationContext.FirstVisiblePage && !Data.Any() )
{
await Paginate( ( CurrentPage - 1 ).ToString() );
await Paginate( ( Page - 1 ).ToString() );
}

await InvokeAsync( StateHasChanged );
Expand Down Expand Up @@ -1247,9 +1247,9 @@ protected internal async Task SaveBatch()
{
// When deleting and the page becomes empty and we aren't the first page:
// go to the previous page
if ( deletedItem && ShowPager && CurrentPage > paginationContext.FirstVisiblePage && !Data.Any() )
if ( deletedItem && ShowPager && Page > paginationContext.FirstVisiblePage && !Data.Any() )
{
await Paginate( ( CurrentPage - 1 ).ToString() );
await Paginate( ( Page - 1 ).ToString() );
}
else if ( newItem )
{
Expand Down Expand Up @@ -1588,31 +1588,31 @@ public Task Paginate( string paginationCommandOrNumber )
{
if ( int.TryParse( paginationCommandOrNumber, out var pageNumber ) )
{
CurrentPage = pageNumber;
Page = pageNumber;
}
else
{
if ( paginationCommandOrNumber == "prev" )
{
CurrentPage--;
Page--;

if ( CurrentPage < 1 )
CurrentPage = 1;
if ( Page < 1 )
Page = 1;
}
else if ( paginationCommandOrNumber == "next" )
{
CurrentPage++;
Page++;

if ( CurrentPage > paginationContext.LastPage )
CurrentPage = paginationContext.LastPage;
if ( Page > paginationContext.LastPage )
Page = paginationContext.LastPage;
}
else if ( paginationCommandOrNumber == "first" )
{
CurrentPage = 1;
Page = 1;
}
else if ( paginationCommandOrNumber == "last" )
{
CurrentPage = paginationContext.LastPage;
Page = paginationContext.LastPage;
}
}

Expand Down Expand Up @@ -2132,7 +2132,7 @@ protected async Task HandleReadData( CancellationToken cancellationToken )
await Task.Yield();

if ( !cancellationToken.IsCancellationRequested )
await ReadData.InvokeAsync( new DataGridReadDataEventArgs<TItem>( DataGridReadDataMode.Paging, Columns, SortByColumns, CurrentPage, PageSize, 0, 0, cancellationToken ) );
await ReadData.InvokeAsync( new DataGridReadDataEventArgs<TItem>( DataGridReadDataMode.Paging, Columns, SortByColumns, Page, PageSize, 0, 0, cancellationToken ) );
}
finally
{
Expand Down Expand Up @@ -2246,31 +2246,31 @@ protected Task OnPaginationItemClick( string pageName )
{
if ( int.TryParse( pageName, out var pageNumber ) )
{
CurrentPage = pageNumber;
Page = pageNumber;
}
else
{
if ( pageName == "prev" )
{
CurrentPage--;
Page--;

if ( CurrentPage < 1 )
CurrentPage = 1;
if ( Page < 1 )
Page = 1;
}
else if ( pageName == "next" )
{
CurrentPage++;
Page++;

if ( CurrentPage > paginationContext.LastPage )
CurrentPage = paginationContext.LastPage;
if ( Page > paginationContext.LastPage )
Page = paginationContext.LastPage;
}
else if ( pageName == "first" )
{
CurrentPage = 1;
Page = 1;
}
else if ( pageName == "last" )
{
CurrentPage = paginationContext.LastPage;
Page = paginationContext.LastPage;
}
}

Expand Down Expand Up @@ -2730,11 +2730,11 @@ private IEnumerable<TItem> FilterViewData()
// only use pagination if the custom data loading is not used
if ( !ManualReadMode && !Virtualize )
{
var skipElements = ( CurrentPage - 1 ) * PageSize;
var skipElements = ( Page - 1 ) * PageSize;
if ( skipElements > filteredData.Count )
{
CurrentPage = paginationContext.LastPage;
skipElements = ( CurrentPage - 1 ) * PageSize;
Page = paginationContext.LastPage;
skipElements = ( Page - 1 ) * PageSize;
}

return filteredData.Skip( skipElements ).Take( PageSize );
Expand Down Expand Up @@ -3384,7 +3384,7 @@ public IReadOnlyList<DataGridBatchEditItem<TItem>> BatchChanges
/// <summary>
/// Gets or sets the current page number.
/// </summary>
[Parameter] public int CurrentPage { get => paginationContext.CurrentPage; set => paginationContext.CurrentPage = value; }
[Parameter] public int Page { get => paginationContext.CurrentPage; set => paginationContext.CurrentPage = value; }

protected PaginationContext<TItem> PaginationContext => paginationContext;

Expand Down Expand Up @@ -3578,7 +3578,7 @@ public IReadOnlyList<DataGridBatchEditItem<TItem>> BatchChanges
/// <summary>
/// Occurs after the selected page has changed.
/// </summary>
[Parameter] public EventCallback<DataGridPageChangedEventArgs> PageChanged { get; set; }
[Parameter] public EventCallback<int> PageChanged { get; set; }

/// <summary>
/// Event handler used to load data manually based on the current page and filter data settings.
Expand Down Expand Up @@ -3867,7 +3867,7 @@ public int SelectedRowIndex
? selectedRowDataIdx
: ( selectedRowDataIdx == -1 )
? -1
: selectedRowDataIdx + ( CurrentPage - 1 ) * PageSize;
: selectedRowDataIdx + ( Page - 1 ) * PageSize;
}
}

Expand Down

This file was deleted.

Loading