【问题标题】:How to catch an click event on pagination buttons next/previous of the DataTables Angular 5如何在 DataTables Angular 5 的下一个/上一个分页按钮上捕获单击事件
【发布时间】:2019-02-23 15:17:18
【问题描述】:

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

jquery 代码:

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

#stack overflow link

#jsFiddle link

但我需要角度数据表。如果你有一个例子,如何捕捉上一个/下一个点击事件。

按钮行点击事件

 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
}

因为我的后端有数千条记录。

【问题讨论】:

  • 你能分享一些你想要的与角度相关的代码吗??
  • 嗨,@malbarmawi 感谢您的快速回复。我在角度数据表中添加了更新的示例代码。请检查this link 一次。他在 jquery 中做过

标签: javascript angular datatable pagination angular-datatables


【解决方案1】:

我推荐使用 ngx-datatable 库,非常完整且易于使用!甚至文档也很好! https://github.com/swimlane/ngx-datatable

【讨论】:

  • C 感谢 ngx-datatable 的响应,它很好。但我有数百万条记录。当我单击下一步按钮时,初始时间仅加载 100 条过滤记录,例如 1-100。我预计只有接下来的 100 条记录,例如 101 - 200。不支持 ngx-datatable
  • ngx-datatable 还支持服务器端分页,传递页面 id(不知道你的 API 是不是这样构建的 :)):swimlane.github.io/ngx-datatable/#server-paging
【解决方案2】:

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

【讨论】:

  • 给我一些 angular2 代码的例子,而不是 jQuery。
【解决方案3】:

你需要为你的 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();
        });
      }
  });
}

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

【讨论】:

  • 嗨,@Antonis 我收到错误 ERROR TypeError: Cannot read property 'nativeElement' of undefined
  • 这几乎意味着'tableRef'未定义。你在课堂上添加了:@ViewChild('mytable') tableRef: ElementRef; 吗?
  • 是的@Antonis,我添加了你的代码。 @ViewChild('mytable') tableRef: ElementRef;。但我没有定义。
【解决方案4】:

您可以在组件内使用任何 jquery 代码并使用箭头函数来维护对 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 demo

【讨论】:

  • 感谢您的努力,我没有收到任何错误,我没有收到next clicked。我使用 angular 5 你使用 angular 6
  • 它适用于所有版本,您可以在 stackblitz 演示中看到,它是否适用于您的项目? @Senthil
  • 我看到了你的 stackblitz 演示。在我的项目中无法使用您的代码
  • 您遇到了什么样的错误,您是否检查过您将功能更改为箭头功能
  • 我正在使用 angular-datable 插件。显示我收到警告错误 DataTables 警告:表 id={id} - 无法重新初始化 DataTable。 我的代码 &lt;table id="geotesting" datatable [dtOptions]="dtOptions" [dtTrigger]="dtTrigger"&gt; npm angular-datatables
猜你喜欢
  • 2017-06-28
  • 2017-10-29
  • 1970-01-01
  • 2021-06-12
  • 2016-07-12
  • 1970-01-01
  • 2018-07-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多