通过上下文菜单在 rhandsontable 中添加多行

Adding multiple rows in rhandsontable via context menu

我正在使用 rhandsontable 包,并想添加一个上下文菜单选项,用于向生成的 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();
                              }"
                       )
                     ))

我遇到了同样的问题,通过在 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 行将添加到第一行下方,而在我的示例中,新行将创建在 table 的顶部。要获取下面的行,请使用 this.alter('insert_row',[0], 5);