Skip to content

Commit

Permalink
Resolve issue for partially reviewed rows
Browse files Browse the repository at this point in the history
  • Loading branch information
jthompson-arcus committed Dec 9, 2024
1 parent 70ad863 commit 5c64e08
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 26 deletions.
6 changes: 5 additions & 1 deletion R/fct_data_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,11 @@ datatable_custom <- function(
deferRender = TRUE,
scrollResize = TRUE,
scrollCollapse = TRUE,
colReorder = TRUE
colReorder = list(
enable = TRUE,
realtime = FALSE,
fixedColumnsLeft = 1
)
)
fixed_opts <- list(
initComplete = DT::JS(
Expand Down
4 changes: 2 additions & 2 deletions R/mod_common_forms.R
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ mod_common_forms_server <- function(
targets = 0,
render = checkbox_render
)),
createdRow = checkbox_create_callback
rowCallback = row_callback
))
})

Expand Down Expand Up @@ -226,7 +226,7 @@ mod_common_forms_server <- function(
targets = 0,
render = checkbox_render
)),
createdRow = checkbox_create_callback
rowCallback = row_callback
))
})

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 @@ -268,7 +268,7 @@ mod_study_forms_server <- function(
targets = 0,
render = checkbox_render
)),
createdRow = checkbox_create_callback
rowCallback = row_callback
))
})

Expand Down
60 changes: 39 additions & 21 deletions R/shiny.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,53 @@ shiny::registerInputHandler('CS.reviewInfo', function(val, ...) {
}, TRUE)

checkbox_callback <- DT::JS(
"table.on('column-reorder', function() {",
"table.rows().every(function() {",
"if (this.data()[0].reviewed == null) {",
"$(':checkbox', this.node()).",
"addClass('indeterminate').",
"prop('indeterminate', this.data()[0].updated == null).",
"prop('readOnly', this.data()[0].updated == false)",
"}",
"})",
"});",
"table.on('click', 'input[type=\"checkbox\"]', function(){",
"var tblId = $(this).closest('.datatables').attr('id');",
"var cell = table.cell($(this).closest('td'));",
"var rowIdx = table.row($(this).closest('tr')).index();",
"var ids = cell.data().ids;",
"var review = $(this).is(':indeterminate') ? null : $(this).is(':checked');",
"cell.data().updated = review;",
"var info = {review: review, ids: ids, row: tblId + '_row_' + rowIdx};",
"Shiny.setInputValue(tblId + '_review_selection:CS.reviewInfo', info);",
"})"
"var tblId = $(this).closest('.datatables').attr('id');",
"var cell = table.cell($(this).closest('td'));",
"var rowIdx = table.row($(this).closest('tr')).index();",
"var ids = cell.data().ids;",
"var review = $(this).is(':indeterminate') ? null : $(this).is(':checked');",
"cell.data().updated = review;",
"var info = {review: review, ids: ids, row: tblId + '_row_' + rowIdx};",
"Shiny.setInputValue(tblId + '_review_selection:CS.reviewInfo', info);",
"});"
)

checkbox_render <- DT::JS(
"function(data, type, row, meta) {",
"var reviewed = data.reviewed;",
"var updated = data.updated;",
"return `<input type='checkbox' ",
"class='${reviewed == null ? 'indeterminate' : reviewed ? 'checked' : 'unchecked'}' ",
"${updated == null ? (reviewed ? 'checked' : '') : (updated ? 'checked' : '')} ",
"${reviewed == null ? 'onclick=\"ts(this)\"' : ''}/>`;",
"var reviewed = data.reviewed;",
"var updated = data.updated;",
"var cb_class = ''",
"if (reviewed == null) {",
"cb_class = updated == null ? '' : 'indeterminate'",
"} else {",
"cb_class = reviewed ? 'checked' : 'unchecked'",
"}",
"return `<input type='checkbox' ",
"class='${cb_class}' ",
"${updated == null ? (reviewed ? 'checked' : '') : (updated ? 'checked' : '')} ",
"${reviewed == null ? 'onclick=\"ts(this)\"' : ''}/>`;",
"}"
)

checkbox_create_callback <- DT::JS(
"function(row, data, dataIndex) {",
"if (data[0].reviewed == null) {",
"let cb = row.cells[0].getElementsByTagName('input')[0]",
"cb.indeterminate = true;",
"}",
row_callback <- DT::JS(
"function(row, data) {",
"if (data[0].reviewed == null) {",
"$(':checkbox', row).",
"addClass('indeterminate').",
"prop('indeterminate', data[0].updated == null).",
"prop('readOnly', data[0].updated == false)",
"}",
"}"
)

Expand Down
2 changes: 1 addition & 1 deletion inst/app/www/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ $(document).ready(function() {
$(':checkbox.indeterminate', table.rows().nodes()).
prop('checked', params.checked).
prop('indeterminate', false).
prop('readOnly', params.checked);
prop('readOnly', !params.checked);
});

/* Define custom Shiny input binding for overall review checkbox.
Expand Down

0 comments on commit 5c64e08

Please sign in to comment.