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

javascript - How to change table's page length dynamically - Stack Overflow

programmeradmin7浏览0评论

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

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 });
    }
});
Share Improve this question edited Jul 17, 2015 at 18:20 Gyrocode. 58.9k16 gold badges156 silver badges191 bronze badges asked Jul 17, 2015 at 17:56 CessnaCessna 3092 gold badges8 silver badges21 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 22

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

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

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']
    ]
});
发布评论

评论列表(0)

  1. 暂无评论