Shiny DTedit, show or hide insert/new button based on rows_selected status in second DTedit table

The reactive for selected row is input$Drinkdt_rows_selected in your case, based on the source code. If you use that, your code works fine. Try this

server <- function(input, output) {
  
  ##  could not install DTedit. So, made a copy of the function
  source("C:\\RStuff\\GWS\\dtedit.R", local=TRUE)

  Drink_Results <- dtedit(
    input, output,
    name = 'Drink',
    thedata = data.frame(
      ID = c(1:3),
      drink = c('Tea', 'Coffea', 'Water'),
      stringsAsFactors = FALSE
    )
  )
  name <- "Drink"

  # create proxy to clear row selection (found Drinkdt by looking in the source)
  Drink_proxy <- DT::dataTableProxy('Drinkdt')

  Container_Results <- dtedit(
    input, output,
    name = 'Container',
    thedata = data.frame(
      ID = c(1:3),
      Container = c('Cup', 'Glass', 'Pint'),
      stringsAsFactors = FALSE
    )
  )

  # create proxy to clear row selection
  Container_proxy <- DT::dataTableProxy('Container')

  # clear Drink row selection
  observeEvent(input$clearrows, {
    Drink_proxy %>% selectRows(NULL)
    shinyjs::hide('Container_add')
  })
  
  sel <- reactive({!is.null(input[[paste0(name, 'dt_rows_selected')]])}) 
  observe({
    print(sel())
    print(input$Drinkdt_rows_selected)
  })

  # when no drink is selected, hide the New button for containers
  observe({
  #observeEvent(input[[paste0(name, 'dt_rows_selected')]], {
    if ( length(input[[paste0(name, 'dt_rows_selected')]])>0 ) {
      shinyjs::show('Container_add')
    }else {
      shinyjs::hide('Container_add')
    }
  })
  
  observeEvent(Drink_Results$thedata, {
    message(Drink_Results$thedata)
  })

  observeEvent(input[[paste0(name, 'dt_rows_selected')]], ignoreNULL = FALSE, {
    # 'no' (NULL) row will be 'selected' after each edit of the data
    message(paste("Selected row:", input[[paste0(name, 'dt_rows_selected')]]))
  })


  # attempt to react on clearing the row-selection
  choice <- reactive({
    if (is.null(input[[paste0(name, 'dt_rows_selected')]])) {
      paste0("Drink not selected")
    }else {
      paste0(input[[paste0(name, 'dt_rows_selected')]], " - ", input$Containerdt_rows_selected)
    }
    
  })

  observeEvent(input$showhide, {
    toggle('Container_add')
  })
  
  # output current combination
  output$choice <- renderText({ choice() })
}

ui <- fluidPage(
  shinyFeedback::useShinyFeedback(),
  useShinyjs(),
  h3('What will you drink?'),
  uiOutput('Drink'),

  # manually clear row selections
  actionButton(inputId="clearrows", label="clear selected drink", icon=icon('trash')),

  hr(),

  h3("What container do you prefer?"),
  uiOutput('Container'),

  hr(),

  # manually hide the New button
  actionButton(inputId="showhide", label="toggle New buttons", icon=icon('refresh')),

  hr(),

  # show current user choices
  textOutput('choice'),

)

shinyApp(ui = ui, server = server)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top