1
1
@page " /custom-properties"
2
- @inject HttpClient Http
3
- @using HyperCache .Shared .Dtos
4
2
5
3
<h3 class =" mt-4 text-primary" >Custom Properties</h3 >
6
4
7
- @if (properties == null )
5
+ @if (pagedResponse == null )
8
6
{
9
7
<div class =" text-center mt-4" >
10
8
<div class =" spinner-border text-primary" role =" status" ></div >
11
9
<p >Loading .. .</p >
12
10
</div >
13
11
}
14
- else if (! properties .Any ())
12
+ else if (! pagedResponse . Items .Any ())
15
13
{
16
14
<div class =" alert alert-info mt-4" role =" alert" >
17
15
No properties found .
28
26
</tr >
29
27
</thead >
30
28
<tbody >
31
- @foreach ( var property in properties )
29
+ @foreach ( var property in pagedResponse . Items )
32
30
{
33
31
<tr >
34
32
<td >@property.Name </td >
40
38
}
41
39
</tbody >
42
40
</table >
41
+
42
+ <nav class =" mt-4" >
43
+ <ul class =" pagination" >
44
+ <li class =" page-item @(pagedResponse.HasPreviousPage ? " " : " disabled " )" >
45
+ <button class =" page-link" @onclick =" PreviousPage" >Previous </button >
46
+ </li >
47
+ @foreach ( var pageNumber in GetPageNumbers ())
48
+ {
49
+ <li class =" page-item @(currentPage == pageNumber ? " active " : " " )" >
50
+ <button class =" page-link" @onclick =" () => ChangePage(pageNumber)" >@pageNumber </button >
51
+ </li >
52
+ }
53
+ <li class =" page-item @(pagedResponse.HasNextPage ? " " : " disabled " )" >
54
+ <button class =" page-link" @onclick =" NextPage" >Next </button >
55
+ </li >
56
+ </ul >
57
+ </nav >
43
58
}
44
59
45
60
@code {
46
- private List <CustomPropertyDto >? properties ;
61
+ private PagedResponseDto <CustomPropertyDto >? pagedResponse ;
62
+ private int currentPage = 1 ;
63
+ private int pageSize = 20 ;
47
64
48
65
protected override async Task OnInitializedAsync ()
49
66
{
50
- properties = await Http .GetFromJsonAsync <List <CustomPropertyDto >>(" CustomProperties/paged?page=1&pageSize=20" );
67
+ await LoadProperties ();
68
+ }
69
+
70
+ private async Task LoadProperties ()
71
+ {
72
+ pagedResponse = await Http .GetFromJsonAsync <PagedResponseDto <CustomPropertyDto >>(
73
+ $" {ApiConstant .CustomPropertiesPaged }?page={currentPage }&pageSize={pageSize }" );
74
+ }
75
+
76
+ private async Task ChangePage (int page )
77
+ {
78
+ currentPage = page ;
79
+ await LoadProperties ();
80
+ }
81
+
82
+ private async Task PreviousPage ()
83
+ {
84
+ if (pagedResponse ? .HasPreviousPage == true )
85
+ {
86
+ currentPage -- ;
87
+ await LoadProperties ();
88
+ }
89
+ }
90
+
91
+ private async Task NextPage ()
92
+ {
93
+ if (pagedResponse ? .HasNextPage == true )
94
+ {
95
+ currentPage ++ ;
96
+ await LoadProperties ();
97
+ }
98
+ }
99
+
100
+ private IEnumerable <int > GetPageNumbers ()
101
+ {
102
+ const int maxVisiblePages = 5 ;
103
+ int startPage = Math .Max (1 , currentPage - maxVisiblePages / 2 );
104
+ int endPage = Math .Min (startPage + maxVisiblePages - 1 , pagedResponse ? .TotalPages ?? 1 );
105
+
106
+ for (int i = startPage ; i <= endPage ; i ++ )
107
+ {
108
+ yield return i ;
109
+ }
51
110
}
52
- }
111
+ }
0 commit comments