繁体   English   中英

如何在DataTables Angular 5的下一个/上一个分页按钮上捕获单击事件

[英]How to catch an click event on pagination buttons next/previous of the DataTables Angular 5

不幸的是,如何在角度5的上一个/下一个分页按钮中捕获单击事件,不幸的是我在jquery中获得了代码。

jQuery代码:

var table = $('#example').DataTable({
 drawCallback: function(){
  $('.paginate_button.next', this.api().table().container())          
  .on('click', function(){
  alert('next');
 });       
 }
}); 

#stack溢出链接

#jsFiddle链接

但是我需要角度数据表。 如果有示例,如何捕获上一个/下一个单击事件。

按钮行单击事件

 buttonInRowClick(event: any): void {
    event.stopPropagation();
    console.log('Button in the row clicked.');
  }

全行点击事件

wholeRowClick(): void {
    console.log('Whole row clicked.');
  }

预期下一个/上一个按钮单击事件

nextButtonClickEvent():void {
   //do next particular records like  101 - 200 rows.
   //we are calling to api
}
previousButtonClickEvent():void {
  //do previous particular the records like  0 - 100 rows.
   //we are calling to API
}

因为我的后端有成千上万的记录。

我建议使用ngx-datatable库,非常完整且易于使用! 连文档都很好! https://github.com/swimlane/ngx-datatable

解决方案是通过在on()调用中为目标元素提供选择器作为第二个参数来使用事件委托,请参见下面的示例。 根据jQuery on()文档

您需要提供有关要绑定功能的元素的Angular组件的引用

<div #mytable></div>

在您的组件中添加带有ViewChild的元素引用

@ViewChild('mytable') tableRef: ElementRef;

然后,使用该元素引用来绑定所需的功能,并使其可以按角度访问

ngAfterViewInit(): void {
  this.table = $(this.tableRef.nativeElement).DataTable({
      drawCallback: function () {
        $('.paginate_button.next', this.api().table().container()).on('click', () =>  {
          // your angular method here
          this.nextButtonClickEvent();
        });
      }
  });
}

只需测试您的范围。 通过使用箭头函数,您可以访问类中编写的方法,而该方法可以解决问题

您可以在组件内部使用任何jquery代码,并使用arrow函数维护this引用

零件

declare let $: any;
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {


  ngOnInit() {
    let table = $('#example').DataTable({
      drawCallback: () => {
        $('.paginate_button.next')
          .on('click', () => {
            this.nextButtonClickEvent();
          });
      }
    });
  }

  nextButtonClickEvent(): void {
       console.log('next clicked');
  }

}

stackblitz演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM