Skip to content

Commit

Permalink
Improve update_cbs()
Browse files Browse the repository at this point in the history
  • Loading branch information
jthompson-arcus committed Dec 6, 2024
1 parent 39238a3 commit 70ad863
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
4 changes: 2 additions & 2 deletions R/mod_common_forms.R
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ mod_common_forms_server <- function(
observeEvent(session$userData$update_checkboxes[[form]], {
checked <- session$userData$update_checkboxes[[form]]

update_cbs(ns("common_form_table"), checked)
update_cbs(ns("SAE_table"), checked)
update_cbs("common_form_table", checked)
update_cbs("SAE_table", checked)
})

observeEvent(input$SAE_table_review_selection, {
Expand Down
2 changes: 1 addition & 1 deletion R/mod_study_forms.R
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ mod_study_forms_server <- function(
observeEvent(session$userData$update_checkboxes[[form]], {
checked <- session$userData$update_checkboxes[[form]]

update_cbs(ns("table"), checked)
update_cbs("table", checked)
})

############################### Outputs: ###################################
Expand Down
11 changes: 4 additions & 7 deletions R/shiny.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,10 @@ checkbox_create_callback <- DT::JS(
"}"
)

update_cbs <- function(tblId, checked) {
"$(':checkbox:not(.indeterminate)', $('#%s .table').DataTable().rows().nodes()).prop('checked', %s)" |>
sprintf(tblId, tolower(checked)) |>
shinyjs::runjs()
"$(':checkbox.indeterminate', $('#%s .table').DataTable().rows().nodes()).prop('checked', %s).prop('indeterminate', false).prop('readOnly', %s)" |>
sprintf(tblId, tolower(checked), tolower(!checked)) |>
shinyjs::runjs()
update_cbs <- function(id, checked, session = getDefaultReactiveDomain()) {
tblId <- session$ns(id)
params <- list(id = tblId, checked = checked)
session$sendCustomMessage('update_checkboxes', params)
}

progress_bar <- function(outputId) {
Expand Down
11 changes: 11 additions & 0 deletions inst/app/www/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ function ts(cb) {

$(document).ready(function() {

Shiny.addCustomMessageHandler("update_checkboxes", function(params) {
var table = $('#' + params.id + " .table").DataTable()
table.column(0).data().each(function(data){data.updated = params.checked})
$(':checkbox:not(.indeterminate)', table.rows().nodes()).
prop('checked', params.checked);
$(':checkbox.indeterminate', table.rows().nodes()).
prop('checked', params.checked).
prop('indeterminate', false).
prop('readOnly', params.checked);
});

/* Define custom Shiny input binding for overall review checkbox.
This is needed to assign an event priority to the checkbox.*/
var customCheckbox = new Shiny.InputBinding();
Expand Down

0 comments on commit 70ad863

Please sign in to comment.