% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/server-treeview.R
\name{tree-server}
\alias{tree-server}
\alias{searchTreeview}
\alias{clearSearchTreeview}
\alias{expandTreeview}
\alias{collapseTreeview}
\alias{updateTreeview}
\title{Manipulate a tree server-side}
\usage{
searchTreeview(
  inputId,
  pattern,
  ignore_case = TRUE,
  exact_match = FALSE,
  reveal_results = TRUE,
  collapse_before = TRUE,
  session = shiny::getDefaultReactiveDomain()
)

clearSearchTreeview(inputId, session = shiny::getDefaultReactiveDomain())

expandTreeview(
  inputId,
  nodeId = NULL,
  levels = 1,
  session = shiny::getDefaultReactiveDomain()
)

collapseTreeview(
  inputId,
  nodeId = NULL,
  session = shiny::getDefaultReactiveDomain()
)

updateTreeview(
  inputId,
  label = NULL,
  selected = NULL,
  session = shiny::getDefaultReactiveDomain()
)
}
\arguments{
\item{inputId}{The id of the input object.}

\item{pattern}{Pattern to search for.}

\item{ignore_case}{Case insensitive.}

\item{exact_match}{Like or equals.}

\item{reveal_results}{Reveal matching nodes.}

\item{collapse_before}{Collapse all nodes before revealing results.}

\item{session}{The session object passed to function given to shinyServer.}

\item{nodeId}{Id of the node to expand or collapse,
use \verb{input$<inputId>_nodes} to see the Ids.
If \code{NULL} expand the all tree.}

\item{levels}{Levels to expand.}

\item{label}{Display label for the control, or \code{NULL} for no label.}

\item{selected}{Default selected value, must correspond to the Id of the node.}
}
\value{
No value, side-effects only.
}
\description{
Interact with a tree contructed with \code{\link[=treeviewInput]{treeviewInput()}} or \code{\link[=treecheckInput]{treecheckInput()}} from the server.
}
\examples{
# searchTreeview ----------------------------------------------------------


library(shiny)
library(shinytreeview)

data("countries")

ui <- fluidPage(
  tags$h3("treeviewInput search 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,
      textInput("search", "Search a country"),
      tags$b("Selected country:"),
      verbatimTextOutput(outputId = "result")
    )
  )
)

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

  observeEvent(input$search, {
    if (isTruthy(input$search)) {
      searchTreeview(
        inputId = "country",
        pattern = input$search,
        reveal_results = TRUE
      )
    } else {
      clearSearchTreeview("country")
      collapseTreeview("country")
    }
  })
}

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



# expand/collapse ---------------------------------------------------------


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")
        ),
        nodes_input = TRUE,
        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)
  })

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

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



# updateTreeview ----------------------------------------------------------


library(shiny)
library(shinytreeview)

data("cities")

ui <- fluidPage(
  tags$h3("Update label & selected value"),
  treeviewInput(
    inputId = "tree",
    label = "Choose a city:",
    choices = make_tree(cities, c("continent", "country", "city")),
    multiple = FALSE,
    prevent_unselect = TRUE
  ),
  verbatimTextOutput(outputId = "result"),
  textInput("label", "New label:", "Choose a city:"),
  radioButtons(
    "selected", "Selected:",
    choices = unique(c(cities$continent, cities$country, cities$city)),
    inline = TRUE
  )
)

server <- function(input, output, session) {
  output$result <- renderPrint({
    input$tree
  })
  observe(updateTreeview(inputId = "tree", label = input$label))
  observe(updateTreeview(inputId = "tree", selected = input$selected))
}

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