将模式窗口中的SelectInput,TextInput和Date更新为先前为每行设置的值(如果有)

时间:2016-10-20 10:05:59

标签: shiny shinydashboard shinybs

我目前正在开发一个我创建了一个具有数据表的仪表板的场景。数据表有一个单击按钮,打开一个与表中该行有关的模态窗口。模态窗口有一组下拉列表,文本输入和日期,一旦单击模态窗口中的“保存”按钮,它们将更新到DB(仅针对该行)。

是否可以检索以前保存的下拉列表,textinputs和日期值(如果有),并在每行的相同下拉列表,文本输入和日期中显示它们?

提前感谢您的帮助。

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 <- 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');"))
 })
}

0 个答案:

没有答案
相关问题