Custom items can be defined in the contextMenu for rhandsontable
in R by using the contextMenu
function.
Here is an example where we define a custom item "Delete Col" in the contextMenu for a rhandsontable
and define its functionality:
library(rhandsontable) # create a sample data frame df <- data.frame(A = c(1, 2, 3), B = c(4, 5, 6), C = c(7, 8, 9)) # create a rhandsontable from the data frame rh <- rhandsontable(df) # define the custom item my_menu_item <- list(delete_col = list(name = "Delete Col", callback = htmlwidgets::JS(" function (key, options) { var colIndex = this.getSelected()[1][1]; this.alter('remove_col', colIndex); }"))) # add the custom item to the contextMenu rh$contextMenu <- my_menu_item # show the table rh
In this example, we create a rhandsontable
based on the sample data frame df
. We then define the custom item delete_col
as a list with two elements: name
(the displayed name of the menu item) and callback
(the JavaScript function that will be called when the menu item is selected). In the callback function, we get the index of the selected column and remove it from the table using the this.alter('remove_col', colIndex)
method. Finally, we add the custom item to the contextMenu
property of the rhandsontable
and show the table.
Note that this is just an example and you can define your own custom items with different functionalities depending on your needs.
Asked: 2021-06-07 11:00:00 +0000
Seen: 8 times
Last updated: Mar 18 '22