Skip to content

Commit

Permalink
DataGridCheckBoxRenderer
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed Mar 7, 2024
1 parent 6027d71 commit 03be6a4
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
84 changes: 84 additions & 0 deletions examples/zzz-grid_editor-checkbox.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@

library(toastui)
library(shiny)

ui <- fluidPage(
theme = bslib::bs_theme(version = 5L),
tags$h2("Checkbox column grid demo"),
tags$script(HTML(
'class DatagridCheckboxRenderer {
constructor(props) {
const el = document.createElement("div");
el.className = "form-check form-switch my-1 d-flex justify-content-center";
const input = document.createElement("input");
const { grid, rowKey, columnInfo } = props;
const checked = Boolean(props.value);
input.type = "checkbox";
input.className = "form-check-input";
input.style.cursor = "pointer";
input.checked = checked;
input.addEventListener("change", () => {
if (input.checked) {
grid.setValue(rowKey, columnInfo.name, "TRUE");
} else {
grid.setValue(rowKey, columnInfo.name, "FALSE");
}
});
el.appendChild(input);
this.el = el;
this.render(props);
}
getElement() {
return this.el;
}
render(props) {
//const checked = Boolean(props.value);
//this.el.checked = checked;
}
}
'
)),
fluidRow(
column(
width = 8,
datagridOutput("grid1"),
verbatimTextOutput("edited1")
)
)
)

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

output$grid1 <- renderDatagrid({
mydata <- data.frame(
month = month.name,
value = sample(c(TRUE, FALSE), 12, replace = TRUE)
)
grid <- datagrid(mydata, data_as_input = TRUE)
# grid$x$options$editingEvent <- "click"
grid %>%
grid_columns(
columns = "value",
# editor = list(
# type = htmlwidgets::JS("DatagridCheckboxRenderer"),
# options = list()
# ),
renderer = list(
type = htmlwidgets::JS("DatagridCheckboxRenderer"),
options = list()
)
)

})

output$edited1 <- renderPrint({
input$grid1_data
})

}

if (interactive())
shinyApp(ui, server)

32 changes: 32 additions & 0 deletions srcjs/modules/grid-renderer-checkbox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class DatagridCheckboxRenderer {
constructor(props) {
const el = document.createElement("div");
el.className = "form-check form-switch d-flex justify-content-center my-1";
const input = document.createElement("input");
const { grid, rowKey, columnInfo } = props;
const checked = Boolean(props.value);
input.type = "checkbox";
input.className = "form-check-input";
input.style.cursor = "pointer";
input.checked = checked;
input.addEventListener("change", () => {
if (input.checked) {
grid.setValue(rowKey, columnInfo.name, "TRUE");
} else {
grid.setValue(rowKey, columnInfo.name, "FALSE");
}
});
el.appendChild(input);
this.el = el;
this.render(props);
}

getElement() {
return this.el;
}

render(props) {
//const checked = Boolean(props.value);
//this.el.checked = checked;
}
}

0 comments on commit 03be6a4

Please sign in to comment.