DT Shiny中的简单JavaScript。 DT内嵌的actionButton的打印值

时间:2018-08-28 09:51:56

标签: javascript r shiny dt

我有以下脚本,旨在打印出被单击/选中的按钮或框的行。复选框的小javascript代码(从另一个SO答案启发而来)可以正常工作。但是,由于我是javascript的新手,所以无法找到actionButton的有效类比。

library(DT)
library(glue)

ui <- fluidPage(
uiOutput("modal"),
fluidRow(
  verbatimTextOutput("value1"),
verbatimTextOutput("value2"),
  column(12,
         DT::dataTableOutput('table'),  tags$script(HTML('$(document).on("click", "input", function () {
                   var checkboxes = document.getElementsByName("selected");
                   var checkboxesChecked = [];
                   for (var i=0; i<checkboxes.length; i++) {
                   if (checkboxes[i].checked) {
                   checkboxesChecked.push(checkboxes[i].value);
                  }
                  }
                 Shiny.onInputChange("checked_rows",checkboxesChecked);

                   var buttons = document.getElementsByName("modified");
                   var buttonsPressed = [];
                   for (var i=0; i<buttons.length; i++) {
                   if (buttons[i].click) {
                   buttonsPressed.push(buttons[i].value);
                  }
                  }
                 Shiny.onInputChange("pressed_rows",buttonsPressed);   
        })
    '))
  )))

server <- function(input, output, session) {
df <- reactiveValues( data = data.frame(rownum = 1:5, stringsAsFactors = FALSE))
output$value1 <- renderPrint({ input$checked_rows }) 
output$value2 <- renderPrint({input$pressed_rows})
output$table <- DT::renderDataTable({
  df$data[["Select"]] <- glue::glue('<input type="checkbox" name="selected" value="{1:nrow(df$data)}"><br>')
  df$data[["Modify"]] <- glue::glue('<input type="button" name="modified" value="{1:nrow(df$data)}"><br>')

  DT::datatable(df$data,rownames=F, escape = FALSE, select = "none")

}) 

}

shinyApp(ui, server)

任何帮助都将不胜感激!

1 个答案:

答案 0 :(得分:0)

希望它可以为您提供帮助:

library(shiny)
library(DT)
library(tidyverse)
library(shinyjs)

ui <- fluidPage(useShinyjs(), # for use js
  DT::dataTableOutput("row"),
  tags$script("$(document).on('click', '#row button', function () {
              Shiny.onInputChange('lastClickId',this.id);
              Shiny.onInputChange('lastClick', Math.random())
              });") # use js
)

server <- function(input, output, session) {
  output$row<-renderDataTable({

      DT=mtcars%>%rownames_to_column()
      DT[["Action"]]<-
        paste0('<div class="btn-group" role="group" aria-label="Basic example">
                <button type="button" class="btn"id=action_',DT$rowname,'>Action</button>
              </div>')


    datatable(DT,escape=F,selection="none")})

  observeEvent(input$lastClick,
               {if ( grepl("action_",input$lastClickId)){

                 showModal(modalDialog(easyClose = T,
                                       span("OKAY") ,
                                       footer = tagList(
                                         modalButton("Cancel"),
                                         actionButton("ok_post", "OK",  class="btn btn-success")
                                       )
                 ))
               }
                 print(input$lastClickId)
                 })

}

shinyApp(ui, server)