简体   繁体   English

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

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

How to catch click event in pagination buttons previous/next in angular 5. unfortunately I got the code in jquery. 不幸的是,如何在角度5的上一个/下一个分页按钮中捕获单击事件,不幸的是我在jquery中获得了代码。

jquery code: jQuery代码:

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

#stack overflow link #stack溢出链接

#jsFiddle link #jsFiddle链接

But I need angular data table. 但是我需要角度数据表。 If you have an example, how to catch the previous/next click event. 如果有示例,如何捕获上一个/下一个单击事件。

Button Row Click event 按钮行单击事件

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

Whole Row Click Event 全行点击事件

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

Expecting next/previous button click event 预期下一个/上一个按钮单击事件

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
}

Because I have thousands of records in my backend. 因为我的后端有成千上万的记录。

I recommend using ngx-datatable library, very complete and easy to use ! 我建议使用ngx-datatable库,非常完整且易于使用! Even the documentation is good ! 连文档都很好! https://github.com/swimlane/ngx-datatable https://github.com/swimlane/ngx-datatable

The solution is to use event delegation by providing selector for target element as a second argument in on() call, see example below. 解决方案是通过在on()调用中为目标元素提供选择器作为第二个参数来使用事件委托,请参见下面的示例。 According to jQuery on() documentation 根据jQuery on()文档

You need to provide a reference to your Angular component about the element that you are going to bind the functionality 您需要提供有关要绑定功能的元素的Angular组件的引用

<div #mytable></div>

In your component add an element reference with ViewChild 在您的组件中添加带有ViewChild的元素引用

@ViewChild('mytable') tableRef: ElementRef;

Then use that element reference to bind the functionality that you want and make it accessible in angular 然后,使用该元素引用来绑定所需的功能,并使其可以按角度访问

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();
        });
      }
  });
}

Just test your scope. 只需测试您的范围。 By using arrow functions you can access the methods written in you class and that should do the trick 通过使用箭头函数,您可以访问类中编写的方法,而该方法可以解决问题

You can use any jquery code inside you component and use arrow function to maintain reference to this 您可以在组件内部使用任何jquery代码,并使用arrow函数维护this引用

component 零件

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 demo stackblitz演示

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

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