Skip to content
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

Is there a way in which the menu can automatically collapse back once a country is selected? #5

Open
Shelmith-Kariuki opened this issue May 3, 2022 · 2 comments

Comments

@Shelmith-Kariuki
Copy link

Shelmith-Kariuki commented May 3, 2022

Hi @pvictor ,

Thank you so much for this package.

Is there a way in which the menu can automatically collapse back once a country is selected?

`
library(shiny)
library(shinytreeview)

data("cities")

ui <- fluidPage(
tags$h3("treeviewInput cities example"),
treeviewInput(
inputId = "tree",
label = "Choose a city:",
choices = make_tree(
cities, c("continent", "country", "city")
),
multiple = FALSE,
prevent_unselect = TRUE,
width = "100%"
),
verbatimTextOutput(outputId = "result")
)

server <- function(input, output, session) {
output$result <- renderPrint({
input$tree
})
}

if (interactive())
shinyApp(ui, server)
`

@pvictor
Copy link
Member

pvictor commented May 3, 2022

Hi,

You can collapse levels from your server with collapseTreeview().

There's two solutions :

  • Collapse all treeview with:
collapseTreeview("tree") # "tree" is inputId of the widget
  • Collapse parent level, for this you need to retrieve nodes' ids:
nodes <- input$tree_nodes
this_node <- nodes[nodes$text == input$tree, "parentId"]
collapseTreeview("tree", nodeId = this_node)

Here's an example:

library(shiny)
library(shinytreeview)

data("cities")

ui <- fluidPage(
  tags$h3("treeviewInput cities example"),
  treeviewInput(
    inputId = "tree",
    label = "Choose a city:",
    choices = make_tree(
      cities, c("continent", "country", "city")
    ),
    multiple = FALSE,
    prevent_unselect = TRUE,
    width = "100%"
  ),
  verbatimTextOutput(outputId = "result"),
  verbatimTextOutput(outputId = "nodes")
)

server <- function(input, output, session) {
  
  observeEvent(input$tree, {
    if (input$tree %in% cities$city) {
      nodes <- input$tree_nodes
      this_node <- nodes[nodes$text == input$tree, "parentId"]
      collapseTreeview("tree", nodeId = this_node)
      # or collapse all levels with:
      # collapseTreeview("tree")
    }
  })
  
  output$result <- renderPrint({
    input$tree
  })
  
  output$nodes <- renderPrint({
    input$tree_nodes
  })
  
}

if (interactive())
  shinyApp(ui, server)

Victor

@Shelmith-Kariuki
Copy link
Author

Hi @pvictor that worked ... thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants