最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to catch an event on pagination buttons nextprevious of the DataTables - Stack Overflow

programmeradmin2浏览0评论

Can anybody show me an example of how to catch the events on pagination buttons next/previous of datatables? In particular I'm interested for the "next" button. It would really help me if you have an example of how to catch the event of a particular pagination button.

I have searched and in datatable and found that to catch an event you should use this :

$('#example').on('page.dt', function ()).DataTable();

But this catches the events for all the pagination buttons. I want to know how to do it for a particular one("next" in my case).

Thanks in advance

Can anybody show me an example of how to catch the events on pagination buttons next/previous of datatables? In particular I'm interested for the "next" button. It would really help me if you have an example of how to catch the event of a particular pagination button.

I have searched and in datatable and found that to catch an event you should use this :

$('#example').on('page.dt', function ()).DataTable();

But this catches the events for all the pagination buttons. I want to know how to do it for a particular one("next" in my case).

Thanks in advance

Share Improve this question asked Feb 10, 2017 at 14:30 JurgenJurgen 1311 gold badge2 silver badges6 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 16

Use the code below to attach click event handler to "Next" pagination button.

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

See this jsFiddle for code and demonstration.

drawCallback: function () {
    var table = this.api();
    var pageInfo = table.page.info();

    $('.paginate_button.next:not(.disabled)', table.table().container())
        .on('click', function () {
            var nextPageIndex = pageInfo.page ;
            alert('Next page index: ' + nextPageIndex);
        });

    // Event handler for page number buttons
    $('.paginate_button:not(.next, .previous)', table.table().container())
        .on('click', function () {
            var clickedPageIndex = $(this).text() - 1; // Adjust to zero-based index
            alert('Clicked page index: ' + clickedPageIndex);
        });

    // Event handler for the "Previous" button
    $('.paginate_button.previous:not(.disabled)', table.table().container())
        .on('click', function () {
            var previousPageIndex = pageInfo.page - 1;
            alert('Previous page index: ' + previousPageIndex);
        });
},
发布评论

评论列表(0)

  1. 暂无评论