0

I am currently working on a scenario where I have created a dashboard which has a datatable. The datatable has a click button which opens up a modal window pertaining to that row in the table. The modal window has a set of dropdowns, textinputs and dates that will be updated into the DB(again for that row alone) once the Save button in the modal window is clicked.

Is it possible to retrieve the previously saved values for the dropdowns,textinputs and dates if any and display them in the same dropdowns,text inputs and dates for each row?

Thanks in advance for helping out.

ui code

ui <- dashboardPage(
  dashboardHeader(title = "Simple App"),
  dashboardSidebar(
    sidebarMenu(id = "tabs",
                menuItem("Menu Item 1", tabName = "one", icon = icon("dashboard"))
    )
  ),
  dashboardBody(
    tabItems(
      tabItem(tabName = "one",h2("Datatable Modal Popup"),
              DT::dataTableOutput('my_table'),uiOutput("popup")
      )
    )
  )
)

server code

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



  samplevaluedata<-c("","Prime","Optimus")  ##add source data here

  shinyInput <- function(FUN, len, id, ...) {inputs <- character(len)
  for (i in seq_len(len)) {
    inputs[i] <- as.character(FUN(paste0(id, i), ...))}
  inputs
  }

  my_data <- reactive({
    a = dbGetQuery(hcltcprod,paste0("select name,mobile,email,assignedto,id from public.tempnew order by 3;"))
    as.data.frame(cbind(View = shinyInput(actionButton, nrow(a),'button_', label = "View", onclick = 'Shiny.onInputChange(\"select_button\",  this.id)' ),a))
  })  
  output$my_table <- DT::renderDataTable(my_data(),selection = 'single',options = list(searching = FALSE,pageLength = 10),server = FALSE, escape = FALSE,rownames= FALSE)

  # Here I created a reactive to save which row was clicked which can be stored for further analysis
  SelectedRow <- eventReactive(input$select_button,{
    as.numeric(strsplit(input$select_button, "_")[[1]][2])
  })

  # This is needed so that the button is clicked once for modal to show, a bug reported here
  # https://github.com/ebailey78/shinyBS/issues/57
  observeEvent(input$select_button, {
    toggleModal(session, "modalExample", "open")
  })

  DataRow <- eventReactive(input$select_button,{
    as.data.frame(my_data()[SelectedRow(),6])
  })

  output$popup <- renderUI({
    print(DataRow())
    bsModal("modalExample", paste0("Data for Row Number: ",SelectedRow()), "", size = "large",
            #column(width=12,DT::renderDataTable(DataRow())),
            column(width=6,selectInput("samplevalue","Select Custom Source*",choices=c("Please select",samplevaluedata))),
            column(width=6,textInput("sampletext",label = "Enter Text",value = NULL,placeholder = NULL)),
            actionButton("openpage","Open"),
            actionButton("savepage","Save"),
            DT::dataTableOutput("t1"))
  })

 tttt <- reactive({
   if(input$openpage==0)
     return()
   isolate({
     a = dbGetQuery(hcltcprod,paste0("select * from public.tempnew where id in (",DataRow(),");"))
     a
   })
 })

 output$t1 <- DT::renderDataTable(tttt())

 observeEvent(input$savepage,{
     a = dbGetQuery(hcltcprod,paste0("update public.tempnew set s_text='",input$sampletext,"',s_value='",input$samplevalue,"' where id in (",DataRow(),");"))
     #a = dbGetQuery(hcltcprod,paste0("insert into public.tempnew values ('Sandeep','1234567891','sss@gmail.com','Bat',getdate(),'Prime','Number');"))
 })
}
Xiongbing Jin
  • 11,779
  • 3
  • 47
  • 41
  • Yes it possible can you your code here? – Dipak Thoke Oct 20 '16 at 10:15
  • I have shared it. Thanks for helping out. – Sanjay Jayaraman Oct 20 '16 at 10:33
  • What do you mean by previously saved values? Do you mean that when you click one row, select/enter some data, and then click another row, the select drop down/text input will have the same data as previous row? – Xiongbing Jin Oct 22 '16 at 01:30
  • I enter the values for the drop down/text input and hit the save button for a particular row. The value gets saved in my db. When I come back to the same row later, I need to see the values that I have selected previously for that row. Hope that sounds clear.... Thanks in advance for helping out... – Sanjay Jayaraman Oct 22 '16 at 10:07

0 Answers0