[英]how can i disable button when the user gets to the last page
I have response in such form我有这种形式的回应
{
"currentPage": 1,
"totalPages": 2,
"totalCount": 2,
"pageSize": 1,
"items": [
{
"productId": 2,
"name": "testPorduct",
"description": "bla bla bla",
"price": 1000,
"state": "new",
"color": "sss",
"rate": 5.5,
"imageFileName": "ooo",
"quantity": 10,
"categoryId": 5
}
],
"hasPrevious": false,
"hasNext": true
}
where i have hasPrevious and hasNext properties, and i need to make button disabled when property hasNext is false.我有 hasPrevious 和 hasNext 属性,当属性 hasNext 为 false 时,我需要禁用按钮。
I use EventEmitter for rerendering page when user change page size or pageNumber, and on every click i make request to the server:当用户更改页面大小或 pageNumber 时,我使用 EventEmitter 重新呈现页面,并且在每次单击时我都会向服务器发出请求:
ngOnInit(): void {
this.restService.getProductsPaged(this.defaultPageSize, this.defaultPageNumber).subscribe(
(data: Product) => this.products = {
currentPage : data.currentPage,
totalPages : data.totalPages,
totalCount : data.totalCount,
pageSize : data.pageSize,
items: data.items,
hasPrevious: data.hasPrevious,
hasNext: data.hasNext,
}
)
}
and for example next page:例如下一页:
next(event){
event.preventDefault;
this.page += 1;
this.pageChange.emit(this.page)
this.restService.getProductsPaged(this.pageSize, this.page).subscribe(
(data: Product) => this.products = {
currentPage : data.currentPage,
totalPages : data.totalPages,
totalCount : data.totalCount,
pageSize : data.pageSize,
items: data.items,
hasPrevious: data.hasPrevious,
hasNext: data.hasNext,
}
)
}
my checkLastPage function我的 checkLastPage function
checkLastPage() : boolean{
var a = this.products.hasNext;
console.log(a);
return !a;
}
and my console.log prints me four times true or false and 'Cannot read property 'hasNext' of undefined' How can i solve this problem?我的console.log 打印我四次真假和'无法读取未定义的属性'hasNext'' 我该如何解决这个问题? Thanks in advance.提前致谢。
The issue you might be having which says that 'Cannot read property 'hasNext' of undefined' occurs because you might not have initialized products object.您可能遇到的问题是“无法读取未定义的属性 'hasNext'”,因为您可能没有初始化产品 object。
You dont need to create a separate function just to disable the button.您不需要创建单独的 function 来禁用按钮。 You can directly use [disabled] directive in angular.您可以直接使用 angular 中的 [disabled] 指令。
For Example:例如:
<button (click)="next($event)" [disabled]="!products || !products.hasNext">
</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.