简体   繁体   English

当用户到达最后一页时如何禁用按钮

[英]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.

相关问题 我可以使用后退按钮检测用户何时到达页面? - Can I detect when a user gets to a page using the back button? 用户在谷歌浏览器中按“后退”按钮时,如何加载上一个状态? - How can I load last state when user presses back button in google chrome? 我可以只禁用 mat-paginator 中的“显示最后一页”按钮吗? - Can I disable only “show last page” button in mat-paginator? 如何在提交表单时没有任何错误地禁用提交按钮 - how do I disable the submit button when the form gets submitted without any errors 单击按钮提交时如何禁用操作表单? - How can I disable action form when click button submit? 当您 hover 超过它时,如何禁用按钮? - How can I disable a button when you hover over it? 当用户单击jQueryUI单选按钮时,如何停止浏览器视口移至页面顶部? - How can I stop the browser viewport moving to the top of the page when the user clicks on a jQueryUI radio button? 在Jquery Mobile中点击浏览器后退按钮时,如何防止用户离开页面? - how can I prevent the user leaving the page when hitting the browser back button in Jquery Mobile? 显示负载掩码时如何禁用用户操作 - How can I disable user actions when load mask is shown 单击复选框时如何禁用页面重定向? - How I can disable page redirect when click on checkbox?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM