Skip to content

Commit

Permalink
Add lazyRender parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
COMZM committed Nov 25, 2024
1 parent 4e63a46 commit 6c6fa73
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# CHANGES IN DT VERSION 0.34

- Added `lazyRender` parameter to `datatable`, which gives the option for the table to be rendered immediately rather than waiting for it to become visible (#1156).

# CHANGES IN DT VERSION 0.33

Expand Down
6 changes: 6 additions & 0 deletions R/datatables.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
#' (only display the table body) when the number of total records is less
#' than the page size. Note, it only works on the client-side processing mode
#' and the `pageLength` option should be provided explicitly.
#' @param lazyRender \code{TRUE} to delay rendering until the table becomes visible
#' (the default) or \code{FALSE} to render the table immediately on page load.
#' @param selection the row/column selection mode (single or multiple selection
#' or disable selection) when a table widget is rendered in a Shiny app;
#' alternatively, you can use a list of the form \code{list(mode = 'multiple',
Expand Down Expand Up @@ -207,6 +209,7 @@ datatable = function(
escape = TRUE, style = 'auto', width = NULL, height = NULL, elementId = NULL,
fillContainer = getOption('DT.fillContainer', NULL),
autoHideNavigation = getOption('DT.autoHideNavigation', NULL),
lazyRender = TRUE,
selection = c('multiple', 'single', 'none'), extensions = list(), plugins = NULL,
editable = FALSE
) {
Expand Down Expand Up @@ -375,6 +378,9 @@ datatable = function(
params$autoHideNavigation = autoHideNavigation
}

# record lazyRender
params$lazyRender = lazyRender

params = structure(modifyList(params, list(
data = data, container = as.character(container), options = options,
callback = if (!missing(callback)) JS('function(table) {', callback, '}')
Expand Down
4 changes: 3 additions & 1 deletion inst/htmlwidgets/datatables.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ HTMLWidgets.widget({
};
},
renderValue: function(el, data, instance) {
if (el.offsetWidth === 0 || el.offsetHeight === 0) {
if (!data.hasOwnProperty("lazyRender"))
data.lazyRender = true;
if ((el.offsetWidth === 0 || el.offsetHeight === 0) && data.lazyRender) {
instance.data = data;
return;
}
Expand Down

0 comments on commit 6c6fa73

Please sign in to comment.