@@ -12,6 +12,10 @@ public partial class Grid<TItem> : BlazorBootstrapComponentBase
12
12
13
13
private List < GridColumn < TItem > > columns = new ( ) ;
14
14
15
+ private GridDetailView < TItem > ? detailView ;
16
+
17
+ public GridEmptyDataTemplate < TItem > ? emptyDataTemplate ;
18
+
15
19
/// <summary>
16
20
/// Current grid state (filters, paging, sorting).
17
21
/// </summary>
@@ -21,18 +25,14 @@ public partial class Grid<TItem> : BlazorBootstrapComponentBase
21
25
22
26
private RenderFragment ? headerSelectionTemplate ;
23
27
24
- private GridDetailView < TItem > ? detailView ;
25
-
26
- public GridEmptyDataTemplate < TItem > ? emptyDataTemplate ;
27
-
28
- public GridLoadingTemplate < TItem > ? loadingTemplate ;
29
-
30
28
private bool isFirstRenderComplete = false ;
31
29
32
30
private List < TItem > ? items = null ;
33
31
34
32
private object ? lastAssignedDataOrDataProvider ;
35
33
34
+ public GridLoadingTemplate < TItem > ? loadingTemplate ;
35
+
36
36
private int pageSize ;
37
37
38
38
private bool requestInProgress = false ;
@@ -69,7 +69,8 @@ protected override void OnInitialized()
69
69
70
70
protected override Task OnParametersSetAsync ( )
71
71
{
72
- if ( ( Data is null && DataProvider is null ) || ( Data is not null && DataProvider is not null ) ) throw new ArgumentException ( $ "Grid requires either { nameof ( Data ) } or { nameof ( DataProvider ) } , but not both or neither.") ;
72
+ if ( ( Data is null && DataProvider is null ) || ( Data is not null && DataProvider is not null ) )
73
+ throw new ArgumentException ( $ "Grid requires either { nameof ( Data ) } or { nameof ( DataProvider ) } , but not both or neither.") ;
73
74
74
75
if ( AllowPaging && PageSize < 0 )
75
76
throw new ArgumentException ( $ "{ nameof ( PageSize ) } must be greater than zero.") ;
@@ -123,6 +124,10 @@ protected override Task OnParametersSetAsync()
123
124
/// </summary>
124
125
public async ValueTask ResetPageNumber ( ) => await ResetPageNumberAsync ( true ) ;
125
126
127
+ public Task SelectAllItemsAsync ( ) => SelectAllItemsInternalAsync ( true ) ;
128
+
129
+ public Task UnSelectAllItemsAsync ( ) => SelectAllItemsInternalAsync ( false ) ;
130
+
126
131
internal void AddColumn ( GridColumn < TItem > column ) => columns . Add ( column ) ;
127
132
128
133
internal async Task FilterChangedAsync ( )
@@ -158,13 +163,13 @@ internal async Task RefreshDataAsync(bool firstRender = false, CancellationToken
158
163
await LoadGridSettingsAsync ( ) ;
159
164
160
165
var request = new GridDataProviderRequest < TItem >
161
- {
162
- PageNumber = AllowPaging ? gridCurrentState . PageIndex : 0 ,
163
- PageSize = AllowPaging ? pageSize : 0 ,
164
- Sorting = AllowSorting ? gridCurrentState . Sorting ?? GetDefaultSorting ( ) ! : null ! ,
165
- Filters = AllowFiltering ? GetFilters ( ) ! : null ! ,
166
- CancellationToken = cancellationToken
167
- } ;
166
+ {
167
+ PageNumber = AllowPaging ? gridCurrentState . PageIndex : 0 ,
168
+ PageSize = AllowPaging ? pageSize : 0 ,
169
+ Sorting = AllowSorting ? gridCurrentState . Sorting ?? GetDefaultSorting ( ) ! : null ! ,
170
+ Filters = AllowFiltering ? GetFilters ( ) ! : null ! ,
171
+ CancellationToken = cancellationToken
172
+ } ;
168
173
169
174
GridDataProviderResult < TItem > result = default ! ;
170
175
@@ -205,6 +210,12 @@ internal async ValueTask ResetPageNumberAsync(bool refreshGrid = false)
205
210
await RefreshDataAsync ( false ) ;
206
211
}
207
212
213
+ internal void SetGridDetailView ( GridDetailView < TItem > detailView ) => this . detailView = detailView ;
214
+
215
+ internal void SetGridEmptyDataTemplate ( GridEmptyDataTemplate < TItem > emptyDataTemplate ) => this . emptyDataTemplate = emptyDataTemplate ;
216
+
217
+ internal void SetGridLoadingTemplate ( GridLoadingTemplate < TItem > loadingTemplate ) => this . loadingTemplate = loadingTemplate ;
218
+
208
219
internal async Task SortingChangedAsync ( GridColumn < TItem > column )
209
220
{
210
221
if ( columns == null || ! columns . Any ( ) )
@@ -380,13 +391,8 @@ private async Task LoadGridSettingsAsync()
380
391
381
392
private async Task OnHeaderCheckboxChanged ( ChangeEventArgs args )
382
393
{
383
- allItemsSelected = bool . TryParse ( args ? . Value ? . ToString ( ) , out var checkboxState ) && checkboxState ;
384
- selectedItems = allItemsSelected ? new HashSet < TItem > ( items ! ) : new HashSet < TItem > ( ) ;
385
- SelectedItemsCount = selectedItems . Count ;
386
- await CheckOrUnCheckAll ( ) ;
387
-
388
- if ( SelectedItemsChanged . HasDelegate )
389
- await SelectedItemsChanged . InvokeAsync ( selectedItems ) ;
394
+ var headerCheckboxState = bool . TryParse ( args ? . Value ? . ToString ( ) , out var checkboxState ) && checkboxState ;
395
+ await SelectAllItemsInternalAsync ( headerCheckboxState ) ;
390
396
}
391
397
392
398
private async Task OnPageChangedAsync ( int newPageNumber )
@@ -495,13 +501,27 @@ private Task SaveGridSettingsAsync()
495
501
return GridSettingsChanged . InvokeAsync ( settings ) ;
496
502
}
497
503
498
- private async Task SetCheckboxStateAsync ( string id , CheckboxState checkboxState ) => await JSRuntime . InvokeVoidAsync ( "window.blazorBootstrap.grid.setSelectAllCheckboxState" , id , ( int ) checkboxState ) ;
504
+ private async Task SelectAllItemsInternalAsync ( bool selectAll )
505
+ {
506
+ if ( SelectionMode != GridSelectionMode . Multiple )
507
+ return ;
499
508
500
- internal void SetGridDetailView ( GridDetailView < TItem > detailView ) => this . detailView = detailView ;
509
+ allItemsSelected = selectAll ;
510
+ selectedItems = allItemsSelected ? new HashSet < TItem > ( items ! ) : new HashSet < TItem > ( ) ;
511
+ SelectedItemsCount = allItemsSelected ? selectedItems . Count : 0 ;
501
512
502
- internal void SetGridEmptyDataTemplate ( GridEmptyDataTemplate < TItem > emptyDataTemplate ) => this . emptyDataTemplate = emptyDataTemplate ;
513
+ if ( allItemsSelected )
514
+ await SetCheckboxStateAsync ( headerCheckboxId , CheckboxState . Checked ) ;
515
+ else
516
+ await SetCheckboxStateAsync ( headerCheckboxId , CheckboxState . Unchecked ) ;
503
517
504
- internal void SetGridLoadingTemplate ( GridLoadingTemplate < TItem > loadingTemplate ) => this . loadingTemplate = loadingTemplate ;
518
+ await CheckOrUnCheckAll ( ) ;
519
+
520
+ if ( SelectedItemsChanged . HasDelegate )
521
+ await SelectedItemsChanged . InvokeAsync ( selectedItems ) ;
522
+ }
523
+
524
+ private async Task SetCheckboxStateAsync ( string id , CheckboxState checkboxState ) => await JSRuntime . InvokeVoidAsync ( "window.blazorBootstrap.grid.setSelectAllCheckboxState" , id , ( int ) checkboxState ) ;
505
525
506
526
/// <summary>
507
527
/// Set filters.
@@ -534,9 +554,11 @@ private void SetFilters(IEnumerable<FilterItem> filterItems)
534
554
#region Properties, Indexers
535
555
536
556
protected override string ? ClassNames =>
537
- BuildClassNames ( Class ,
557
+ BuildClassNames (
558
+ Class ,
538
559
( "bb-table" , true ) ,
539
- ( BootstrapClass . TableSticky , FixedHeader ) ) ;
560
+ ( BootstrapClass . TableSticky , FixedHeader )
561
+ ) ;
540
562
541
563
/// <summary>
542
564
/// Gets or sets the grid delete.
@@ -705,8 +727,10 @@ private void SetFilters(IEnumerable<FilterItem> filterItems)
705
727
public string ? GridContainerClass { get ; set ; }
706
728
707
729
private string ? GridContainerClassNames =>
708
- BuildClassNames ( GridContainerClass ,
709
- ( BootstrapClass . TableResponsive , Responsive ) ) ;
730
+ BuildClassNames (
731
+ GridContainerClass ,
732
+ ( BootstrapClass . TableResponsive , Responsive )
733
+ ) ;
710
734
711
735
/// <summary>
712
736
/// Gets or sets the grid container css style.
@@ -715,8 +739,10 @@ private void SetFilters(IEnumerable<FilterItem> filterItems)
715
739
public string ? GridContainerStyle { get ; set ; }
716
740
717
741
private string ? GridContainerStyleNames =>
718
- BuildStyleNames ( GridContainerStyle ,
719
- ( $ "height:{ Height . ToString ( CultureInfo . InvariantCulture ) } { Unit . ToCssString ( ) } ", FixedHeader ) ) ;
742
+ BuildStyleNames (
743
+ GridContainerStyle ,
744
+ ( $ "height:{ Height . ToString ( CultureInfo . InvariantCulture ) } { Unit . ToCssString ( ) } ", FixedHeader )
745
+ ) ;
720
746
721
747
/// <summary>
722
748
/// This event is fired when the grid state is changed.
0 commit comments