[英]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
"}")
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 被命名为row
和data
,而不是nRow
和nData
。 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.