【问题标题】:Pass javascript function as parameter将javascript函数作为参数传递
【发布时间】:2020-06-26 17:58:59
【问题描述】:

有没有办法使用DT::datatable() 将javascript 函数作为参数传递?我正在尝试使用colvis button's columnText 参数,但datatable() 总是转换字符串中的值。

我的表和这个很相似(我用一个容器作为头部,所以不能使用 colnames 参数)

library(DT)

sketch = htmltools::withTags(table(
  class = 'display',
  thead(
    tr(
      th(rowspan = 2, 'Species'),
      th(colspan = 2, 'Sepal'),
      th(colspan = 2, 'Petal')
    ),
    tr(
      lapply(rep(c('Length', 'Width'), 2), th)
    )
  )
))

# Work
datatable(iris,
          extensions = c('Buttons'),
          options = list(dom = c('Bfrtip'),
                         buttons = list(list(extend = 'colvis'))
          ),
          container = sketch)


# Doesn't work
datatable(iris,
          extensions = c('Buttons'),
          options = list(dom = c('Bfrtip'),
                         buttons = list(list(extend = 'colvis',
                                             columnText = function ( dt, idx, title ) { return (idx+1) }))
          ),
          container = sketch)

【问题讨论】:

    标签: r datatables dt


    【解决方案1】:

    你必须使用JS函数:

     columnText = JS("function ( dt, idx, title ) { return (idx+1) }")
    

    【讨论】:

      猜你喜欢
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      • 2012-09-12
      • 2015-06-09
      • 2018-02-23
      • 2012-05-30
      相关资源
      最近更新 更多