我目前正在处理一个我创建了一个具有数据表的仪表板的场景。数据表有一个单击按钮,可打开与表中该行相关的模式窗口。模态窗口有一组下拉菜单、文本输入和日期,一旦单击模态窗口中的“保存”按钮,它们将被更新到数据库中(再次仅针对该行)。
是否可以检索先前保存的下拉列表、文本输入和日期的值(如果有)并将它们显示在相同的下拉列表、文本输入和每一行的日期中?
提前感谢您的帮助。
用户界面代码
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 <- 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');"))
})
}