如何将'afterColumnResize'事件与'rhandsontable'一起使用? [英] How to use the 'afterColumnResize' event with 'rhandsontable'?

查看:89
本文介绍了如何将'afterColumnResize'事件与'rhandsontable'一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript库 Handsontable 中有一个事件

The JavaScript library Handsontable has an event afterColumnResize, triggered when a column is manually resized. How to use it with the 'rhandsontable' package in Shiny?

解决方案

Here is how:

library(shiny)
library(rhandsontable)
library(htmlwidgets)

jsCode <- c(
  "function(el, x) {",
  "  Handsontable.hooks.add('afterColumnResize', function(index, size){",
  "    Shiny.setInputValue('newsize', {index: index+1, size: size});",
  "  });",
  "}"
)

ui <- fluidPage(
  rHandsontableOutput("dataTable"),
  br(),
  verbatimTextOutput("sizeinfo")
)

server <- function(input, output, session) {
  df = data.frame(
    company = c('a', 'b', 'c', 'd'),
    bond    = c(0.2, 1, 0.3, 0),
    equity  = c(0.7, 0, 0.5, 1),
    cash    = c(0.1, 0, 0.2, 0),
    stringsAsFactors = FALSE
  )
  output$dataTable <- renderRHandsontable({
    rhandsontable(df, manualColumnResize = TRUE, manualRowResize = TRUE) %>% 
      onRender(jsCode)
  })
  output$sizeinfo <- renderPrint({
    req(input$newsize)
    sprintf(
      "Column %d has new size %dpx.", 
      input$newsize$index, input$newsize$size
    )
  })
}

shinyApp(ui, server)

这篇关于如何将'afterColumnResize'事件与'rhandsontable'一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆