javascript - 如何在用 rhandsontable 呈现的表格中使用 js 格式化特定行中的数字?

标签 javascript html r shiny rhandsontable

在本文底部的简化代码中,我相信是 js 用于格式化使用 rhandsontable 呈现的表格的输出。我在代码的 js 部分尝试了行/列格式并取得了一些成功。但是,如下图所示,对于添加的所有列,我将如何格式化表格的第 2 行,使其显示为整数(四舍五入为数字 = 0,因此没有小数),并用逗号分隔千位?

我试过常用的 formatC() 等,但没有成功。看起来答案可能就在 js 中。

enter image description here

代码:

library(rhandsontable)
library(shiny)

mydata <- data.frame('Series 1' = c(1,2000.39,3,4),check.names = FALSE)

rownames(mydata) <- c('A','B','C','D') 

ui <- fluidPage(
  rHandsontableOutput("mytable"),
  textInput('NewCol', 'Enter new column name'),
  actionButton("goButton", "Update Table")
)

server <- function(input, output) {
  output$mytable = renderRHandsontable(df())
  
  df <- eventReactive(input$goButton, {
    if(input$NewCol!="" && !is.null(input$NewCol) && input$goButton>0){
      newcol <- data.frame(NROW(mydata))
      newcol[2,] <- c(1234.22)
      names(newcol) <- input$NewCol
      mydata <<- cbind(mydata, newcol)
    }
    rhandsontable(mydata,rowHeaderWidth = 100)%>%
      hot_cols(
        renderer = "function(instance, td, row, col, prop, value, cellProperties) {
        Handsontable.renderers.NumericRenderer.apply(this, arguments);
          // format as integers first 2 rows:
            if(row == 0 || row == 1){td.innerHTML = `${value}`;} 
          // shade 2nd row:
            if(row == 1){td.style.background='#eff0f1'} 
          // format as % the 2nd set of 2 rows:
            if(row == 2 || row == 3){td.innerHTML = `${Number.parseFloat(value*100)}%`} 
        }") %>% 
      hot_row(c(2), readOnly = TRUE) # makes row 2 read-only
    
  }, ignoreNULL = FALSE)
  observe(if (!is.null(input$mytable)) mydata <<- hot_to_r(input$mytable))
}

shinyApp(ui,server)

最佳答案

一种选择是使用 Internationalization API 提供的格式化程序函数使用例如格式化您的数字国际数字格式。要获得逗号作为分组标记,您可以使用例如美国地区:

library(rhandsontable)
library(shiny)

mydata <- data.frame('Series 1' = c(1,2000.39,3,4),check.names = FALSE)

rownames(mydata) <- c('A','B','C','D') 

ui <- fluidPage(
  rHandsontableOutput("mytable"),
  textInput('NewCol', 'Enter new column name'),
  actionButton("goButton", "Update Table")
)

server <- function(input, output) {
  output$mytable = renderRHandsontable(df())
  
  df <- eventReactive(input$goButton, {
    if(input$NewCol!="" && !is.null(input$NewCol) && input$goButton>0){
      newcol <- data.frame(NROW(mydata))
      newcol[2,] <- c(1234.22)
      names(newcol) <- input$NewCol
      mydata <<- cbind(mydata, newcol)
    }
    rhandsontable(mydata,rowHeaderWidth = 100)%>%
      hot_cols(
        renderer = "function(instance, td, row, col, prop, value, cellProperties) {
          Handsontable.renderers.NumericRenderer.apply(this, arguments);
          
          const formatter = new Intl.NumberFormat('en-US', {
            maximumFractionDigits: 0
          })
          
          // format as integers first 2 rows:
            if(row == 0 || row == 1){td.innerHTML = `${value}`;} 
          // shade 2nd row:
            if(row == 1){td.style.background='#eff0f1'} 
          // format as % the 2nd set of 2 rows:
            if(row == 2 || row == 3){td.innerHTML = `${Number.parseFloat(value*100)}%`} 
          
          // format second row as numbers:
            if(row == 1) { td.innerHTML = `${formatter.format(value)}`;} 
        
         }
      ") %>% 
      hot_row(c(2), readOnly = TRUE) # makes row 2 read-only
    
  }, ignoreNULL = FALSE)
  observe(if (!is.null(input$mytable)) mydata <<- hot_to_r(input$mytable))
}

shinyApp(ui,server)

enter image description here

关于javascript - 如何在用 rhandsontable 呈现的表格中使用 js 格式化特定行中的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74471023/

相关文章:

Javascript取消标记没有id的跨度

html - 在开发移动兼容的 HTML 应用程序时,是否有一种方法可以指定特定输入[type=text] 只允许使用数字?

javascript - 粘性页眉和页脚可滚动的主区域

r - 创建名称数量不断增加的随机数据框列表

c++ - 为什么标准 R 中值函数比简单的 C++ 替代函数慢得多?

javascript - 没有事件的 jQuery 实时绑定(bind)

javascript - 命名本地 jQuery 文件的正确约定是什么?

javascript - for 循环用于表单中的所有输入类型 - 已编辑

javascript - Css - 移动位置固定,最终解决方案

r - 添加颜色渐变密度图R