【问题标题】:Adding multiple rows in rhandsontable via context menu通过上下文菜单在 rhandsontable 中添加多行
【发布时间】:2020-12-16 12:57:25
【问题描述】:

我正在使用rhandsontablepackage,并想添加一个上下文菜单选项,用于向生成的handsontable 添加多行。我试图将 this 示例改编为我的 R 等效项,但没有成功。我相信我的javascript 是问题所在,但没有发现问题所在的经验。

如何添加自定义上下文菜单项以添加多行?提前致谢。

Reprex

library(rhandsontable)

iris %>%
  head(5) %>%
  rhandsontable() %>%
  hot_context_menu(customOpts =
                     list(
                       name = "Add 5 rows at the top",
                       callback = htmlwidgets::JS(
                         "function (key, options) {
                              this.alter('insert_row', 1, 5);
                              this.render();
                              }"
                       )
                     ))

【问题讨论】:

    标签: javascript r handsontable rhandsontable


    【解决方案1】:

    我遇到了同样的问题,并通过在 list 中添加 insert_row = list 来指定将要自定义的选项来解决它:

    library(rhandsontable)
    
    iris %>%
      head(5) %>%
      rhandsontable() %>%
      
      
      hot_context_menu(customOpts =
                         
                         list(
                           insert_row = list(
                             name = "Add 5 rows at the top",
                             callback = htmlwidgets::JS(
                               "function (key, options) {
                                  this.alter('insert_row',0, 5);
                                  this.render();
                                  }"
                             )
                           )
                         ))
    

    我还将您的示例中的 this.alter('insert_row',1, 5); 更改为 this.alter('insert_row',0, 5);。这是因为如果您在示例中使用它,则新的 5 行将添加到第一行下方,而在我的示例中,新行将在表格顶部创建。要获取下面的行,请使用this.alter('insert_row',[0], 5);

    【讨论】:

    • 这太好了,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多