-
Notifications
You must be signed in to change notification settings - Fork 0
/
Paging
97 lines (90 loc) · 3.22 KB
/
Paging
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
### Paging C#
```csharp
// hàm set data cho paging data (đã tạo rồi)
private void SetPagingData<T>(PagingData pagingData, IQueryable<T> query)
{
pagingData.ItemTotal = query.Count();
pagingData.PageTotal = pagingData.GetPageTotal();
pagingData.PageIndex = pagingData.GetPageIndex();
if (pagingData.PageSize.HasValue)
{
var skip = pagingData.GetSkipNumber();
var take = pagingData.PageSize.Value;
query = query.Skip(skip).Take(take);
}
}
public Acknowledgement<DataWithPaging<Product>> GetProductListTest(SearchModel searchModel)
{
var ack = new Acknowledgement<DataWithPaging<Product>>();
var db = POSReadOnlyContext;
// Tạo queryable bình thường
var query = db.Products.Include(i => i.ProductAmountThreshold)
.Include(i => i.ProductUnit)
.Include(i => i.ProductBatches.Select(j => j.Transactions))
.AsQueryable();
// filter theo search value
var condition = $"Name.Contains(\"{searchModel.SearchValue}\")";
query = System.Linq.Dynamic.DynamicQueryable.Where(query, condition);
// skip take paging
SetPagingData(searchModel.PagingData, query);
// lay data ve memory
var data = query.ToList();
ack.Data = new DataWithPaging<Product>()
{
Data = data,
PagingData = searchModel.PagingData,
};
ack.IsSuccess = true;
return ack;
}
```
### Paging javascript
```jsx
_getData = () => {
const { searchModel, } = ~~
ajax.post({
url: `/api/xxx/xxx/`,
data: JSON.stringify(searchModel),
successCallback: ack => {
const _data = ack.data.data // data đã paging
const _pagingData = ack.data.pagingData // paging data đã được tính toán
// update state ở đây
}
})
}
_gotoNext = () => {
const { pagingData } = ~~
if (pagingData.pageIndex < pagingData.pageTotal) {
this._gotoPage(pagingData.pageIndex + 1);
}
}
_gotoPrev = () => {
const { pagingData } = ~~
if (pagingData.pageIndex > 1) {
this._gotoPage(pagingData.pageIndex - 1);
}
}
_gotoPage = (index) => {
const { searchModel, } = ~~
this.updateObject(searchModel.pagingData, { pageIndex: index }, () => {
this._getData();
})
}
_gotoFirstPage = () => {
this._gotoPage(1);
}
_gotoLastPage = () => {
const { searchModel, } = ~~
this._gotoPage(searchModel.pagingData.pageTotal);
}
import Pagination from 'components/Pagination';
<Pagination
total={pagingData.pageTotal}
current={pagingData.pageIndex}
onClickNumber={this._gotoPage}
onClickNext={this._gotoNext}
onClickPrev={this._gotoPrev}
onClickFirst={this._gotoFirstPage}
onClickLast={this._gotoLastPage}
/>
```