javascript 如何动态更改表格的页长

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31481910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 13:52:26  来源:igfitidea点击:

How to change table's page length dynamically

javascriptjquerydatatabledatatableswindow-resize

提问by Cessna

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

有没有办法pageLength在 jQuery 的“window.resize”事件中在运行时更改dataTable的设置?

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 pageLengthto change, whenever the window is resized.

我希望在pageLength调整窗口大小时进行更改。

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

回答by Gyrocode.com

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

使用page.len()API 函数动态更改页面长度。

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