开发者

How to change results per page value in datatables

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 curre开发者_开发百科nt design. Thanx!

http://datatables.net/


The fully correct answer would be to use both and display length to 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.


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

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

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


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

});


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


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

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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜