3

I'm using the rhandsontablepackage and would like to add a context menu option for adding multiple rows to the handsontable produced. I've tried to adapt this example into my R equivalent with no luck. I believe my javascript is the issue but don't have the experience to spot what's wrong.

How can I add a custom context menu item for adding multiple rows? Thanks in advance.

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();
                              }"
                       )
                     ))
Michael Bird
  • 771
  • 8
  • 21

1 Answers1

2

I had the same issue, and solved it by designating the option that will be customized by adding insert_row = list inside 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();
                              }"
                         )
                       )
                     ))

I also changed this.alter('insert_row',1, 5);as in your example to this.alter('insert_row',0, 5);. That is because if you have it as in your example, the new 5 rows are added below the first row, whereas as in my example the new rows will be created at the top of the table. To get the rows below, use this.alter('insert_row',[0], 5);

ViviG
  • 1,613
  • 1
  • 9
  • 23