From 6c6fa73fd1b6b0eb41831c0d75e55c1a31e92c48 Mon Sep 17 00:00:00 2001 From: COMZM Date: Mon, 25 Nov 2024 15:34:58 -0500 Subject: [PATCH] Add lazyRender parameter --- NEWS.md | 1 + R/datatables.R | 6 ++++++ inst/htmlwidgets/datatables.js | 4 +++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 5e7e7277..c163497d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/datatables.R b/R/datatables.R index 999aec14..2e4355bf 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{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', @@ -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 ) { @@ -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..c484e50f 100644 --- a/inst/htmlwidgets/datatables.js +++ b/inst/htmlwidgets/datatables.js @@ -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; }