Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery datatables, programmatically change number of rows displayed

Is there any way to programmatically change the number of rows shown without having to manually select from the dropdown?

I know how to change the default number of rows already. When the table first loads, I want it to load all the rows, and then "refresh" the table to maybe only show the first 10 rows. But I want to refresh the table programmitcally instead of having to select a number from the dropdown.

My issue is that if the default number of rows is less than the total number of rows, then when the hidden rows are shown after changing the dropdown number or by paging through the rows, those rows do not have all the css nor js functionality that I attributed to all the rows. For some reason only the default shown rows have all the functionality, and rows later showed do not.

I figure the simplest way to fix this with the least amount of code is to just load all rows by default and then programmitically change how many rows are being showed to only show the first 10 or so.

Thanks for any help.

like image 700
user2140916 Avatar asked Mar 06 '13 16:03

user2140916


2 Answers

At first you should set datatables as variable:

var oTable = $('#some_selector').dataTable({
    //some properties
})

and then you can set settings on the fly:

var oSettings = oTable.fnSettings();
oSettings._iDisplayLength = 5;

and simply redraw your datatables with new settings:

oTable.fnDraw();
like image 90
Lukasz Koziara Avatar answered Oct 10 '22 13:10

Lukasz Koziara


previous answer did not work for me, could be that I'm using a newer version of DT. to set displayLength to 20 this worked for me:

table.context["0"]._iDisplayLength = "20";
table.draw();

I'm using datatables 1.10.10

like image 25
cecsil Avatar answered Oct 10 '22 13:10

cecsil