java调用shiny,使用Java脚本删除Shiny应用程序中的DataTable行

I'm trying to remove rows from a Datatable in a Shiny app using Javascript. In the table I have a column with a "delete" button for each row. This would be more or less what I'm trying to do, but I can't get it to work.

In my server.R:

initComplete

"function () {",

" var table = this.api();",

" $('#delete_button').on('click', function() {",

" table.row($(this).parents('tr')).remove().draw();",

" });",

"}"

)

shinyInput

inputs

for (i in seq_len(len)) {

inputs[i]

}

inputs

}

output$under_list

list_under

list_under$Delete

style = "color: red;background-color: white",

onclick = paste0("Shiny.setInputValue( \"delete_button\" , this.id, {priority: \"event\"})"))

table %

DT::datatable(filter = "none", rownames = F

,extensions = 'FixedColumns'

,options = list(pageLength = 10,scrollX = TRUE,

fixedColumns = list(leftColumns = 2),

initComplete = initComplete),

escape=F

) %>%

formatCurrency(c(5,8,9,11,15),digits=2,currency='€') %>%

formatPercentage(13:14)

})

解决方案

library(DT)

library(shiny)

rowNames = FALSE # whether to show row names

colIndex

dat = iris[1:5,]

dat[["Action"]]

as.character(tags$button("delete", id = paste0("delete-",i), `data-index` = i))

}, FUN.VALUE = character(1L))

dat[["rowId"]]

callback

'$("button[id^=delete]").on("click", function() {',

' var index = $(this).data("index");',

' var rowId = "#row-" + index;',

' table.row(rowId).remove().draw();',

'});'

)

datatable(

dat,

rownames = rowNames,

escape = -ncol(dat)+1L,

callback = callback,

options = list(

rowId = JS(sprintf("function(data){return data[%d];}",

ncol(dat)-1L+colIndex)),

columnDefs = list(

list(visible = FALSE, targets = ncol(dat)-1L+colIndex),

list(className = "dt-center", targets = "_all")

)

)

)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值