From 1e73424d631e8e9a3155079b38839102d269a866 Mon Sep 17 00:00:00 2001 From: tesar-tech Date: Mon, 16 Dec 2024 18:17:14 +0100 Subject: [PATCH 1/9] Breaking changes in examples leading from int -> long --- .../Pages/Tests/DataGrid/DataExternalSourcePage.razor | 2 +- Demos/Blazorise.Demo/Pages/Tests/DataGrid/DataGridPage.razor | 2 +- .../Blazorise.Demo/Pages/Tests/DataGrid/DataGridPage.razor.cs | 2 +- Demos/Blazorise.Demo/Pages/Tests/DataGrid/PagerPage.razor | 2 +- .../DataGrid/Examples/DataGridAggregatesLargeDataExample.razor | 2 +- .../DataGrid/Examples/DataGridLargeDataExample.razor | 3 ++- .../Extensions/DataGrid/Examples/DataGridPagerExample.razor | 2 +- 7 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Demos/Blazorise.Demo/Pages/Tests/DataGrid/DataExternalSourcePage.razor b/Demos/Blazorise.Demo/Pages/Tests/DataGrid/DataExternalSourcePage.razor index 54b3298967..49a00e8357 100644 --- a/Demos/Blazorise.Demo/Pages/Tests/DataGrid/DataExternalSourcePage.razor +++ b/Demos/Blazorise.Demo/Pages/Tests/DataGrid/DataExternalSourcePage.razor @@ -101,7 +101,7 @@ if ( !e.CancellationToken.IsCancellationRequested ) { - IEnumerable response = ( await GetDataFromExternalSource( e.Page, e.PageSize ) ); + IEnumerable response = ( await GetDataFromExternalSource( (int)e.Page, e.PageSize ) ); if ( !e.CancellationToken.IsCancellationRequested ) { diff --git a/Demos/Blazorise.Demo/Pages/Tests/DataGrid/DataGridPage.razor b/Demos/Blazorise.Demo/Pages/Tests/DataGrid/DataGridPage.razor index 6feea6e1d9..2499fb5b1a 100644 --- a/Demos/Blazorise.Demo/Pages/Tests/DataGrid/DataGridPage.razor +++ b/Demos/Blazorise.Demo/Pages/Tests/DataGrid/DataGridPage.razor @@ -256,7 +256,7 @@ CustomFilter="@OnCustomFilter" PageSize="5" CurrentPage="currentPage" - PageChanged="(e) => currentPage = e.Page" + PageChanged="(e) => currentPage = (int)e.Page" 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..f435e3ca11 100644 --- a/Demos/Blazorise.Demo/Pages/Tests/DataGrid/DataGridPage.razor.cs +++ b/Demos/Blazorise.Demo/Pages/Tests/DataGrid/DataGridPage.razor.cs @@ -211,7 +211,7 @@ private async Task OnReadData( DataGridReadDataEventArgs e ) if ( e.ReadDataMode is DataGridReadDataMode.Virtualize ) response = filteredData.Skip( e.VirtualizeOffset ).Take( e.VirtualizeCount ).ToList(); else if ( e.ReadDataMode is DataGridReadDataMode.Paging ) - response = filteredData.Skip( ( e.Page - 1 ) * e.PageSize ).Take( e.PageSize ).ToList(); + response = filteredData.Skip( (int)( e.Page - 1 ) * e.PageSize ).Take( e.PageSize ).ToList(); else throw new Exception( "Unhandled ReadDataMode" ); diff --git a/Demos/Blazorise.Demo/Pages/Tests/DataGrid/PagerPage.razor b/Demos/Blazorise.Demo/Pages/Tests/DataGrid/PagerPage.razor index 18b923499b..f958a187f9 100644 --- a/Demos/Blazorise.Demo/Pages/Tests/DataGrid/PagerPage.razor +++ b/Demos/Blazorise.Demo/Pages/Tests/DataGrid/PagerPage.razor @@ -51,7 +51,7 @@ - @for ( int i = context.FirstVisiblePage; i <= context.LastVisiblePage; ++i ) + @for ( long i = context.FirstVisiblePage; i <= context.LastVisiblePage; ++i ) { var pageNumber = i; @pageNumber From a8427991b0d89d61da7c01b3ea5e0d1f6c882357 Mon Sep 17 00:00:00 2001 From: tesar-tech Date: Mon, 16 Dec 2024 18:23:36 +0100 Subject: [PATCH 2/9] int -> long in datagrid.TotalItems and related pieces. Safe-rails for virtualize --- .../Contexts/PageButtonContext.cs | 6 ++-- .../Contexts/PaginationContext.cs | 28 +++++++++---------- .../Blazorise.DataGrid/DataGrid.razor.cs | 27 ++++++++++++------ .../Blazorise.DataGrid/DataGridState.cs | 2 +- .../DataGridFilteredDataEventArgs.cs | 4 +-- .../DataGridPageChangedEventArgs.cs | 4 +-- .../DataGridReadDataEventArgs.cs | 4 +-- .../_DataGridPagination.razor | 10 +++---- 8 files changed, 47 insertions(+), 38 deletions(-) diff --git a/Source/Extensions/Blazorise.DataGrid/Contexts/PageButtonContext.cs b/Source/Extensions/Blazorise.DataGrid/Contexts/PageButtonContext.cs index 6a440088a7..1d3b55cb2d 100644 --- a/Source/Extensions/Blazorise.DataGrid/Contexts/PageButtonContext.cs +++ b/Source/Extensions/Blazorise.DataGrid/Contexts/PageButtonContext.cs @@ -14,7 +14,7 @@ public class PageButtonContext /// /// Button page number . /// Indicates if page is active. - public PageButtonContext( int pageNumber, bool active ) + public PageButtonContext( long pageNumber, bool active ) { PageNumber = pageNumber; Active = active; @@ -23,13 +23,13 @@ public PageButtonContext( int pageNumber, bool active ) /// /// Gets the page number. /// - public int PageNumber { get; private set; } + public long PageNumber { get; private set; } /// /// Gets the page number. /// [Obsolete( "PageNumer is deprecated and will be removed in future versions, please use PageNumber instead.", true )] - public int PageNumer { get { return PageNumber; } set { PageNumber = value; } } + public long PageNumer { get { return PageNumber; } set { PageNumber = value; } } /// /// Get the flag that indicates if the page is active. diff --git a/Source/Extensions/Blazorise.DataGrid/Contexts/PaginationContext.cs b/Source/Extensions/Blazorise.DataGrid/Contexts/PaginationContext.cs index 0c9ec13ed0..fe5b774801 100644 --- a/Source/Extensions/Blazorise.DataGrid/Contexts/PaginationContext.cs +++ b/Source/Extensions/Blazorise.DataGrid/Contexts/PaginationContext.cs @@ -13,7 +13,7 @@ public class PaginationContext private event CurrentPageChangedEventHandler CurrentPageChanged; - public delegate void CurrentPageChangedEventHandler( int value ); + public delegate void CurrentPageChangedEventHandler( long value ); private event CurrentPageSizeChangedEventHandler CurrentPageSizeChanged; @@ -21,17 +21,17 @@ public class PaginationContext private event TotalItemsChangedEventHandler TotalItemsChanged; - public delegate void TotalItemsChangedEventHandler( int value ); + public delegate void TotalItemsChangedEventHandler( long value ); - private int firstVisiblePage; + private long firstVisiblePage; - private int lastVisiblePage; + private long lastVisiblePage; - private int currentPage = 1; + private long currentPage = 1; private int currentPageSize = 10; - private int? totalItems; + private long? totalItems; private DataGrid parentDataGrid; @@ -58,7 +58,7 @@ public void UnsubscribeOnPageChanged( CurrentPageChangedEventHandler listener ) CurrentPageChanged -= listener; } - public void TriggerCurrentPageChange( int value ) + public void TriggerCurrentPageChange( long value ) { CurrentPageChanged?.Invoke( value ); } @@ -88,7 +88,7 @@ public void UnsubscribeOnTotalItemsChanged( TotalItemsChangedEventHandler listen TotalItemsChanged -= listener; } - public void TriggerTotalItemsChange( int value ) + public void TriggerTotalItemsChange( long value ) { TotalItemsChanged?.Invoke( value ); } @@ -132,7 +132,7 @@ private void CalculateFirstAndLastVisiblePage() /// /// Gets or sets the current page /// - public int CurrentPage + public long CurrentPage { get => currentPage; set @@ -148,11 +148,11 @@ public int CurrentPage /// /// Gets the last page number. /// - public int LastPage + public long LastPage { get { - var lastPage = Math.Max( (int)Math.Ceiling( ( TotalItems ?? 0 ) / (double)currentPageSize ), 1 ); + long lastPage = Math.Max( (long)Math.Ceiling( ( TotalItems ?? 0 ) / (double)currentPageSize ), 1 ); if ( CurrentPage > lastPage ) CurrentPage = lastPage; @@ -164,7 +164,7 @@ public int LastPage /// /// Gets the number of the first page that can be clicked in a large dataset. /// - public int FirstVisiblePage + public long FirstVisiblePage { get { @@ -177,7 +177,7 @@ public int FirstVisiblePage /// /// Gets the number of the last page that can be clicked in a large dataset. /// - public int LastVisiblePage + public long LastVisiblePage { get { @@ -215,7 +215,7 @@ public int CurrentPageSize /// /// This field must be set only when is used to load the data. /// - public int? TotalItems + public long? TotalItems { // If we're using ReadData than TotalItems must be set so we can know how many items are available get => ( ( parentDataGrid.ManualReadMode || parentDataGrid.VirtualizeManualReadMode ) ? totalItems : parentDataGrid.FilteredData?.Count() ) ?? 0; diff --git a/Source/Extensions/Blazorise.DataGrid/DataGrid.razor.cs b/Source/Extensions/Blazorise.DataGrid/DataGrid.razor.cs index b34ddec160..d74ab0b7b2 100644 --- a/Source/Extensions/Blazorise.DataGrid/DataGrid.razor.cs +++ b/Source/Extensions/Blazorise.DataGrid/DataGrid.razor.cs @@ -1026,7 +1026,7 @@ private async void OnPageSizeChanged( int pageSize ) await ReloadInternal( paginationContext.CancellationTokenSource.Token ); } - private async void OnPageChanged( int currentPage ) + private async void OnPageChanged( long currentPage ) { paginationContext.CancellationTokenSource?.Cancel(); paginationContext.CancellationTokenSource = new(); @@ -2172,14 +2172,22 @@ protected async ValueTask> VirtualizeItemsProviderHan ? Math.Min( request.Count, TotalItems.Value - request.StartIndex ) : request.Count; - await HandleVirtualizeReadData( request.StartIndex, requestCount, request.CancellationToken ); + await HandleVirtualizeReadData( request.StartIndex, (int)requestCount, request.CancellationToken ); await Task.Yield(); // This line makes sure SetParametersAsync catches up, since we depend upon Data Parameter. if ( request.CancellationToken.IsCancellationRequested ) return default; else - return new( Data.ToList(), TotalItems.Value ); + return new( Data.ToList(), LongToIntSafe(TotalItems) );//safty rails for virualize - doesn't overflow, but doesn't work either } + + int LongToIntSafe(long? value) => value switch + { + null => 0, + > int.MaxValue => int.MaxValue, + < int.MinValue => int.MinValue, + _ => (int)value + }; internal async Task HandleSelectedCell( TItem item, DataGridRowInfo rowInfo, DataGridColumn column ) { @@ -2730,14 +2738,15 @@ private IEnumerable FilterViewData() // only use pagination if the custom data loading is not used if ( !ManualReadMode && !Virtualize ) { - var skipElements = ( CurrentPage - 1 ) * PageSize; + long skipElements = ( CurrentPage - 1 ) * PageSize; if ( skipElements > filteredData.Count ) { CurrentPage = paginationContext.LastPage; skipElements = ( CurrentPage - 1 ) * PageSize; } - - return filteredData.Skip( skipElements ).Take( PageSize ); + + //here the conversion to int is safe, because without manualReadMode it's not probable user will use more than int.MaxValue data + return filteredData.Skip( (int)skipElements ).Take( PageSize ); } return filteredData; @@ -3208,7 +3217,7 @@ private bool IsMultiSelectAllIndeterminate /// /// This field must be set only when is used to load the data. /// - [Parameter] public int? TotalItems { get => paginationContext.TotalItems; set => paginationContext.TotalItems = value; } + [Parameter] public long? TotalItems { get => paginationContext.TotalItems; set => paginationContext.TotalItems = value; } /// /// Gets the data after all of the filters have being applied. @@ -3377,7 +3386,7 @@ public IReadOnlyList> BatchChanges /// /// Gets or sets the current page number. /// - [Parameter] public int CurrentPage { get => paginationContext.CurrentPage; set => paginationContext.CurrentPage = value; } + [Parameter] public long CurrentPage { get => paginationContext.CurrentPage; set => paginationContext.CurrentPage = value; } protected PaginationContext PaginationContext => paginationContext; @@ -3850,7 +3859,7 @@ public IReadOnlyList> BatchChanges /// /// Gets a zero-based index of the currently selected row if found; otherwise it'll return -1. Considers the current pagination. /// - public int SelectedRowIndex + public long SelectedRowIndex { get { diff --git a/Source/Extensions/Blazorise.DataGrid/DataGridState.cs b/Source/Extensions/Blazorise.DataGrid/DataGridState.cs index 49d312571d..fa6c2dee38 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 long CurrentPage { get; set; } /// /// Gets or sets the maximum number of items for each page. diff --git a/Source/Extensions/Blazorise.DataGrid/EventArguments/DataGridFilteredDataEventArgs.cs b/Source/Extensions/Blazorise.DataGrid/EventArguments/DataGridFilteredDataEventArgs.cs index acf8c7c55e..3f03d5fa1e 100644 --- a/Source/Extensions/Blazorise.DataGrid/EventArguments/DataGridFilteredDataEventArgs.cs +++ b/Source/Extensions/Blazorise.DataGrid/EventArguments/DataGridFilteredDataEventArgs.cs @@ -17,7 +17,7 @@ public class DataGridFilteredDataEventArgs : EventArgs /// List of filtered data items. /// Number of filtered items. /// Total available items in the data-source. - public DataGridFilteredDataEventArgs( IEnumerable filteredData, int filteredItems, int totalItems ) + public DataGridFilteredDataEventArgs( IEnumerable filteredData, int filteredItems, long totalItems ) { Data = filteredData; FilteredItems = filteredItems; @@ -37,5 +37,5 @@ public DataGridFilteredDataEventArgs( IEnumerable filteredData, int filte /// /// Gets the total available items in the data-source. /// - public int TotalItems { get; } + public long TotalItems { get; } } \ No newline at end of file diff --git a/Source/Extensions/Blazorise.DataGrid/EventArguments/DataGridPageChangedEventArgs.cs b/Source/Extensions/Blazorise.DataGrid/EventArguments/DataGridPageChangedEventArgs.cs index f8f6c8cd15..2845a00cb0 100644 --- a/Source/Extensions/Blazorise.DataGrid/EventArguments/DataGridPageChangedEventArgs.cs +++ b/Source/Extensions/Blazorise.DataGrid/EventArguments/DataGridPageChangedEventArgs.cs @@ -14,7 +14,7 @@ public class DataGridPageChangedEventArgs : EventArgs /// /// Page number at the moment of initialization. /// Maximum number of items per page. - public DataGridPageChangedEventArgs( int page, int pageSize ) + public DataGridPageChangedEventArgs( long page, int pageSize ) { Page = page; PageSize = pageSize; @@ -23,7 +23,7 @@ public DataGridPageChangedEventArgs( int page, int pageSize ) /// /// Gets the requested page number. /// - public int Page { get; } + public long Page { get; } /// /// Gets the max number of items requested by page. diff --git a/Source/Extensions/Blazorise.DataGrid/EventArguments/DataGridReadDataEventArgs.cs b/Source/Extensions/Blazorise.DataGrid/EventArguments/DataGridReadDataEventArgs.cs index 038c62dd14..ff0e581b5e 100644 --- a/Source/Extensions/Blazorise.DataGrid/EventArguments/DataGridReadDataEventArgs.cs +++ b/Source/Extensions/Blazorise.DataGrid/EventArguments/DataGridReadDataEventArgs.cs @@ -44,7 +44,7 @@ public DataGridReadDataEventArgs( DataGridReadDataMode readDataMode, IEnumerable> columns, IList> sortByColumns, - int page, + long page, int pageSize, int virtualizeOffset, int virtualizeCount, @@ -77,7 +77,7 @@ public DataGridReadDataEventArgs( /// /// Gets the requested page number. /// - public int Page { get; } + public long Page { get; } /// /// Gets the max number of items requested by page. diff --git a/Source/Extensions/Blazorise.DataGrid/_DataGridPagination.razor b/Source/Extensions/Blazorise.DataGrid/_DataGridPagination.razor index db3f697e37..cdf4efa53d 100644 --- a/Source/Extensions/Blazorise.DataGrid/_DataGridPagination.razor +++ b/Source/Extensions/Blazorise.DataGrid/_DataGridPagination.razor @@ -153,7 +153,7 @@ } else { - var totalItems = PaginationContext?.TotalItems ?? default; + long totalItems = PaginationContext?.TotalItems ?? default; if ( totalItems == 0 ) { @@ -161,8 +161,8 @@ } else { - var curStart = ( ( PaginationContext.CurrentPage - 1 ) * PaginationContext.CurrentPageSize + 1 ); - var curEnd = Math.Min( PaginationContext.CurrentPage * PaginationContext.CurrentPageSize, totalItems ); + long curStart = ( ( PaginationContext.CurrentPage - 1 ) * PaginationContext.CurrentPageSize + 1 ); + long curEnd = Math.Min( PaginationContext.CurrentPage * PaginationContext.CurrentPageSize, totalItems ); @Localizer.Localize( ParentDataGrid.Localizers?.NumbersOfItemsLocalizer, "{0} - {1} of {2} items", curStart, curEnd, totalItems ) } } @@ -210,7 +210,7 @@ } - @for ( int i = PaginationContext.FirstVisiblePage; i <= PaginationContext.LastVisiblePage; ++i ) + @for ( long i = PaginationContext.FirstVisiblePage; i <= PaginationContext.LastVisiblePage; ++i ) { var pageNumber = i; var pageNumberString = i.ToString(); @@ -237,7 +237,7 @@ else {