以编程方式更改DataTables的DisplayLength

时间:2022-12-06 14:29:06

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.

我一直在使用dataTables,它是一个可靠的工具。可以选择设置初始显示长度iDisplayLength。我想在表初始化后以编程方式更改显示长度。

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

1 个解决方案

#1


4  

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

我不认为有这样做的“公共API”方法。有一个工作,虽然我过去使用过。它在DataTables 1.9.4中工作...不确定任何其他版本

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.

免责声明:我不确定这可能会对内部产生什么影响...我通常会回避手动设置下划线前缀变量,因为作者可能不打算访问没有API的开发人员,但我没有碰到任何副作用......里程可能会有所不同等等。

#1


4  

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

我不认为有这样做的“公共API”方法。有一个工作,虽然我过去使用过。它在DataTables 1.9.4中工作...不确定任何其他版本

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.

免责声明:我不确定这可能会对内部产生什么影响...我通常会回避手动设置下划线前缀变量,因为作者可能不打算访问没有API的开发人员,但我没有碰到任何副作用......里程可能会有所不同等等。