Last active
August 29, 2015 14:06
-
-
Save saurfang/cdb93616e955c17e6599 to your computer and use it in GitHub Desktop.
Shiny Sortable CheckboxGroupInput with Update on Change
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sortableCheckboxGroupInput <- function(inputId, ...) { | |
# build jquery-ui dependency for sortable | |
jqueryUIDep <- htmlDependency("jqueryui", "1.10.4", c(href="shared/jqueryui/1.10.4"), | |
script = "jquery-ui.min.js", | |
stylesheet = "jquery-ui.min.css") | |
# sortable enabler | |
sortableScript <- " | |
function makeSortable(el) { | |
el.sortable({ | |
//clone a helper which will append to the end so that it would visually | |
//preserve the checked status while dragging | |
helper: 'clone', | |
placeholder: { | |
element: function(item) { | |
//clone the item and show it so the placeholder replicate the item | |
//and will be reflected in our values | |
var clone = item.clone(); | |
clone.show(); | |
//uncheck the box so the original box won't mess with our values | |
var checkbox = item.children(':checkbox'); | |
checkbox.prop('was-checked', checkbox.prop('checked')); | |
checkbox.prop('checked', false); | |
return clone; | |
}, | |
update: function() { | |
return; | |
} | |
}, | |
change: function(event, ui) { | |
//propogate changes in position directly to the element being moved | |
$(event.target).trigger('change'); | |
}, | |
stop: function(event, ui) { | |
//now we want to restore the checked status if it was checked | |
var checkbox = ui.item.children(':checkbox'); | |
checkbox.prop('checked', checkbox.prop('was-checked')); | |
} | |
}); | |
}" | |
# return label and select tag | |
attachDependencies( | |
tagList( | |
singleton(tags$head(tags$script(sortableScript))), | |
checkboxGroupInput(inputId, ...), | |
tags$script(paste0("makeSortable($('#", inputId, "'));"))), | |
jqueryUIDep) | |
} | |
shinyApp( | |
ui = fluidPage( | |
sortableCheckboxGroupInput("variables", "", 1:10, 1:10), | |
textOutput("variables") | |
), | |
server = function(input, output) { | |
output$variables <- renderText(unique(input$variables)) | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment