From e248d6965bdcc799e8f30fa0971fceda37a3b207 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Tesa=C5=99?= <33880579+tesar-tech@users.noreply.github.com> Date: Thu, 19 Dec 2024 11:00:13 +0000 Subject: [PATCH] DataGrid: Mixes CurrentPage and PageChanged into single two-way bindable param (#5896) * [DataGrid] Mixes CurrentPage and PageChanged into single two-way bindable Page parameter; Fixes pagesize "bug" in datagrid test page; Removes unnecessary DataGridPageChangedEventArgs.cs * Rename currentPage on PaginationContext.cs * From CurrentPage to Page in DataGridState.cs * Move getter properties * Adjust release notes title * Move PageChanged --------- Co-authored-by: Mladen Macanovic --- .../Pages/Tests/DataGrid/DataGridPage.razor | 5 +- .../Tests/DataGrid/DataGridPage.razor.cs | 1 + .../Pages/Tests/DataGrid/PagerPage.razor | 4 +- .../Tests/DataGrid/StateManagementPage.razor | 2 +- .../Examples/DataGridPagerExample.razor | 4 +- .../DataGridStateManagementExample.razor | 2 +- .../News/2025-05-01-release-notes-200.razor | 27 ++++++ .../Contexts/PaginationContext.cs | 66 +++++++------- .../Blazorise.DataGrid/DataGrid.razor.cs | 86 ++++++++++--------- .../Blazorise.DataGrid/DataGridState.cs | 2 +- .../DataGridPageChangedEventArgs.cs | 32 ------- .../_DataGridPagination.razor | 20 ++--- 12 files changed, 126 insertions(+), 125 deletions(-) delete mode 100644 Source/Extensions/Blazorise.DataGrid/EventArguments/DataGridPageChangedEventArgs.cs diff --git a/Demos/Blazorise.Demo/Pages/Tests/DataGrid/DataGridPage.razor b/Demos/Blazorise.Demo/Pages/Tests/DataGrid/DataGridPage.razor index 6feea6e1d9..cb93e7560e 100644 --- a/Demos/Blazorise.Demo/Pages/Tests/DataGrid/DataGridPage.razor +++ b/Demos/Blazorise.Demo/Pages/Tests/DataGrid/DataGridPage.razor @@ -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" diff --git a/Demos/Blazorise.Demo/Pages/Tests/DataGrid/DataGridPage.razor.cs b/Demos/Blazorise.Demo/Pages/Tests/DataGrid/DataGridPage.razor.cs index a919ed91d4..ab6aa4e225 100644 --- a/Demos/Blazorise.Demo/Pages/Tests/DataGrid/DataGridPage.razor.cs +++ b/Demos/Blazorise.Demo/Pages/Tests/DataGrid/DataGridPage.razor.cs @@ -28,6 +28,7 @@ public partial class DataGridPage private DataGrid dataGrid; public int currentPage { get; set; } = 1; + public int currentPageSize { get; set; } = 5; private bool editable = true; private bool fixedHeader = false; diff --git a/Demos/Blazorise.Demo/Pages/Tests/DataGrid/PagerPage.razor b/Demos/Blazorise.Demo/Pages/Tests/DataGrid/PagerPage.razor index 18b923499b..2cab8418a8 100644 --- a/Demos/Blazorise.Demo/Pages/Tests/DataGrid/PagerPage.razor +++ b/Demos/Blazorise.Demo/Pages/Tests/DataGrid/PagerPage.razor @@ -50,7 +50,7 @@ @context.TotalItems - @for ( int i = context.FirstVisiblePage; i <= context.LastVisiblePage; ++i ) { var pageNumber = i; @@ -59,7 +59,7 @@ - @foreach ( var curPageSize in context.PageSizes ) { @curPageSize diff --git a/Demos/Blazorise.Demo/Pages/Tests/DataGrid/StateManagementPage.razor b/Demos/Blazorise.Demo/Pages/Tests/DataGrid/StateManagementPage.razor index c8672602dc..25ff0f8597 100644 --- a/Demos/Blazorise.Demo/Pages/Tests/DataGrid/StateManagementPage.razor +++ b/Demos/Blazorise.Demo/Pages/Tests/DataGrid/StateManagementPage.razor @@ -94,7 +94,7 @@ await LocalStorage.RemoveItemAsync( STORAGE_KEY ); var state = new DataGridState() { - CurrentPage = 1, + Page = 1, PageSize = 10, }; await dataGridRef.LoadState( state ); diff --git a/Documentation/Blazorise.Docs/Pages/Docs/Extensions/DataGrid/Examples/DataGridPagerExample.razor b/Documentation/Blazorise.Docs/Pages/Docs/Extensions/DataGrid/Examples/DataGridPagerExample.razor index a3a707efa0..5088fab8d4 100644 --- a/Documentation/Blazorise.Docs/Pages/Docs/Extensions/DataGrid/Examples/DataGridPagerExample.razor +++ b/Documentation/Blazorise.Docs/Pages/Docs/Extensions/DataGrid/Examples/DataGridPagerExample.razor @@ -33,7 +33,7 @@ @context.TotalItems - @for ( int i = context.FirstVisiblePage; i <= context.LastVisiblePage; ++i ) { var pageNumber = i; @@ -42,7 +42,7 @@ - @foreach ( var curPageSize in context.PageSizes ) { @curPageSize diff --git a/Documentation/Blazorise.Docs/Pages/Docs/Extensions/DataGrid/Examples/DataGridStateManagementExample.razor b/Documentation/Blazorise.Docs/Pages/Docs/Extensions/DataGrid/Examples/DataGridStateManagementExample.razor index 0ff0ad2433..f4e06005c2 100644 --- a/Documentation/Blazorise.Docs/Pages/Docs/Extensions/DataGrid/Examples/DataGridStateManagementExample.razor +++ b/Documentation/Blazorise.Docs/Pages/Docs/Extensions/DataGrid/Examples/DataGridStateManagementExample.razor @@ -72,7 +72,7 @@ var state = new DataGridState() { - CurrentPage = 1, + Page = 1, PageSize = 10, }; diff --git a/Documentation/Blazorise.Docs/Pages/News/2025-05-01-release-notes-200.razor b/Documentation/Blazorise.Docs/Pages/News/2025-05-01-release-notes-200.razor index 7d601f2206..1a9f742e49 100644 --- a/Documentation/Blazorise.Docs/Pages/News/2025-05-01-release-notes-200.razor +++ b/Documentation/Blazorise.Docs/Pages/News/2025-05-01-release-notes-200.razor @@ -76,6 +76,25 @@ + + + DataGrid + + + + + CurrentPage becomes Page, and this parameter supports two-way binding using @@bind-Page="currentPage". + The rename also happened on PaginationContext properties CurrentPageSize and CurrentPage. They have now been renamed to PageSize and Page, respectively. + DataGridState.CurrentPage has been also renamed to Page. + + + + + DataGridPageChangedEventArgs has been removed as it served no purpose. Now, the PageChanged event returns a simple int representing the current page number. + + + + Renaming RightAligned to EndAligned in Dropdown, and DropdownList @@ -107,6 +126,14 @@ You may now allow multiple values to be selected in the DataGridSelectColumn. Set the new Multiple parameter to true to enable this feature. Please bind the corresponding array to successfully bind the multiple values. + + Two-Way Binding Page Parameter + + + + The Page parameter now supports two-way binding, allowing you to synchronize it with a variable using @@bind-Page="currentPage". + + Final Notes diff --git a/Source/Extensions/Blazorise.DataGrid/Contexts/PaginationContext.cs b/Source/Extensions/Blazorise.DataGrid/Contexts/PaginationContext.cs index 0c9ec13ed0..f768d63226 100644 --- a/Source/Extensions/Blazorise.DataGrid/Contexts/PaginationContext.cs +++ b/Source/Extensions/Blazorise.DataGrid/Contexts/PaginationContext.cs @@ -11,13 +11,13 @@ public class PaginationContext { #region Members - private event CurrentPageChangedEventHandler CurrentPageChanged; + private event PageChangedEventHandler PageChanged; - public delegate void CurrentPageChangedEventHandler( int value ); + public delegate void PageChangedEventHandler( int value ); - private event CurrentPageSizeChangedEventHandler CurrentPageSizeChanged; + private event PageSizeChangedEventHandler PageSizeChanged; - public delegate void CurrentPageSizeChangedEventHandler( int value ); + public delegate void PageSizeChangedEventHandler( int value ); private event TotalItemsChangedEventHandler TotalItemsChanged; @@ -27,9 +27,9 @@ public class PaginationContext private int lastVisiblePage; - private int currentPage = 1; + private int page = 1; - private int currentPageSize = 10; + private int pageSize = 10; private int? totalItems; @@ -48,34 +48,34 @@ public PaginationContext( DataGrid parentDataGrid ) #region Methods - public void SubscribeOnPageChanged( CurrentPageChangedEventHandler listener ) + public void SubscribeOnPageChanged( PageChangedEventHandler listener ) { - CurrentPageChanged += listener; + PageChanged += listener; } - public void UnsubscribeOnPageChanged( CurrentPageChangedEventHandler listener ) + public void UnsubscribeOnPageChanged( PageChangedEventHandler listener ) { - CurrentPageChanged -= listener; + PageChanged -= listener; } - public void TriggerCurrentPageChange( int value ) + public void TriggerPageChange( int value ) { - CurrentPageChanged?.Invoke( value ); + PageChanged?.Invoke( value ); } - public void SubscribeOnPageSizeChanged( CurrentPageSizeChangedEventHandler listener ) + public void SubscribeOnPageSizeChanged( PageSizeChangedEventHandler listener ) { - CurrentPageSizeChanged += listener; + PageSizeChanged += listener; } - public void UnsubscribeOnPageSizeChanged( CurrentPageSizeChangedEventHandler listener ) + public void UnsubscribeOnPageSizeChanged( PageSizeChangedEventHandler listener ) { - CurrentPageSizeChanged -= listener; + PageSizeChanged -= listener; } - public void TriggerCurrentPageSizeChange( int value ) + public void TriggerPageSizeChange( int value ) { - CurrentPageSizeChanged?.Invoke( value ); + PageSizeChanged?.Invoke( value ); } public void SubscribeOnTotalItemsChanged( TotalItemsChangedEventHandler listener ) @@ -100,8 +100,8 @@ private void CalculateFirstAndLastVisiblePage() { var step = (int)Math.Floor( MaxPaginationLinks / 2d ); - var leftButton = CurrentPage - step; - var rightButton = CurrentPage + step; + var leftButton = Page - step; + var rightButton = Page + step; if ( leftButton <= 1 ) { @@ -132,15 +132,15 @@ private void CalculateFirstAndLastVisiblePage() /// /// Gets or sets the current page /// - public int CurrentPage + public int Page { - get => currentPage; + get => page; set { - if ( currentPage != value ) + if ( page != value ) { - currentPage = value; - TriggerCurrentPageChange( value ); + page = value; + TriggerPageChange( value ); } } } @@ -152,10 +152,10 @@ public int LastPage { get { - var lastPage = Math.Max( (int)Math.Ceiling( ( TotalItems ?? 0 ) / (double)currentPageSize ), 1 ); + var lastPage = Math.Max( (int)Math.Ceiling( ( TotalItems ?? 0 ) / (double)pageSize ), 1 ); - if ( CurrentPage > lastPage ) - CurrentPage = lastPage; + if ( Page > lastPage ) + Page = lastPage; return lastPage; } @@ -190,15 +190,15 @@ public int LastVisiblePage /// /// Gets or sets the current page size /// - public int CurrentPageSize + public int PageSize { - get => currentPageSize; + get => pageSize; set { - if ( currentPageSize != value ) + if ( pageSize != value ) { - currentPageSize = value; - TriggerCurrentPageSizeChange( value ); + pageSize = value; + TriggerPageSizeChange( value ); } } } diff --git a/Source/Extensions/Blazorise.DataGrid/DataGrid.razor.cs b/Source/Extensions/Blazorise.DataGrid/DataGrid.razor.cs index 704ee6fd83..58e48521df 100644 --- a/Source/Extensions/Blazorise.DataGrid/DataGrid.razor.cs +++ b/Source/Extensions/Blazorise.DataGrid/DataGrid.razor.cs @@ -187,7 +187,7 @@ public async Task LoadState( DataGridState dataGridState ) } PageSize = dataGridState.PageSize; - CurrentPage = dataGridState.CurrentPage; + Page = dataGridState.Page; if ( !dataGridState.ColumnDisplayingStates.IsNullOrEmpty() ) { @@ -266,7 +266,7 @@ public Task> GetState() { var dataGridState = new DataGridState() { - CurrentPage = CurrentPage, + Page = Page, PageSize = PageSize, EditState = EditState, EditItem = editState == DataGridEditState.None ? default : editItem, @@ -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 ); } @@ -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 ); @@ -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 ) { @@ -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; } } @@ -2132,7 +2132,7 @@ protected async Task HandleReadData( CancellationToken cancellationToken ) await Task.Yield(); if ( !cancellationToken.IsCancellationRequested ) - await ReadData.InvokeAsync( new DataGridReadDataEventArgs( DataGridReadDataMode.Paging, Columns, SortByColumns, CurrentPage, PageSize, 0, 0, cancellationToken ) ); + await ReadData.InvokeAsync( new DataGridReadDataEventArgs( DataGridReadDataMode.Paging, Columns, SortByColumns, Page, PageSize, 0, 0, cancellationToken ) ); } finally { @@ -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; } } @@ -2730,11 +2730,11 @@ private IEnumerable 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 ); @@ -3195,6 +3195,16 @@ private bool IsMultiSelectAllIndeterminate /// internal bool HasValidationsSummary => ShowValidationsSummary && ValidationsSummaryErrors?.Length > 0; + /// + /// Gets the pagination context. + /// + protected PaginationContext PaginationContext => paginationContext; + + /// + /// Gets the pagination templates. + /// + protected PaginationTemplates PaginationTemplates => paginationTemplates; + /// /// Gets or sets the datagrid data-source. /// @@ -3384,11 +3394,12 @@ public IReadOnlyList> BatchChanges /// /// Gets or sets the current page number. /// - [Parameter] public int CurrentPage { get => paginationContext.CurrentPage; set => paginationContext.CurrentPage = value; } - - protected PaginationContext PaginationContext => paginationContext; + [Parameter] public int Page { get => paginationContext.Page; set => paginationContext.Page = value; } - protected PaginationTemplates PaginationTemplates => paginationTemplates; + /// + /// Occurs after the selected page has changed. + /// + [Parameter] public EventCallback PageChanged { get; set; } /// /// Gets or sets content of table body for empty DisplayData. @@ -3473,7 +3484,7 @@ public IReadOnlyList> BatchChanges /// /// Gets or sets the maximum number of items for each page. /// - [Parameter] public int PageSize { get => paginationContext.CurrentPageSize; set => paginationContext.CurrentPageSize = value; } + [Parameter] public int PageSize { get => paginationContext.PageSize; set => paginationContext.PageSize = value; } /// /// Occurs after the has changed. @@ -3575,11 +3586,6 @@ public IReadOnlyList> BatchChanges /// [Parameter] public bool RowContextMenuPreventDefault { get; set; } - /// - /// Occurs after the selected page has changed. - /// - [Parameter] public EventCallback PageChanged { get; set; } - /// /// Event handler used to load data manually based on the current page and filter data settings. /// @@ -3867,7 +3873,7 @@ public int SelectedRowIndex ? selectedRowDataIdx : ( selectedRowDataIdx == -1 ) ? -1 - : selectedRowDataIdx + ( CurrentPage - 1 ) * PageSize; + : selectedRowDataIdx + ( Page - 1 ) * PageSize; } } diff --git a/Source/Extensions/Blazorise.DataGrid/DataGridState.cs b/Source/Extensions/Blazorise.DataGrid/DataGridState.cs index 49d312571d..139e02a94b 100644 --- a/Source/Extensions/Blazorise.DataGrid/DataGridState.cs +++ b/Source/Extensions/Blazorise.DataGrid/DataGridState.cs @@ -120,7 +120,7 @@ private static string ExtractFieldName( Expression> /// /// Gets or sets the current page number. /// - public int CurrentPage { get; set; } + public int Page { get; set; } /// /// Gets or sets the maximum number of items for each page. diff --git a/Source/Extensions/Blazorise.DataGrid/EventArguments/DataGridPageChangedEventArgs.cs b/Source/Extensions/Blazorise.DataGrid/EventArguments/DataGridPageChangedEventArgs.cs deleted file mode 100644 index f8f6c8cd15..0000000000 --- a/Source/Extensions/Blazorise.DataGrid/EventArguments/DataGridPageChangedEventArgs.cs +++ /dev/null @@ -1,32 +0,0 @@ -#region Using directives -using System; -#endregion - -namespace Blazorise.DataGrid; - -/// -/// Provides the data for datagrid page change. -/// -public class DataGridPageChangedEventArgs : EventArgs -{ - /// - /// Initializes a new instance of page changed event argument. - /// - /// Page number at the moment of initialization. - /// Maximum number of items per page. - public DataGridPageChangedEventArgs( int page, int pageSize ) - { - Page = page; - PageSize = pageSize; - } - - /// - /// Gets the requested page number. - /// - public int Page { get; } - - /// - /// Gets the max number of items requested by page. - /// - public int PageSize { get; } -} \ No newline at end of file diff --git a/Source/Extensions/Blazorise.DataGrid/_DataGridPagination.razor b/Source/Extensions/Blazorise.DataGrid/_DataGridPagination.razor index db3f697e37..f29f939471 100644 --- a/Source/Extensions/Blazorise.DataGrid/_DataGridPagination.razor +++ b/Source/Extensions/Blazorise.DataGrid/_DataGridPagination.razor @@ -161,8 +161,8 @@ } else { - var curStart = ( ( PaginationContext.CurrentPage - 1 ) * PaginationContext.CurrentPageSize + 1 ); - var curEnd = Math.Min( PaginationContext.CurrentPage * PaginationContext.CurrentPageSize, totalItems ); + var curStart = ( ( PaginationContext.Page - 1 ) * PaginationContext.PageSize + 1 ); + var curEnd = Math.Min( PaginationContext.Page * PaginationContext.PageSize, totalItems ); @Localizer.Localize( ParentDataGrid.Localizers?.NumbersOfItemsLocalizer, "{0} - {1} of {2} items", curStart, curEnd, totalItems ) } } @@ -186,7 +186,7 @@ @if ( ShowPager && !ParentDataGrid.Virtualize ) { - + @if ( FirstPageButtonTemplate is not null ) { @@ -198,7 +198,7 @@ } - + @if ( PreviousPageButtonTemplate is not null ) { @@ -214,9 +214,9 @@ { var pageNumber = i; var pageNumberString = i.ToString(); - var pageActive = pageNumber == PaginationContext.CurrentPage; + var pageActive = pageNumber == PaginationContext.Page; - + @if ( PageButtonTemplate is not null ) { @@ -236,7 +236,7 @@ } else { - @for ( int i = PaginationContext.FirstVisiblePage; i <= PaginationContext.LastVisiblePage; ++i ) { var pageNumber = i; @@ -245,7 +245,7 @@ } - + @if ( NextPageButtonTemplate is not null ) { @@ -257,7 +257,7 @@ } - + @if ( LastPageButtonTemplate is not null ) { @@ -278,7 +278,7 @@ } else { - @foreach ( var curPageSize in PaginationContext.PageSizes ) { @curPageSize