From def0cae8e4d5861068306039ec940e89e01a2ff9 Mon Sep 17 00:00:00 2001 From: Mosk915 <59397359+Mosk915@users.noreply.github.com> Date: Mon, 25 Nov 2024 18:05:29 -0500 Subject: [PATCH] Add lazyRender parameter (#1158) Co-authored-by: Yihui Xie --- DESCRIPTION | 2 +- NEWS.md | 1 + R/datatables.R | 6 ++++++ inst/htmlwidgets/datatables.js | 2 +- man/datatable.Rd | 4 ++++ 5 files changed, 13 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index e8408f24..3f78633b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: DT Type: Package Title: A Wrapper of the JavaScript Library 'DataTables' -Version: 0.33.1 +Version: 0.33.2 Authors@R: c( person("Yihui", "Xie", role = "aut"), person("Joe", "Cheng", email = "joe@posit.co", role = c("aut", "cre")), diff --git a/NEWS.md b/NEWS.md index 5e7e7277..9e65aba4 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # CHANGES IN DT VERSION 0.34 +- Added `lazyRender` parameter to `DT::datatable()`, which gives the option for the table to be rendered immediately rather than waiting for it to become visible (thanks, @Mosk915, #1156). # CHANGES IN DT VERSION 0.33 diff --git a/R/datatables.R b/R/datatables.R index 999aec14..aa02e74e 100644 --- a/R/datatables.R +++ b/R/datatables.R @@ -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{FALSE} to render the table immediately on page load, +#' otherwise delay rendering until the table becomes visible. #' @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', @@ -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 = NULL, selection = c('multiple', 'single', 'none'), extensions = list(), plugins = NULL, editable = FALSE ) { @@ -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, '}') diff --git a/inst/htmlwidgets/datatables.js b/inst/htmlwidgets/datatables.js index 765b53cb..136a8b94 100644 --- a/inst/htmlwidgets/datatables.js +++ b/inst/htmlwidgets/datatables.js @@ -180,7 +180,7 @@ HTMLWidgets.widget({ }; }, renderValue: function(el, data, instance) { - if (el.offsetWidth === 0 || el.offsetHeight === 0) { + if ((el.offsetWidth === 0 || el.offsetHeight === 0) && data.lazyRender !== false) { instance.data = data; return; } diff --git a/man/datatable.Rd b/man/datatable.Rd index a3bef5f9..f52bab7a 100644 --- a/man/datatable.Rd +++ b/man/datatable.Rd @@ -21,6 +21,7 @@ datatable( elementId = NULL, fillContainer = getOption("DT.fillContainer", NULL), autoHideNavigation = getOption("DT.autoHideNavigation", NULL), + lazyRender = NULL, selection = c("multiple", "single", "none"), extensions = list(), plugins = NULL, @@ -105,6 +106,9 @@ then vertical and/or horizontal scrolling of the table cells will occur.} than the page size. Note, it only works on the client-side processing mode and the `pageLength` option should be provided explicitly.} +\item{lazyRender}{\code{FALSE} to render the table immediately on page load, +otherwise delay rendering until the table becomes visible.} + \item{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',