rhandsontable
rhandsontable copied to clipboard
columnSorting prevents table re-render
The option columnSorting
, when enabled, does not allow a table to be re-rendered reactively, when the input data changes.
In the example below, the table should contain 3 or 5 columns depending on the user's choice. However, when I set columnSorting = TRUE
the table does not get changed, and after few clicks it does not allow cells editing.
The example below demonstrates the issue with and without columnSorting
effect.
library(shiny)
library(rhandsontable)
ui <- shiny::fluidPage(
h2("Option 'columnSorting' affects reactive properties of the table"),
p("Depending on the selected option the table should display 3 (narrow) or 5 (wide) columns"),
p("When SORTING is TRUE, the table does not get re-rendered from narrow to wide"),
checkboxInput("sorting", "SORTING option", value = FALSE, width = NULL),
radioButtons("type", "Table type:",
c("Narrow" = "narrow",
"Wide" = "wide")),
rHandsontableOutput("table")
)
server <- function(input, output) {
output$table <- renderRHandsontable({
if (input$type == "narrow") {
rhandsontable(data = head(mtcars[,1:3]),
columnSorting = input$sorting)
} else {
rhandsontable(data = head(mtcars[,1:5]),
columnSorting = input$sorting) }
})
}
shinyApp(ui, server)
rhandsontable version: 0.3.7 shiny version: 1.4.0
Being inspired by the beautiful solution at https://github.com/jrowen/rhandsontable/issues/336, I have found a workaround for the issue. The idea is to send JS request to 'handsontable' in order to disable 'ColumnSorting' Plugin before the table gets rendered:
shinyjs::runjs("HTMLWidgets.find('#table').hot.getPlugin('ColumnSorting').disablePlugin()")
See https://gist.github.com/DzimitryM/62bd55f289bf4e8c74abb27f92c39557 for an example
Do you know if theres a way to only enable column sorting on certain columns?
@lmeninato, I don't know a straight way to do it, however, there is a workaround. It implies disabling sorting for certain columns, e.g. for "cyl" in this case:
rhandsontable(data = head(mtcars),
columnSorting = T) %>%
hot_col(col = "cyl",
columnSorting = list(compareFunctionFactory = 'function(sortOrder, columnMeta) {
return function(value, nextValue) {
return 0;
}
}' )
)