javascriptjquerydatatablepager

change dataTables page number in resize


I'm using dataTables (https://datatables.net) and trying to do a responsive page number, I need this:

Desktop: first prev 1 2 3 4 5 next last

Mobile: first prev 1 2 3 next last

Actually with my code I get this on mobile, nothing change: first prev 1 2 3 4 5 next last

This is my code:

$(window).resize(function () {
   if ($(this).outerWidth() < 768){
      // change the dataTable pageLength in here
      $.fn.DataTable.ext.pager.numbers_length = 3;     
   } else {
      // default pageLength
      $.fn.DataTable.ext.pager.numbers_length = 5;
   }
});

Solution

  • I needed the same solution and solved it only by redrawing the page area.

    https://datatables.net/reference/api/draw()

    let $landingPaginationTable = $('.landing-table.pagination-table');
    
    $(window).resize(() => {
        if (window.screen.width < 768) {
            $.fn.DataTable.ext.pager.numbers_length = 3;
        } else if (window.screen.width <= 1024) {
            $.fn.DataTable.ext.pager.numbers_length = 5;
        } else {
            $.fn.DataTable.ext.pager.numbers_length = 7;
        }
    
        $landingPaginationTable.DataTable().draw('page');
    });