Question
How to conditionally deactivate or remove selection items in the rhandsontable context menu?
In running the below simplified code, the user can add or remove table rows by right-clicking on a row which, through the rhandsontable
package context menu, generates a pop-up of action choices. In the code you can see how I used the onRender(...)
function and JavaScript to deactivate row deletion when there is only 1 row in the table. This works.
However, I would also like to either deactivate, or remove (whichever is simpler), the "Insert row above" selection when the user accesses the context menu from the first row of the table. When the user accesses the context menu from any table row other than the first then "Insert row above" should be functional. Basically, I never want a top row that is blank. Any ideas for how to do this?
I went through the handsontable
listing of hooks and could not find any equivalent of "beforeAddRow" or the like.
library(shiny)
library(rhandsontable)
library(htmlwidgets)
ui <- fluidPage(
br(),
rHandsontableOutput("simple_table")
)
server <- function(input, output) {
output$simple_table <- renderRHandsontable({
rhandsontable(data.frame(Value = numeric(1)), contextMenu = TRUE, rowHeaders = TRUE) %>%
hot_cols(colWidths = 100, type = "numeric") %>%
# Below prevents row deletion when there is only 1 row in table
onRender(
c(
"function(el, x) {",
" var hot = this.hot;",
" Handsontable.hooks.add('beforeRemoveRow', function(index, amount){",
" var nrows = hot.countRows();",
" if(nrows === 1) {",
" return false;",
" }",
" }, hot);",
"}"
)
)
})
}
shinyApp(ui = ui, server = server)