css - 在 DT 中添加页脚

标签 css r dt

我正在尝试在数据表的页脚中添加一个 colsum

我用 https://github.com/rstudio/DT/issues/39 做了 1 列

sketch <- htmltools::withTags(table(
  tableHeader(cars),
  tableFooter(cars)
))


datatable(cars,container = sketch, rownames = F, 
      options = list(
        footerCallback = JS(
          "function( tfoot, data, start, end, display ) {",
          "var api = this.api();",
          "$( api.column(1).footer() ).html(",
          "api.column(1 ).data().reduce( function ( a, b ) {",
          "return a + b;",
          "} )",  # remove ; here
          ");",
          "}")
      )
)

我正在努力为
- 所有专栏
- 所有 num 列(或除 char id 之外的所有列更容易)

编辑:解决方案:)

dt_test <- structure(list(`pathologie principale` = c("Effet toxique des métaux", 
                                                  "Autres résultats anormaux des examens chimiques du sang", "Néphrite tubulo-interstitielle chronique", 
                                                  "Atteintes tubulo-interstitielles et tubulaires dues à des médicaments et des métaux lourds", 
                                                  "Autres maladies     pulmonaires obstructives chroniques", "Autres résultats anormaux de l'examen des urines"
),     Fort = c(12L, 4L, 3L, 2L, 2L, 2L), Moyen = c(2L, 0L, 0L, 0L, 1L, 1L), Faible = c(4L, 0L, 0L, 0L, 4L, 0L)),   
.Names = c("pathologie principale",                  "Fort", "Moyen", "Faible"), class = c("data.table", "data.frame"
), row.names = c(NA, -6L))


sketch <- htmltools::withTags(table(
  tableHeader(dt_test),
  tableFooter(sapply(dt_test, function(x) ifelse( (is.numeric(x)) ,sum(x)     ,"total" ))
)))


datatable(dt_test,
      container = sketch, 
      rownames = F
)

最佳答案

不需要footerCallback:

sketch <- htmltools::withTags(table(
    tableHeader(dt_test),
    tableFooter(sapply(dt_test, function(x) if(is.numeric(x)) sum(x)))
))


datatable(dt_test,
          container = sketch, 
          rownames = F
)

enter image description here

关于css - 在 DT 中添加页脚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42651632/

相关文章:

html - 用于访问特定 selectInput 并使初始元素变为粗体的 Css 属性

r - 在 R 中编写自定义分类器和预测函数

r - 在具有多个条目的 R Shiny 数据表中搜索列

r - Shiny - 在数据表中选择记录时如何突出显示传单 map 上的对象?

html - 对齐元素 HTML/CSS float 或显示 : inline-block

html - 将 <pre> 包裹在表格单元格内

javascript - 事件类不显示

r - R中的线性插值(lm),奇怪的行为

r - 如何用R计算分类的精度、召回率和准确率?

jquery - 使用 DT::datatable 为用户调整列大小