闪亮的javascript列在数据表的多个页面上求和

发布于 2025-01-11 04:43:59 字数 1505 浏览 0 评论 0原文

这是我上一个问题的后续问题,可以找到 这里

在此代码片段中,我使用 Javascript 计算闪亮数据表的列总和,因为我希望它直接显示在数据表下方。

library(shiny)
library(DT)

ui <- shinyUI(fluidPage(
  h1("Testing TableTools"),
  mainPanel(dataTableOutput("display"))
))

Names <- c("", names(mtcars))
FooterNames <- c(rep("", 5), Names[6], rep("", 6))

server <- function(input, output, session) {
  sketch <- htmltools::withTags(table(
    tableHeader(Names), tableFooter(FooterNames)
  ))
  
  opts <- list(
    footerCallback = JS(
    "function(tfoot, data, start, end, display) {
      var api = this.api(), data;
      var sum1 =  api.column(5).data().reduce(function(a, b) {
        return a + b;
      });
      sum1 = Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(sum1)
      $(api.column(5).footer()).html('SubTotal:  ' + sum1)
    }"
    )
  )
  
  output$display <- DT::renderDataTable(container = sketch, extensions = "Buttons", options = opts, {
    mtcars
  })
}

shinyApp(ui = ui, server = server)

原始表格

但是,对表进行排序将更改列总和,因为总和只是显示列的总和。 我想更改它,以便它在表的多个“页面”的情况下显示总和,如果只有一页,它应该像现在一样计算它。

排序表

This is a follow-up question to my previous question which can be found here.

In this code snippet I compute the column sum of a shiny datatable using Javascript as I want it to be shown directly under the datatable.

library(shiny)
library(DT)

ui <- shinyUI(fluidPage(
  h1("Testing TableTools"),
  mainPanel(dataTableOutput("display"))
))

Names <- c("", names(mtcars))
FooterNames <- c(rep("", 5), Names[6], rep("", 6))

server <- function(input, output, session) {
  sketch <- htmltools::withTags(table(
    tableHeader(Names), tableFooter(FooterNames)
  ))
  
  opts <- list(
    footerCallback = JS(
    "function(tfoot, data, start, end, display) {
      var api = this.api(), data;
      var sum1 =  api.column(5).data().reduce(function(a, b) {
        return a + b;
      });
      sum1 = Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(sum1)
      $(api.column(5).footer()).html('SubTotal:  ' + sum1)
    }"
    )
  )
  
  output$display <- DT::renderDataTable(container = sketch, extensions = "Buttons", options = opts, {
    mtcars
  })
}

shinyApp(ui = ui, server = server)

original table

However, sorting the table will change the column sum as the sum is only the sum of the shown column.
I want to change it such that it shows the total sum in case of multiple "pages" of the table and if there is only one page it should compute it as it does now.

sorted table

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷乱花海 2025-01-18 04:43:59

JavaScript 中 renderDT 中的 server = FALSEcolumn(5, {search: 'applied'})

library(shiny)
library(DT)

ui <- shinyUI(fluidPage(
  h1("Testing TableTools"),
  mainPanel(DTOutput("display"))
))

Names <- c("", names(mtcars))
FooterNames <- c(rep("", 5), Names[6], rep("", 6))

server <- function(input, output, session) {
  sketch <- htmltools::withTags(table(
    tableHeader(Names), tableFooter(FooterNames)
  ))
  
  opts <- list(
    footerCallback = JS(
      "function(tfoot, data, start, end, display){
      var api = this.api(), data;
      var sum1 = api.column(5, {search: 'applied'}).data().reduce(function(a, b) {
        return a + b;
      });
      sum1 = Intl.NumberFormat('de-DE', {style: 'currency', currency: 'EUR'}).format(sum1);
      $(api.column(5).footer()).html('SubTotal:  ' + sum1);
    }"
    )
  )
  
  output$display <- renderDT({
    datatable(
      mtcars, 
      container = sketch, 
      extensions = "Buttons", 
      options = opts
    )
  }, server = FALSE)
}

shinyApp(ui = ui, server = server)

server = FALSE in renderDT and column(5, {search: 'applied'}) in the JavaScript:

library(shiny)
library(DT)

ui <- shinyUI(fluidPage(
  h1("Testing TableTools"),
  mainPanel(DTOutput("display"))
))

Names <- c("", names(mtcars))
FooterNames <- c(rep("", 5), Names[6], rep("", 6))

server <- function(input, output, session) {
  sketch <- htmltools::withTags(table(
    tableHeader(Names), tableFooter(FooterNames)
  ))
  
  opts <- list(
    footerCallback = JS(
      "function(tfoot, data, start, end, display){
      var api = this.api(), data;
      var sum1 = api.column(5, {search: 'applied'}).data().reduce(function(a, b) {
        return a + b;
      });
      sum1 = Intl.NumberFormat('de-DE', {style: 'currency', currency: 'EUR'}).format(sum1);
      $(api.column(5).footer()).html('SubTotal:  ' + sum1);
    }"
    )
  )
  
  output$display <- renderDT({
    datatable(
      mtcars, 
      container = sketch, 
      extensions = "Buttons", 
      options = opts
    )
  }, server = FALSE)
}

shinyApp(ui = ui, server = server)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文