1
+ <div class =" mb-3" >
2
+ <Button Type =" ButtonType.Button" Color =" ButtonColor.Primary" Size =" ButtonSize.Small" @onclick =" AddEmployee" > Add Employee </Button >
3
+ <Button Type =" ButtonType.Button" Color =" ButtonColor.Danger" Size =" ButtonSize.Small" @onclick =" RemoveEmployee" > Remove Employee </Button >
4
+ </div >
5
+
6
+ <Grid TItem =" Employee1"
7
+ @ref =" grid"
8
+ Class =" table table-hover table-bordered table-striped"
9
+ DataProvider =" EmployeesDataProvider"
10
+ AllowFiltering =" true"
11
+ AllowPaging =" true"
12
+ PageSize =" 5"
13
+ AllowSorting =" true"
14
+ Responsive =" true" >
15
+
16
+ <GridColumns >
17
+ <GridColumn TItem =" Employee1" HeaderText =" Id" PropertyName =" Id" SortKeySelector =" item => item.Id" >
18
+ @context.Id
19
+ </GridColumn >
20
+ <GridColumn TItem =" Employee1" HeaderText =" Employee Name" PropertyName =" Name" FilterTextboxWidth =" 50" SortKeySelector =" item => item.Name" >
21
+ @context.Name
22
+ </GridColumn >
23
+ <GridColumn TItem =" Employee1" HeaderText =" Designation" PropertyName =" Designation" SortKeySelector =" item => item.Designation" >
24
+ @context.Designation
25
+ </GridColumn >
26
+ <GridColumn TItem =" Employee1" HeaderText =" DOJ" PropertyName =" DOJ" SortKeySelector =" item => item.DOJ" >
27
+ @context.DOJ
28
+ </GridColumn >
29
+ <GridColumn TItem =" Employee1" HeaderText =" Active" PropertyName =" IsActive" SortKeySelector =" item => item.IsActive" >
30
+ @context.IsActive
31
+ </GridColumn >
32
+ </GridColumns >
33
+
34
+ </Grid >
35
+
36
+ @code {
37
+ Grid <Employee1 > grid = default ! ;
38
+ private List <Employee1 >? employees = default ! ;
39
+
40
+ private async Task <GridDataProviderResult <Employee1 >> EmployeesDataProvider (GridDataProviderRequest < Employee1 > request )
41
+ {
42
+ if (employees is null ) // pull employees only one time for client-side filtering, sorting, and paging
43
+ employees = GetEmployees (); // call a service or an API to pull the employees
44
+
45
+ return await Task .FromResult (request .ApplyTo (employees ));
46
+ }
47
+
48
+ private List <Employee1 > GetEmployees ()
49
+ {
50
+ return new List <Employee1 >
51
+ {
52
+ new Employee1 { Id = 107 , Name = " Alice" , Designation = " AI Engineer" , DOJ = new DateOnly (1998 , 11 , 17 ), IsActive = true },
53
+ new Employee1 { Id = 103 , Name = " Bob" , Designation = " Senior DevOps Engineer" , DOJ = new DateOnly (1985 , 1 , 5 ), IsActive = true },
54
+ new Employee1 { Id = 106 , Name = " John" , Designation = " Data Engineer" , DOJ = new DateOnly (1995 , 4 , 17 ), IsActive = true },
55
+ new Employee1 { Id = 104 , Name = " Pop" , Designation = " Associate Architect" , DOJ = new DateOnly (1985 , 6 , 8 ), IsActive = false },
56
+ new Employee1 { Id = 105 , Name = " Ronald" , Designation = " Senior Data Engineer" , DOJ = new DateOnly (1991 , 8 , 23 ), IsActive = true },
57
+ new Employee1 { Id = 102 , Name = " Line" , Designation = " Architect" , DOJ = new DateOnly (1977 , 1 , 12 ), IsActive = true },
58
+ new Employee1 { Id = 101 , Name = " Daniel" , Designation = " Architect" , DOJ = new DateOnly (1977 , 1 , 12 ), IsActive = true },
59
+ new Employee1 { Id = 113 , Name = " Merlin" , Designation = " Senior Consultant" , DOJ = new DateOnly (1989 , 10 , 2 ), IsActive = true },
60
+ new Employee1 { Id = 117 , Name = " Sharna" , Designation = " Data Analyst" , DOJ = new DateOnly (1994 , 5 , 12 ), IsActive = true },
61
+ new Employee1 { Id = 108 , Name = " Zayne" , Designation = " Data Analyst" , DOJ = new DateOnly (1991 , 1 , 1 ), IsActive = true },
62
+ new Employee1 { Id = 109 , Name = " Isha" , Designation = " App Maker" , DOJ = new DateOnly (1996 , 7 , 1 ), IsActive = true },
63
+ new Employee1 { Id = 111 , Name = " Glenda" , Designation = " Data Engineer" , DOJ = new DateOnly (1994 , 1 , 12 ), IsActive = true },
64
+ };
65
+ }
66
+
67
+ private async Task AddEmployee ()
68
+ {
69
+ if (employees is null )
70
+ employees = new ();
71
+
72
+ // for the same employees collection, we are adding an object
73
+ // explicit grid refresh required
74
+ employees .Add (CreateEmployee ());
75
+ await grid .RefreshDataAsync ();
76
+ }
77
+
78
+ private Employee1 CreateEmployee ()
79
+ {
80
+ var emp = new Employee1 ();
81
+ emp .Id = (employees ? .Any () ?? false ) ? employees .Max (x => x .Id ) + 1 : 1 ;
82
+ emp .Name = $" Employee {emp .Id }" ;
83
+ emp .Designation = $" QA Engineer {emp .Id }" ;
84
+ emp .DOJ = new DateOnly (new Random ().Next (1970 , 2000 ), new Random ().Next (1 , 12 ), new Random ().Next (1 , 25 ));
85
+ emp .IsActive = true ;
86
+ return emp ;
87
+ }
88
+
89
+ private async Task RemoveEmployee ()
90
+ {
91
+ if (employees ! .Count > 0 )
92
+ employees ! .Remove (employees .Last ());
93
+
94
+ await grid .RefreshDataAsync ();
95
+ }
96
+ }
0 commit comments