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.
I am attaching my ui and server code.
ui.R
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.R
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','s...@gmail.com','Bat',getdate(),'Prime','Number');")) }) }