R ShinyBS弹出窗口 [英] R shinyBS popup window

查看:95
本文介绍了R ShinyBS弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个项目中工作,必须创建闪亮的表单.我目前在用户界面中有一个数据表,该表具有超链接形式的电子邮件.单击超链接后,将打开模式窗口,在该窗口中,我还有另一个UI,其中显示了要填充的各个字段.我这里有一个保存按钮,单击该按钮后应该在后端更新我的数据库.

I working on a project where I have to create a form in shiny. I currently have a datatable in the UI which has email in the form of hyperlink. Once the hyperlink is clicked the modal window opens where I have another UI which shows the various fields to be filled. I have a save button here that should update my DB in the backend once the button is clicked.

我面临的问题是我无法将每个电子邮件都引用到该特定模式窗口,并且我的更新查询更新了数据库中的所有记录.有没有办法将所有已单击的记录详细信息传递到模式窗口?

The problem I am facing is that I am unable to reference each email to that particular modal window and my update query updates all the records in the DB. Is there a way to pass all the record details that has been clicked into the modal window??

我基本上需要知道的是如何更新我单击的记录并为其打开弹出窗口?

What I basically need to know is how to update the record that I have clicked on and for which the pop up window is opened??

我将附加UI.R和server.R以供使用.

I am attaching the UI.R and server.R for use.

enter code here

ui.R

library(shiny)
library(DT)
library(shinyBS)
fluidPage(
         fluidRow(
                  actionButton(inputId = "view",label = "Hi")),
                  #actionButton(inputId =  "savepage1", label = "Save"),
                  DT::dataTableOutput('my_table'),
                  bsModal("FormModal", "My Modal", "",textOutput('mytext'),uiOutput("form1"),
                          actionButton("savepage2","Save"),DT::dataTableOutput("table1"),size = "large")

         )

enter code here

server.R

library(shinyBS)
server <- function(session, input, output){
  
uedata<-c("","Prime","Optimus")  ##add source data here
  
  output$form1<-renderUI({
    tagList(
      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)))
  })

  on_click_js = "Shiny.onInputChange('mydata', '%s');
  $('#FormModal').modal('show')"
  
  convert_to_link = function(x) {
    as.character(tags$a(href = "#", onclick = sprintf(on_click_js,x), x))
  }
  
  observeEvent(input$view,{
    session$sendCustomMessage(type = "unbinding_table_elements", "my_table")
    output$my_table <- DT::renderDataTable({
      a=dbGetQuery(hcltcprod,paste0("select name,mobile,email,assignedto from public.tempnew order by 3;"))
      a <- data.frame(a,row.names = NULL)
      a$email <- sapply(a$email,convert_to_link)
      a1 <- datatable(a,
                     escape = F,
                     options = list(paging = FALSE, ordering = FALSE, searching = FALSE, rownames = FALSE,
                                    preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node());}'),
                                    drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')))
                                    
      a1
    })
  })
  
  observeEvent(input$my_table_cell_clicked, {
    print(Sys.time())
  })

  observe({
    if(input$savepage2==0)
      return()
    isolate({
      for(i in 1:nrow(a))
     dbGetQuery(hcltcprod,paste0("update public.tempnew set s_text='",input$samplevalue,"',s_value='",input$sampletext,"' where mobile in ('",a$email,"');"))
    })
  })
  
}

推荐答案

由于您的示例已连接到数据库,而您没有提供示例数据,因此我将使用mtcars数据集.以链接中的示例为基础,您可以使用以下命令查看所选数据:

As your example is connected to database and you didnt provide sample data I will go with mtcars dataset. Building on the example in the link you can view the selected data using the following:

rm(list = ls())
library(DT)
library(shiny)
library(shinyBS)
library(shinyjs)
library(shinydashboard)

# This function will create the buttons for the datatable, they will be unique
shinyInput <- function(FUN, len, id, ...) {inputs <- character(len)
                                           for (i in seq_len(len)) {
                                             inputs[i] <- as.character(FUN(paste0(id, i), ...))}
                                           inputs
}

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) {
  my_data <- reactive({
    testdata <- mtcars
    as.data.frame(cbind(View = shinyInput(actionButton, nrow(testdata),'button_', label = "View", onclick = 'Shiny.onInputChange(\"select_button\",  this.id)' ),testdata))
  })  
  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,{
    my_data()[SelectedRow(),2:ncol(my_data())]
  })

  output$popup <- renderUI({
    bsModal("modalExample", paste0("Data for Row Number: ",SelectedRow()), "", size = "large",
            column(12,                   
                   DT::renderDataTable(DataRow())

            )
    )
  })

}

shinyApp(ui, server)

这篇关于R ShinyBS弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆