Programmatically Change the DisplayLength of DataTables

datatables, javascript, jquery-ui

Solution

I don't think there's a "public API" method of doing this. There is a work around, though I've used in the past. It does work in `DataTables 1.9.4`... not sure about any other versions

var table = $('#table').dataTable();
table.fnSettings()._iDisplayLength = displayLength; //variable from your question
table.fnDraw(); //redraws the table

Disclaimer: I'm not sure what this may affect internally... I usually shy away from setting underscore-prefixed variables manually as the author likely isn't intending on developers accessing those without the API, but I haven't run into any side-effects... Mileage may vary, etc etc.

Problem

I have been using dataTables for a while and its a solid tool. There is a option for setting the initial display length, iDisplayLength. I would like to change the display length programmatically after the table has initialized. My custom data table widget lives inside a modal. When I resize the modal I resize the datatable. I would like to also adjust the Display Length in the same `resize` function. ``` resize: function( event, ui ) { $('.dataTables_scroll').css('height', magicRation * $('#modal').height() ); $('.dataTables_scrollBody').css('height', $('.dataTables_scroll').height() - 91 ); }, ``` ``` var displayLength = Math.floor($('.dataTables_scrollBody').outerHeight() / $('#DataTables_Table_0 > tbody td').first().outerHeight()); ```

Original source