-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Grid: Select all or Unselect all items programmatically (#988)
* Grid: Select all or Unselect all items programmatically
- Loading branch information
Showing
4 changed files
with
159 additions
and
33 deletions.
There are no files selected for viewing
File renamed without changes.
90 changes: 90 additions & 0 deletions
90
...mponents/Pages/Grid/06-selection/Grid_Demo_02_B_Multiple_Selection_Programmatically.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<div class="mb-3"> | ||
<Button Type="ButtonType.Button" Color="ButtonColor.Primary" Size="ButtonSize.Small" @onclick="SelectAllEmployees">Select All</Button> | ||
<Button Type="ButtonType.Button" Color="ButtonColor.Primary" Size="ButtonSize.Small" @onclick="UnselectAllEmployees">Unselect All</Button> | ||
</div> | ||
|
||
<Grid @ref="gridRef" TItem="Employee1" | ||
Class="table table-hover table-bordered" | ||
DataProvider="EmployeesDataProvider" | ||
AllowFiltering="true" | ||
AllowSelection="true" | ||
SelectionMode="GridSelectionMode.Multiple" | ||
SelectedItemsChanged="OnSelectedItemsChanged" | ||
Responsive="true"> | ||
|
||
<GridColumns> | ||
<GridColumn TItem="Employee1" HeaderText="Id" PropertyName="Id"> | ||
@context.Id | ||
</GridColumn> | ||
<GridColumn TItem="Employee1" HeaderText="Employee Name" PropertyName="Name"> | ||
@context.Name | ||
</GridColumn> | ||
<GridColumn TItem="Employee1" HeaderText="Designation" PropertyName="Designation"> | ||
@context.Designation | ||
</GridColumn> | ||
<GridColumn TItem="Employee1" HeaderText="DOJ" PropertyName="DOJ"> | ||
@context.DOJ | ||
</GridColumn> | ||
<GridColumn TItem="Employee1" HeaderText="Active" PropertyName="IsActive"> | ||
@context.IsActive | ||
</GridColumn> | ||
</GridColumns> | ||
|
||
</Grid> | ||
|
||
<div class="mt-3"> | ||
Selected Items Count: @selectedEmployees.Count | ||
</div> | ||
|
||
<div class="mt-2"> | ||
Selected Employees: | ||
<ul> | ||
@foreach (var emp in selectedEmployees) | ||
{ | ||
<li>@emp.Name</li> | ||
} | ||
</ul> | ||
</div> | ||
|
||
@code { | ||
private Grid<Employee1> gridRef; | ||
private IEnumerable<Employee1> employees = default!; | ||
|
||
private HashSet<Employee1> selectedEmployees = new(); | ||
|
||
private async Task<GridDataProviderResult<Employee1>> EmployeesDataProvider(GridDataProviderRequest<Employee1> request) | ||
{ | ||
Console.WriteLine("EmployeesDataProvider called..."); | ||
|
||
if (employees is null) // pull employees only one time for client-side filtering, sorting, and paging | ||
employees = GetEmployees(); // call a service or an API to pull the employees | ||
return await Task.FromResult(request.ApplyTo(employees)); | ||
} | ||
|
||
private IEnumerable<Employee1> GetEmployees() | ||
{ | ||
return new List<Employee1> | ||
{ | ||
new Employee1 { Id = 107, Name = "Alice", Designation = "AI Engineer", DOJ = new DateOnly(1998, 11, 17), IsActive = true }, | ||
new Employee1 { Id = 103, Name = "Bob", Designation = "Senior DevOps Engineer", DOJ = new DateOnly(1985, 1, 5), IsActive = true }, | ||
new Employee1 { Id = 106, Name = "John", Designation = "Data Engineer", DOJ = new DateOnly(1995, 4, 17), IsActive = true }, | ||
new Employee1 { Id = 104, Name = "Pop", Designation = "Associate Architect", DOJ = new DateOnly(1985, 6, 8), IsActive = false }, | ||
new Employee1 { Id = 105, Name = "Ronald", Designation = "Senior Data Engineer", DOJ = new DateOnly(1991, 8, 23), IsActive = true }, | ||
new Employee1 { Id = 102, Name = "Line", Designation = "Architect", DOJ = new DateOnly(1977, 1, 12), IsActive = true }, | ||
new Employee1 { Id = 101, Name = "Daniel", Designation = "Architect", DOJ = new DateOnly(1977, 1, 12), IsActive = true }, | ||
new Employee1 { Id = 108, Name = "Zayne", Designation = "Data Analyst", DOJ = new DateOnly(1991, 1, 1), IsActive = true }, | ||
new Employee1 { Id = 109, Name = "Isha", Designation = "App Maker", DOJ = new DateOnly(1996, 7, 1), IsActive = true }, | ||
}; | ||
} | ||
|
||
private Task OnSelectedItemsChanged(HashSet<Employee1> employees) | ||
{ | ||
selectedEmployees = employees is not null && employees.Any() ? employees : new(); | ||
return Task.CompletedTask; | ||
} | ||
|
||
private Task SelectAllEmployees() => gridRef.SelectAllItemsAsync(); | ||
|
||
private Task UnselectAllEmployees() => gridRef.UnSelectAllItemsAsync(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters