Skip to content

Adds support for hiding columns in updateReactable #344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions R/shiny.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#' or `NA` to deselect all rows.
#' @param expanded Expanded rows. Either `TRUE` to expand all rows, or `FALSE`
#' to collapse all rows.
#' @param hidden Hidden columns. A character vector that contains the column names to be
#' hidden.
#' @param page The current page. A single, positive integer.
#' @param meta Custom table metadata. Either a named list with new values, or `NA`
#' to clear all metadata. New values are merged into the current metadata, so only
Expand All @@ -33,6 +35,7 @@
#' actionButton("select_btn", "Select rows"),
#' actionButton("clear_btn", "Clear selection"),
#' actionButton("expand_btn", "Expand rows"),
#' actionButton("hide_btn", "Hide columns"),
#' actionButton("collapse_btn", "Collapse rows"),
#' actionButton("page_btn", "Change page"),
#' selectInput("filter_type", "Filter type", unique(data$Type), multiple = TRUE),
Expand Down Expand Up @@ -65,6 +68,11 @@
#' updateReactable("table", expanded = TRUE)
#' })
#'
#' observeEvent(input$hide_btn, {
#' # Hide columns
#' updateReactable("table", hidden = c('Type', 'Price'))
#' })
#'
#' observeEvent(input$collapse_btn, {
#' # Collapse all rows
#' updateReactable("table", expanded = FALSE)
Expand All @@ -91,7 +99,7 @@
#'
#' @export
updateReactable <- function(outputId, data = NULL, selected = NULL, expanded = NULL,
page = NULL, meta = NULL, session = NULL) {
page = NULL, meta = NULL, hidden=NA, session = NULL) {
if (is.null(session)) {
if (requireNamespace("shiny", quietly = TRUE)) {
session <- shiny::getDefaultReactiveDomain()
Expand Down Expand Up @@ -128,6 +136,12 @@ updateReactable <- function(outputId, data = NULL, selected = NULL, expanded = N
selected <- as.list(as.integer(selected) - 1)
}

if (!all(is.na(hidden))){
if (!(is.character(hidden) || is.null(hidden))) {
stop("`hidden` must be character array or NULL")
}
}

if (!is.null(expanded) && !is.logical(expanded)) {
stop("`expanded` must be TRUE or FALSE")
}
Expand Down Expand Up @@ -157,7 +171,6 @@ updateReactable <- function(outputId, data = NULL, selected = NULL, expanded = N
if (length(jsEvals) == 0) {
jsEvals <- NULL
}

newState <- filterNulls(list(
data = data,
dataKey = dataKey,
Expand All @@ -168,6 +181,13 @@ updateReactable <- function(outputId, data = NULL, selected = NULL, expanded = N
jsEvals = jsEvals
))

if (is.null(hidden)){
newState[['hidden']] <- list(NULL)
}
else if (!all(is.na(hidden))){
newState[['hidden']] <- hidden
}

if (length(newState) > 0) {
session$sendCustomMessage(sprintf("__reactable__%s", outputId), newState)
}
Expand Down
2 changes: 1 addition & 1 deletion inst/htmlwidgets/reactable.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/htmlwidgets/reactable.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/htmlwidgets/reactable.server.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/htmlwidgets/reactable.server.js.map

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions man/updateReactable.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions srcjs/Reactable.js
Original file line number Diff line number Diff line change
Expand Up @@ -1943,6 +1943,7 @@ function Table({
return
}
const setRowsSelected = instance.setRowsSelected
const setHiddenColumns = instance.setHiddenColumns
const gotoPage = instance.gotoPage
const toggleAllRowsExpanded = instance.toggleAllRowsExpanded

Expand All @@ -1960,6 +1961,13 @@ function Table({
const selectedRowIds = newState.selected.map(index => String(index))
setRowsSelected(selectedRowIds)
}
if (newState.hidden != null) {
const hiddenArray = Array.isArray(newState.hidden) ? newState.hidden: [newState.hidden]
setHiddenColumns(hiddenArray)
}
else{
setHiddenColumns([])
}
if (newState.page != null) {
// Get the latest page count in case a data update changes the number of pages
const nearestValidPage = Math.min(
Expand All @@ -1983,6 +1991,7 @@ function Table({
}, [
nested,
instance.setRowsSelected,
instance.setHiddenColumns,
instance.gotoPage,
instance.toggleAllRowsExpanded,
dataColumns,
Expand Down