简体   繁体   English

在 shiny 应用程序中将 formatStyle() 与 JS 代码组合时,未加载 DT::datatable

[英]DT::datatable is not loaded when combining formatStyle() with JS code in a shiny app

I have a shiny app in which I want the cursor to change when the user hovers over Species column and also I want to format this column with:我有一个 shiny 应用程序,当用户将鼠标悬停在Species列上时,我希望 cursor 在其中更改,并且我想使用以下格式设置此列:

%>%
        formatStyle('View',  color = 'red', backgroundColor = 'orange', fontWeight = 'bold')

But when I add this certain code line my table is not and loaded and is stuck in 'Processing' mode.但是当我添加这个特定的代码行时,我的表没有加载并且卡在“处理”模式下。 This does not happen when I delete the JS part for mouse hovering ability.当我删除鼠标悬停功能的JS部分时,不会发生这种情况。 How can I combine them?我怎样才能将它们结合起来?

rowCallback = JS(
                              "function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {",
                              "var full_text = aData[1] + ','+ aData[2] + ',' + aData[3] + ','+ aData[4];",
                              "$('td:eq(5)', nRow).attr('title', full_text);", # Tool Tip
                              "$('td:eq(5)', nRow).css('cursor', 'pointer');", # Cursor icon changes to hand (pointer) on Hover
                              "}")

app应用程序

library(shiny)
library(DT)

shinyApp(
    ui = fluidPage(
        DT::dataTableOutput("irisTable")
    ),
    server = function(input, output) {

        output$irisTable <- DT::renderDataTable({
            DT::datatable(datasets::iris, 
                          options = list(rowCallback = JS(
                              "function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {",
                              "var full_text = aData[1] + ','+ aData[2] + ',' + aData[3] + ','+ aData[4];",
                              "$('td:eq(5)', nRow).attr('title', full_text);", # Tool Tip
                              "$('td:eq(5)', nRow).css('cursor', 'pointer');", # Cursor icon changes to hand (pointer) on Hover
                              "}"),pageLength = 5,columnDefs = list(list(className = 'dt-left', targets = "_all"))),rownames= T,
                          selection = list(mode = 'single', target = 'cell')

            )%>%
                formatStyle('Species',  color = 'red', backgroundColor = 'orange', fontWeight = 'bold')

        })
    }
)

That's because formatStyle also uses the rowCallback option but the arguments are named row and data , not nRow and nData .这是因为formatStyle也使用rowCallback选项,但 arguments 被命名为rowdata ,而不是nRownData You have to use these names and this works:您必须使用这些名称,这有效:

library(shiny)
library(DT)

shinyApp(
  ui = fluidPage(
    DTOutput("irisTable")
  ),
  
  server = function(input, output) {
    
    output$irisTable <- renderDT({
      datatable(
        datasets::iris, 
        options = list(
          rowCallback = JS(
            "function(row, data) {",
            "var full_text = data[1] + ',' + data[2] + ',' + data[3] + ',' + data[4];",
            "$('td:eq(5)', row).attr('title', full_text);", # Tool Tip
            "$('td:eq(5)', row).css('cursor', 'pointer');", # Cursor icon changes to hand (pointer) on Hover
            "}"),
          pageLength = 5,
          columnDefs = list(
            list(className = 'dt-left', targets = "_all")
          )
        ),
        rownames= TRUE,
        selection = list(mode = 'single', target = 'cell')
      )%>%
        formatStyle('Species',  color = 'red', backgroundColor = 'orange', fontWeight = 'bold')
      
    })
  }
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DT 表闪亮 R 格式样式 - DT table Shiny R formatStyle 在闪亮的应用程序中使用自定义回调时,DT数据表行未突出显示 - DT datatable row is not highlighted when using custom callback in a shiny app 确定是否在闪亮的应用程序中单击了DT数据表 - Determine if DT datatable is clicked in shiny app 为什么图标不显示在 Shiny 应用程序的 DT::datatable 中? - Why are the icons not displaying in a DT::datatable in Shiny app? 对于闪亮的DT,如何同时使用FormatStyle和Extension? - For DT in shiny, how to use FormatStyle and Extension at the same time ? 是否可以在R Shiny中使用多个DT formatStyle函数? - Is it Possible to Use Multiple DT formatStyle functions in R Shiny? 在 shiny 应用程序中取消单击 DT::datatable 行时隐藏 textOutput() - Hide textOutput() when un-click row of a DT::datatable in shiny app 在 shiny 应用程序中使用 renderDT() 时,删除 DT::datatable 的 header 会导致“找不到匹配的记录” - Deleting DT::datatable's header results in 'no matching records found' when using renderDT() in a shiny app 使用 JS/html 控制/延迟 Shiny DT 数据表的搜索速度 - Control/delay the search speed of Shiny DT datatable using JS/html 使用自定义容器在闪亮应用程序的DT数据表中添加垂直线 - Add vertical line in a DT datatable in a shiny app with custom container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM