rstudio / sortable

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

sortable_js_capture_input attribute and documentation #44

Closed JohnCoene closed 4 years ago

JohnCoene commented 4 years ago

The documentation of the sortable_js_capture_input function states:

This captures the state of a ‘sortable’ list. It will look for an ‘id’ attribute of the first child for each element...

However it appears that the function get_child_id_or_text_js_fn looks for the data-rank-id attribute.

get_child_id_or_text_js_fn <- function() {
  paste0(collapse = "\n",
    "function(child) {",
    "  return ",
    #    use child element attribute 'data-rank-id'
    "    $(child).attr('data-rank-id') || ",
    #    otherwise return the inner text of the element
    #    use inner text vs `.text()` to avoid extra white space
    "    $.trim(child.innerText);",
    "}"
  )
}

I'm unsure whether the function or the documentation should be different. Example below.

library(shiny)
library(sortable)

ui <- fluidPage(
  div(
    id = "sortable",
    div(id = 1, `data-rank-id` = "HELLO", class = "well", "Hello"),
    div(id = 2, `data-rank-id` = "WORLD", class = "well", "world")
  ),
  verbatimTextOutput("chosen"),
  sortable::sortable_js(
    "sortable", 
    options = sortable::sortable_options(
      onSort = sortable::sortable_js_capture_input("selected")
    )
  )
)

server <- function(input, output){
  output$chosen <- renderPrint(input$selected)
}

shinyApp(ui, server)
schloerke commented 4 years ago

You are correct. The docs should be updated to point to data-rank-id.

andrie commented 4 years ago

Thanks for your report as well as the example. I have used your example in the package to improve the docs for this function.