rstudio / sortable

R htmlwidget for Sortable.js
https://rstudio.github.io/sortable/
Other
127 stars 30 forks source link

onSort not capturing changes in order when new elements are inserted with insertUI #83

Closed gacolitti closed 2 years ago

gacolitti commented 2 years ago

When I click an action button to insert an element into a div, I want the order of the elements to be updated using sortable_options(onSort = sortable_js_capture_input(input_id = "selected"), but this is not the case now. Please see the following reproducible example. You'll notice that the order is only available inside input$selected after moving an element, not inserting with insertUI.

library(shiny)
library(sortable)

ui <- fluidPage(
  actionButton("add", "add"),
  tags$ul(id = "lst"),
  verbatimTextOutput("text"),
  sortable_js(css_id = "lst",  options = sortable_options(
    onSort = sortable_js_capture_input(input_id = "selected"),
    onLoad = sortable_js_capture_input(input_id = "selected")
  ))
)

server <- function(input, output, session) {

  observeEvent(input$add, {
    insertUI(
      selector = "#lst",
      where = "beforeEnd",
      ui = tags$li(paste0("test", input$add))
    )
  })

  output$text <- renderText({
    req(input$selected)
    input$selected
  })

}

shinyApp(ui, server)
andrie commented 2 years ago

The onLoad and onSort JavaScript methods only fire when you drag the elements in the UI. This works as expected in your example.