Cessna
Cessna

Reputation: 309

How to change table's page length dynamically

Is there a way to change the pageLength setting of the dataTable on runtime within the "window.resize" event of jQuery?

These are the dataTable settings I'm using

$('#dataTable').DataTable({
    paging: true,
    pageLength: 35,
    searching: true,
    lengthChange: false,
    info: false,
    scrollCollapse: true,
    scrollY: "calc(74vh)"
});

I want the pageLength to change, whenever the window is resized.

I'm trying this

$(window).resize(function () {
    if ($(this).height() >= "1080"){
        // change the dataTable pageLength in here
        $('#dataTable').DataTable({ pageLength: 50 });
    } else {
        // default pageLength
        $('#dataTable').DataTable({ pageLength: 35 });
    }
});

Upvotes: 9

Views: 30220

Answers (3)

Mitul Pokiya
Mitul Pokiya

Reputation: 172

The Javascript shown below is used to initialise the table with the length of the page shown

new DataTable('#example', {
    lengthMenu: [
        [10, 25, 50, -1],
        [10, 25, 50, 'All']
    ]
});

Upvotes: 0

Fernand Zangue
Fernand Zangue

Reputation: 11

To change the pageLength parameter use:

var table=$('#dataTable').DataTable({
    paging: true,
    sort: true,
    scrollX: true,
    searching: true,
    lengthMenu: [[2,5,10,25, 100, -1], [2,5,10,25, 100, "All"]],
    pageLength: 5,
});
table.page.len(-1).draw();

Upvotes: 1

Gyrocode.com
Gyrocode.com

Reputation: 58880

Use page.len() API function to change page length dynamically.

$(window).resize(function () {
    if ($(this).height() >= 1080){
        // change the dataTable pageLength in here
        $('#dataTable').DataTable().page.len(50).draw();
    } else {
        // default pageLength
        $('#dataTable').DataTable().page.len(35).draw();
    }
});

Upvotes: 22

Related Questions