Skip to content

Commit

Permalink
expand & collapse methods + list of nodesId
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed May 6, 2020
1 parent 1693e57 commit 1b89668
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Generated by roxygen2: do not edit by hand

export(collapseTreeview)
export(expandTreeview)
export(make_tree)
export(searchTreeview)
export(treeviewInput)
Expand Down
22 changes: 22 additions & 0 deletions R/onLoad.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,26 @@
return(data)
}
}, force = TRUE)
shiny::registerInputHandler("treeview.nodes", function(data, ...) {
if (is.null(data)) {
NULL
} else {
tryCatch({
do.call("rbind", lapply(data, function(x) {
x <- as.data.frame(x, stringsAsFactors = FALSE)
if (is.null(x$parentId))
x$parentId <- NA_character_
x
}))
}, error = function(e) {
warning("shinytreeview error: ", e$message)
data.frame(
text = character(0),
nodeId = character(0),
parentId = character(0),
stringsAsFactors = FALSE
)
})
}
}, force = TRUE)
}
47 changes: 47 additions & 0 deletions R/server-treeview.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,50 @@ searchTreeview <- function(inputId,
session$sendInputMessage(inputId, message)
}



#' Expand or collapse a \code{treeviewInput}
#'
#' @param inputId The id of the input object.
#' @param nodeId Id of the node to expand or collapse,
#' use \code{input$<inputId>_nodes} to see the Ids.
#' If \code{NULL} expand the all tree.
#' @param levels Levels to expand.
#' @param session The session object passed to function given to shinyServer.
#'
#' @return None.
#' @export
#'
#' @name expand-collapse
#'
#' @example examples/collapse-expand.R
expandTreeview <- function(inputId,
nodeId = NULL,
levels = 1,
session = shiny::getDefaultReactiveDomain()) {
message <- list(expand = dropNulls(list(
nodeId = nodeId,
options = list(
levels = levels
)
)))
session$sendInputMessage(inputId, message)
}

#' @export
#' @rdname expand-collapse
collapseTreeview <- function(inputId,
nodeId = NULL,
session = shiny::getDefaultReactiveDomain()) {
message <- list(collapse = dropNulls(list(
nodeId = nodeId
)))
session$sendInputMessage(inputId, message)
}







56 changes: 56 additions & 0 deletions examples/collapse-expand.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

library(shiny)
library(shinytreeview)

data("countries")

ui <- fluidPage(
tags$h3("treeviewInput expand/collapse example"),
fluidRow(
column(
width = 4,
treeviewInput(
inputId = "country",
label = "Choose a country:",
choices = make_tree(
countries, c("continent", "subregion", "name")
),
width = "100%"
)
),
column(
width = 8,
actionButton("expandAll", "Expand all"),
actionButton("expandWAfrica", "Expand Western Africa"),
actionButton("collapseAll", "Collapse all"),
tags$br(),
tags$b("Selected country:"),
verbatimTextOutput(outputId = "result")
)
)
)

server <- function(input, output, session) {

output$result <- renderPrint({
input$country
})

observeEvent(input$expandAll, {
expandTreeview("country", levels = 3)
})

observeEvent(input$expandWAfrica, {
nodes <- input$country_nodes
w_africa <- nodes[nodes$text == "Western Africa", "nodeId"]
print(w_africa)
expandTreeview("country", nodeId = w_africa, levels = 3)
})

observeEvent(input$collapseAll, {
collapseTreeview("country")
})
}

if (interactive())
shinyApp(ui, server)
20 changes: 20 additions & 0 deletions inst/assets/bootstrap-treeview/treeview-bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ $.extend(treeviewInputBinding, {
}
tree.search(data.search.pattern, data.search.options);
}
if (data.hasOwnProperty("expand")) {
if (data.expand.hasOwnProperty("nodeId")) {
var expandedNode = tree.findNodes("^" + data.expand.nodeId + "$", "nodeId");
tree.expandNode(expandedNode, data.expand.options);
} else {
tree.expandAll(data.expand.options);
}
}
if (data.hasOwnProperty("collapse")) {
if (data.collapse.hasOwnProperty("nodeId")) {
var collapsedNode = tree.findNodes("^" + data.collapse.nodeId + "$", "nodeId");
tree.collapseNode(collapsedNode);
} else {
tree.collapseAll();
}
}
},
getState: function(el) {},
initialize: function(el) {
Expand All @@ -71,6 +87,10 @@ $.extend(treeviewInputBinding, {
revealResults: false
});
}
var nodes = tree.getNodes().map(function(o) {
return {text: o.text, nodeId: o.nodeId, parentId: o.parentId};
});
Shiny.onInputChange(el.id + "_nodes:treeview.nodes", nodes);
});
}
});
Expand Down
96 changes: 96 additions & 0 deletions man/expand-collapse.Rd

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

0 comments on commit 1b89668

Please sign in to comment.