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: change total items from int to long #5902

Open
wants to merge 11 commits into
base: next-2.0
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@

if ( !e.CancellationToken.IsCancellationRequested )
{
IEnumerable<Employee> response = ( await GetDataFromExternalSource( e.Page, e.PageSize ) );
IEnumerable<Employee> response = ( await GetDataFromExternalSource( (int)e.Page, e.PageSize ) );

if ( !e.CancellationToken.IsCancellationRequested )
{
Expand Down
62 changes: 62 additions & 0 deletions Demos/Blazorise.Demo/Pages/Tests/DataGrid/DataGridLargePage.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
@page "/tests/datagridlarge"

<h3>DataGrid on datasets larger than int.MaxValue</h3>

<DataGrid TItem="LargeModelItem"
Data="items"
TotalItems="totalCount"
ReadData="OnDataRead"
ShowPager
>
<DataGridColumn Field="@nameof( LargeModelItem.Id )" Caption="id" />
<DataGridColumn Field="@nameof( LargeModelItem.Name )" Caption="Name" Editable/>
</DataGrid>



@code {
class LargeModelItem
{
public long Id { get; set; }
public string Name { get; set; } = "";
}


long totalCount = 10_000_000_000_000;//

IEnumerable<LargeModelItem> items = Enumerable.Empty<LargeModelItem>();

private void OnDataRead(DataGridReadDataEventArgs<LargeModelItem> e)
{

// if (e.ReadDataMode == DataGridReadDataMode.Virtualize)
// {
//
// items = Enumerable.Range(e.VirtualizeOffset, e.VirtualizeCount).Select(i => new LargeModelItem
// {
// Id = i,
// Name = $"Item id { i} "
// });
// }

if (e.ReadDataMode == DataGridReadDataMode.Paging)
{
items = LongRange((e.Page - 1) * e.PageSize, e.PageSize)
.Select(i => new LargeModelItem
{
Id = i,
Name = $"Item id {i}"
});
}

}


public static IEnumerable<long> LongRange(long start, long count)
{
for (long i = 0; i < count; i++)
{
yield return start + i;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@
CustomFilter="@OnCustomFilter"
PageSize="5"
CurrentPage="currentPage"
PageChanged="(e) => currentPage = e.Page"
PageChanged="(e) => currentPage = (int)e.Page"
tesar-tech marked this conversation as resolved.
Show resolved Hide resolved
FilteredDataChanged="@OnFilteredDataChanged"
UseValidation
SortChanged="@OnSortChanged"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ private async Task OnReadData( DataGridReadDataEventArgs<Employee> 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" );

Expand Down
2 changes: 1 addition & 1 deletion Demos/Blazorise.Demo/Pages/Tests/DataGrid/PagerPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<ItemsPerPageTemplate></ItemsPerPageTemplate>
<PageSelectorTemplate>
<Select TextColor="TextColor.Success" @bind-Value="@context.CurrentPage" Size="Size.Small">
@for ( int i = context.FirstVisiblePage; i <= context.LastVisiblePage; ++i )
@for ( long i = context.FirstVisiblePage; i <= context.LastVisiblePage; ++i )
{
var pageNumber = i;
<SelectItem Value="@pageNumber">@pageNumber</SelectItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
if ( e.ReadDataMode is DataGridReadDataMode.Virtualize )
response = employeeList.Skip( e.VirtualizeOffset ).Take( e.VirtualizeCount ).ToList();
else if ( e.ReadDataMode is DataGridReadDataMode.Paging )
response = employeeList.Skip( ( e.Page - 1 ) * e.PageSize ).Take( e.PageSize ).ToList();
response = employeeList.Skip( (int)( e.Page - 1 ) * e.PageSize ).Take( e.PageSize ).ToList();
tesar-tech marked this conversation as resolved.
Show resolved Hide resolved
else
throw new Exception( "Unhandled ReadDataMode" );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
if ( e.ReadDataMode is DataGridReadDataMode.Virtualize )
response = ( await EmployeeData.GetDataAsync() ).Skip( e.VirtualizeOffset ).Take( e.VirtualizeCount ).ToList();
else if ( e.ReadDataMode is DataGridReadDataMode.Paging )
response = ( await EmployeeData.GetDataAsync() ).Skip( ( e.Page - 1 ) * e.PageSize ).Take( e.PageSize ).ToList();
response = ( await EmployeeData.GetDataAsync() ).Skip((int)( e.Page - 1 ) * e.PageSize )//converting to int will break on collections larger than int.MaxValue
tesar-tech marked this conversation as resolved.
Show resolved Hide resolved
.Take( e.PageSize ).ToList();
else
throw new Exception( "Unhandled ReadDataMode" );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<ItemsPerPageTemplate></ItemsPerPageTemplate>
<PageSelectorTemplate>
<Select TextColor="TextColor.Success" @bind-Value="@context.CurrentPage" Size="Size.Small">
@for ( int i = context.FirstVisiblePage; i <= context.LastVisiblePage; ++i )
@for ( long i = context.FirstVisiblePage; i <= context.LastVisiblePage; ++i )
{
var pageNumber = i;
<SelectItem Value="@pageNumber">@pageNumber</SelectItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class PageButtonContext
/// </summary>
/// <param name="pageNumber">Button page number .</param>
/// <param name="active">Indicates if page is active.</param>
public PageButtonContext( int pageNumber, bool active )
public PageButtonContext( long pageNumber, bool active )
{
PageNumber = pageNumber;
Active = active;
Expand All @@ -23,13 +23,13 @@ public PageButtonContext( int pageNumber, bool active )
/// <summary>
/// Gets the page number.
/// </summary>
public int PageNumber { get; private set; }
public long PageNumber { get; private set; }

/// <summary>
/// Gets the page number.
/// </summary>
[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; } }

/// <summary>
/// Get the flag that indicates if the page is active.
Expand Down
28 changes: 14 additions & 14 deletions Source/Extensions/Blazorise.DataGrid/Contexts/PaginationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@ public class PaginationContext<TItem>

private event CurrentPageChangedEventHandler CurrentPageChanged;

public delegate void CurrentPageChangedEventHandler( int value );
public delegate void CurrentPageChangedEventHandler( long value );

private event CurrentPageSizeChangedEventHandler CurrentPageSizeChanged;

public delegate void CurrentPageSizeChangedEventHandler( int value );

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<TItem> parentDataGrid;

Expand All @@ -58,7 +58,7 @@ public void UnsubscribeOnPageChanged( CurrentPageChangedEventHandler listener )
CurrentPageChanged -= listener;
}

public void TriggerCurrentPageChange( int value )
public void TriggerCurrentPageChange( long value )
{
CurrentPageChanged?.Invoke( value );
}
Expand Down Expand Up @@ -88,7 +88,7 @@ public void UnsubscribeOnTotalItemsChanged( TotalItemsChangedEventHandler listen
TotalItemsChanged -= listener;
}

public void TriggerTotalItemsChange( int value )
public void TriggerTotalItemsChange( long value )
{
TotalItemsChanged?.Invoke( value );
}
Expand Down Expand Up @@ -132,7 +132,7 @@ private void CalculateFirstAndLastVisiblePage()
/// <summary>
/// Gets or sets the current page
/// </summary>
public int CurrentPage
public long CurrentPage
{
get => currentPage;
set
Expand All @@ -148,11 +148,11 @@ public int CurrentPage
/// <summary>
/// Gets the last page number.
/// </summary>
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;
Expand All @@ -164,7 +164,7 @@ public int LastPage
/// <summary>
/// Gets the number of the first page that can be clicked in a large dataset.
/// </summary>
public int FirstVisiblePage
public long FirstVisiblePage
{
get
{
Expand All @@ -177,7 +177,7 @@ public int FirstVisiblePage
/// <summary>
/// Gets the number of the last page that can be clicked in a large dataset.
/// </summary>
public int LastVisiblePage
public long LastVisiblePage
{
get
{
Expand Down Expand Up @@ -215,7 +215,7 @@ public int CurrentPageSize
/// <remarks>
/// This field must be set only when <see cref="DataGrid{TItem}.ReadData"/> is used to load the data.
/// </remarks>
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;
Expand Down
27 changes: 18 additions & 9 deletions Source/Extensions/Blazorise.DataGrid/DataGrid.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -2172,14 +2172,22 @@ protected async ValueTask<ItemsProviderResult<TItem>> 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<TItem> rowInfo, DataGridColumn<TItem> column )
{
Expand Down Expand Up @@ -2730,14 +2738,15 @@ private IEnumerable<TItem> 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;
Expand Down Expand Up @@ -3208,7 +3217,7 @@ private bool IsMultiSelectAllIndeterminate
/// <remarks>
/// This field must be set only when <see cref="ReadData"/> is used to load the data.
/// </remarks>
[Parameter] public int? TotalItems { get => paginationContext.TotalItems; set => paginationContext.TotalItems = value; }
[Parameter] public long? TotalItems { get => paginationContext.TotalItems; set => paginationContext.TotalItems = value; }

/// <summary>
/// Gets the data after all of the filters have being applied.
Expand Down Expand Up @@ -3377,7 +3386,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 long CurrentPage { get => paginationContext.CurrentPage; set => paginationContext.CurrentPage = value; }

protected PaginationContext<TItem> PaginationContext => paginationContext;

Expand Down Expand Up @@ -3850,7 +3859,7 @@ public IReadOnlyList<DataGridBatchEditItem<TItem>> BatchChanges
/// <summary>
/// Gets a zero-based index of the currently selected row if found; otherwise it'll return -1. Considers the current pagination.
/// </summary>
public int SelectedRowIndex
public long SelectedRowIndex
{
get
{
Expand Down
2 changes: 1 addition & 1 deletion Source/Extensions/Blazorise.DataGrid/DataGridState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private static string ExtractFieldName<TValue>( Expression<Func<TItem, TValue>>
/// <summary>
/// Gets or sets the current page number.
/// </summary>
public int CurrentPage { get; set; }
public long CurrentPage { get; set; }

/// <summary>
/// Gets or sets the maximum number of items for each page.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class DataGridFilteredDataEventArgs<TItem> : EventArgs
/// <param name="filteredData">List of filtered data items.</param>
/// <param name="filteredItems">Number of filtered items.</param>
/// <param name="totalItems">Total available items in the data-source.</param>
public DataGridFilteredDataEventArgs( IEnumerable<TItem> filteredData, int filteredItems, int totalItems )
public DataGridFilteredDataEventArgs( IEnumerable<TItem> filteredData, int filteredItems, long totalItems )
{
Data = filteredData;
FilteredItems = filteredItems;
Expand All @@ -37,5 +37,5 @@ public DataGridFilteredDataEventArgs( IEnumerable<TItem> filteredData, int filte
/// <summary>
/// Gets the total available items in the data-source.
/// </summary>
public int TotalItems { get; }
public long TotalItems { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class DataGridPageChangedEventArgs : EventArgs
/// </summary>
/// <param name="page">Page number at the moment of initialization.</param>
/// <param name="pageSize">Maximum number of items per page.</param>
public DataGridPageChangedEventArgs( int page, int pageSize )
public DataGridPageChangedEventArgs( long page, int pageSize )
{
Page = page;
PageSize = pageSize;
Expand All @@ -23,7 +23,7 @@ public DataGridPageChangedEventArgs( int page, int pageSize )
/// <summary>
/// Gets the requested page number.
/// </summary>
public int Page { get; }
public long Page { get; }

/// <summary>
/// Gets the max number of items requested by page.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public DataGridReadDataEventArgs(
DataGridReadDataMode readDataMode,
IEnumerable<DataGridColumn<TItem>> columns,
IList<DataGridColumn<TItem>> sortByColumns,
int page,
long page,
int pageSize,
int virtualizeOffset,
int virtualizeCount,
Expand Down Expand Up @@ -77,7 +77,7 @@ public DataGridReadDataEventArgs(
/// <summary>
/// Gets the requested page number.
/// </summary>
public int Page { get; }
public long Page { get; }

/// <summary>
/// Gets the max number of items requested by page.
Expand Down
Loading
Loading