Skip to content

Commit

Permalink
Allow custom PageSizeList and always show PageSize
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Miller committed Dec 27, 2023
1 parent e8a53a6 commit a9ebf9d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Maui.DataGrid.Sample/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<dg:DataGrid Grid.Row="1" ItemsSource="{Binding Teams}" SelectionEnabled="True" SelectedItem="{Binding SelectedTeam}"
RowHeight="70" HeaderHeight="50" BorderColor="{StaticResource GridBorderColor}" RowToEdit="{Binding TeamToEdit}"
HeaderBackground="{StaticResource GridHeaderBgColor}" HeaderBordersVisible="{Binding HeaderBordersVisible}"
PullToRefreshCommand="{Binding RefreshCommand}" IsRefreshing="{Binding IsRefreshing}" PaginationEnabled="{Binding PaginationEnabled}" PageSize="5"
PullToRefreshCommand="{Binding RefreshCommand}" IsRefreshing="{Binding IsRefreshing}" PaginationEnabled="{Binding PaginationEnabled}" PageSize="6"
ActiveRowColor="{StaticResource ActiveRowColor}" FooterBackground="{StaticResource GridFooterBgColor}" x:Name="_dataGrid1">
<dg:DataGrid.Columns>
<dg:DataGridColumn Title="Logo" PropertyName="Logo" SortingEnabled="False">
Expand Down
14 changes: 14 additions & 0 deletions Maui.DataGrid/CompatibilitySuppressions.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- https://learn.microsoft.com/en-us/dotnet/fundamentals/package-validation/diagnostic-ids -->
<Suppressions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>F:Maui.DataGrid.DataGrid.PageSizeListProperty</Target>
<Left>lib/net7.0/Maui.DataGrid.dll</Left>
<Right>lib/net7.0/Maui.DataGrid.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>F:Maui.DataGrid.DataGrid.RowToEditProperty</Target>
Expand Down Expand Up @@ -29,6 +36,13 @@
<Right>lib/net7.0/Maui.DataGrid.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Maui.DataGrid.DataGrid.set_PageSizeList(System.Collections.ObjectModel.ObservableCollection{System.Int32})</Target>
<Left>lib/net7.0/Maui.DataGrid.dll</Left>
<Right>lib/net7.0/Maui.DataGrid.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Maui.DataGrid.DataGrid.set_RowToEdit(System.Object)</Target>
Expand Down
2 changes: 1 addition & 1 deletion Maui.DataGrid/DataGrid.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
</Grid.ColumnDefinitions>
<HorizontalStackLayout VerticalOptions="Center" IsVisible="{Binding PageSizeVisible, Source={Reference self}}">
<Label Text="# per page:" Margin="5,0,0,0" VerticalTextAlignment="Center" TextColor="Black" />
<Picker ItemsSource="{Binding PageSizeList, Source={Reference self}, Mode=OneTime}" SelectedItem="{Binding PageSize, Source={Reference self}}" TextColor="Black" />
<Picker ItemsSource="{Binding PageSizeList, Source={Reference self}, Mode=TwoWay}" SelectedItem="{Binding PageSize, Source={Reference self}}" TextColor="Black" />
</HorizontalStackLayout>
<HorizontalStackLayout Grid.Column="2" VerticalOptions="Center">
<Label Text="Page:" Margin="0,0,5,0" VerticalTextAlignment="Center" TextColor="Black" />
Expand Down
51 changes: 48 additions & 3 deletions Maui.DataGrid/DataGrid.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ namespace Maui.DataGrid;

using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
Expand All @@ -19,6 +18,7 @@ namespace Maui.DataGrid;
public partial class DataGrid
{
#region Fields
private static readonly SortedSet<int> DefaultPageSizeList = [5, 10, 50, 100, 200, 1000];

private static readonly ColumnDefinitionCollection HeaderColumnDefinitions =
[
Expand All @@ -29,6 +29,7 @@ public partial class DataGrid
private readonly WeakEventManager _itemSelectedEventManager = new();
private readonly WeakEventManager _refreshingEventManager = new();

private readonly SortedSet<int> _pageSizeList = DefaultPageSizeList;
private readonly Style _defaultHeaderStyle;
private readonly Style _defaultSortIconStyle;

Expand Down Expand Up @@ -184,6 +185,24 @@ private IEnumerable<object> GetPaginatedItems(IEnumerable<object> unpaginatedIte
return unpaginatedItems.Skip(skip).Take(PageSize);
}

/// <summary>
/// Checks if PageSizeList contains the new PageSize value, so that it shows in the dropdown
/// </summary>
private void UpdatePageSizeList()
{
if (PageSizeList.Contains(PageSize))
{
return;
}

if (_pageSizeList.Add(PageSize))
{
PageSizeList = new(_pageSizeList);
OnPropertyChanged(nameof(PageSizeList));
OnPropertyChanged(nameof(PageSize));
}
}

private void SortAndPaginate(SortData? sortData = null)
{
lock (_sortAndPaginateLock)
Expand Down Expand Up @@ -457,9 +476,23 @@ private void SortAndPaginate(SortData? sortData = null)
self.PageCount = (int)Math.Ceiling(self.ItemsSource.Cast<object>().Count() / (double)self.PageSize);
}
self.SortAndPaginate();
self.UpdatePageSizeList();
}
});

/// <summary>
/// Gets or sets the list of available page sizes for the DataGrid.
/// </summary>
public static readonly BindableProperty PageSizeListProperty =
BindablePropertyExtensions.Create<DataGrid, ObservableCollection<int>>(new(DefaultPageSizeList),
propertyChanged: (b, o, n) =>
{
if (o != n && b is DataGrid self)
{
self.UpdatePageSizeList();
}
});

/// <summary>
/// Gets or sets a value indicating whether the page size is visible in the DataGrid.
/// </summary>
Expand Down Expand Up @@ -862,9 +895,13 @@ public int PageSize
}

/// <summary>
/// List of page sizes
/// Gets or sets the list of available page sizes
/// </summary>
public IReadOnlyCollection<int> PageSizeList { get; } = ImmutableArray.Create(5, 10, 50, 100, 200, 1000);
public ObservableCollection<int> PageSizeList
{
get => (ObservableCollection<int>)GetValue(PageSizeListProperty);
set => SetValue(PageSizeListProperty, value);
}

/// <summary>
/// Gets or sets whether the page size picker is visible
Expand Down Expand Up @@ -1173,6 +1210,14 @@ internal void Reload()

try
{
// Check if PageSizeList contains the new PageSize value, so that it shows in the dropdown
if (!PageSizeList.Contains(PageSize))
{
PageSizeList.Add(PageSize);
OnPropertyChanged(nameof(PageSizeList));
OnPropertyChanged(nameof(PageSize));
}

InitHeaderView();

if (_internalItems is not null)
Expand Down

0 comments on commit a9ebf9d

Please sign in to comment.