Javascript 如何更改数据表中每页值的结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2871705/
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-08-23 02:16:56  来源:igfitidea点击:

How to change results per page value in datatables

javascriptdatatable

提问by detj

Datatables has an option to select the number of records shown per page. The default value starts from 10, 25, 50 and 100. How can I change it to start from 5 instead of 10? 10 records is a bit too much and takes a lot of space in my current design. Thanx!

Datatables 有一个选项可以选择每页显示的记录数。默认值从 10、25、50 和 100 开始。如何将其更改为从 5 开始而不是 10?在我目前的设计中,10 条记录有点太多了,占用了很多空间。谢谢!

http://datatables.net/

http://datatables.net/

回答by TheFrack

The fully correct answer would be to use both and display length to 5:

完全正确的答案是同时使用和显示长度为 5:

$(document).ready( function(){
    $('#table').dataTable({
    "iDisplayLength": 5,
    "aLengthMenu": [[5, 10, 25, 50, -1], [5, 10, 25, 50, "All"]]
    });
});

If you use JUST "iDisplayLength", then the dropdown will not have that length in options later or when the page loads (instead you will see the first option, IE 10 by default). If you JUST use "aLengthMenu", then your results will still default to 10 instead of the first menu option.

如果您仅使用“iDisplayLength”,那么稍后或页面加载时,下拉列表中的选项中将没有该长度(相反,您将看到第一个选项,默认情况下为 IE 10)。如果您只是使用“aLengthMenu”,那么您的结果仍将默认为 10,而不是第一个菜单选项。

回答by seneyr

You will want to use the iDisplayLength parameter when you initialize the DataTable object. Here's the example they list in their documentation:

在初始化 DataTable 对象时,您将需要使用 iDisplayLength 参数。这是他们在文档中列出的示例:

$(document).ready( function() {
    $('#example').dataTable( {
        "iDisplayLength": 50
    } );
} )

More information can be found here: http://www.datatables.net/usage/options

更多信息可以在这里找到:http: //www.datatables.net/usage/options

回答by Asmita Savaliya

$.extend(true, $.fn.dataTable.defaults, {
    "lengthMenu": [[5, 10, 15, 20, 25], [5, 10, 15, 20, 25]],
    "pageLength": 5

});

回答by Captain Red

The answer solved my problem of needing the following scenario

答案解决了我需要以下场景的问题

$(document).ready( function(){
    $('#table').dataTable({
  "aLengthMenu": [[10, 25, 50, 100], ["10 Per Page", "25 Per Page", "50 Per Page", "100 Per Page"]]
    });
});

回答by Greg M.

I realize that this question is old, but the accepted answer does not answer the OP's question.

我意识到这个问题很旧,但接受的答案并没有回答 OP 的问题。

The answer is to override the aLengthMenu option when initializing the dataTable. See here: http://datatables.net/examples/advanced_init/length_menu.html

答案是在初始化数据表时覆盖 aLengthMenu 选项。见这里:http: //datatables.net/examples/advanced_init/length_menu.html

回答by Gulfam Khan

It hardly for the the data tables 1.9
"iDisplayLength": 50

数据表 1.9
"iDisplayLength": 50

回答by André van Rensburg

You can simply add:

您可以简单地添加:

"lengthMenu": [ 
 [10, 25, 50, -1], 
 [10, 25, 50, "All"] 
] // remember to add  "," if you initialize more option manually

or if you only want to add this option

或者如果您只想添加此选项

$('#tablename').dataTable( {
  "lengthMenu": [ [10, 25, 50, -1], [10, 25, 50, "All"] ]
} );

which will give you a drop-down to select the number of records per page in pagination.

这将为您提供一个下拉菜单,以选择分页中每页的记录数。