Skip to content

Commit

Permalink
added grid_col_checkbox()
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed Mar 8, 2024
1 parent 76a0bd8 commit 001e058
Show file tree
Hide file tree
Showing 12 changed files with 220 additions and 146 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Imports:
shinyWidgets
Suggests:
apexcharter,
bslib,
knitr,
rmarkdown,
scales,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export(datagridOutput)
export(datagrid_proxy)
export(grid_click)
export(grid_col_button)
export(grid_col_checkbox)
export(grid_colorbar)
export(grid_columns)
export(grid_columns_opts)
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# toastui 0.3.3

* New function `grid_col_checkbox()` to add checkboxes into a column.


# toastui 0.3.2

* Updated tui-grid to 4.21.22.
Expand Down
86 changes: 86 additions & 0 deletions R/grid-column-renderer.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@

#' Display buttons in grid's column
#'
#' @param grid A table created with [datagrid()].
#' @param column The name of the column where to create buttons.
#' @param inputId The `input` slot that will be used to access the value.
#' @param label Label to display on button, if `NULL` use column's content.
#' @param icon Icon to display in button.
#' @param status Bootstrap status (color) of the button: default, primary, success, info, warning, danger, ...
#' A class prefixed by `btn-` will be added to the button.
#' @param btn_width Button's width.
#' @param ... Further arguments passed to [grid_columns()].
#'
#' @return A `datagrid` htmlwidget.
#' @export
#'
#' @example examples/ex-grid_col_button.R
grid_col_button <- function(grid,
column,
inputId,
label = NULL,
icon = NULL,
status = "default",
btn_width = "100%",
...) {
check_grid(grid, "grid_col_button")
stopifnot(is.character(status))
stopifnot(is.character(column) & length(column) == 1)
column <- check_grid_column(grid, column)
if (!is.null(icon)) {
icon_deps <- htmltools::findDependencies(icon)
grid$dependencies <- c(
grid$dependencies,
icon_deps
)
icon <- htmltools::doRenderTags(icon)
}
grid_columns(
grid = grid,
columns = column,
...,
renderer = list(
type = JS("datagrid.renderer.button"),
options = dropNulls(list(
status = status[1],
width = btn_width,
label = label,
inputId = inputId,
icon = icon
))
)
)
}



#' Display checkboxes in grid's column
#'
#' @param grid A table created with [datagrid()].
#' @param column The name of the column where to create buttons.
#' @param class CSS classes to add to checkbox container.
#' @param ... Further arguments passed to [grid_columns()].
#'
#' @return A `datagrid` htmlwidget.
#' @export
#'
#' @example examples/ex-grid_col_checkbox.R
grid_col_checkbox <- function(grid,
column,
class = "form-check d-flex justify-content-center my-1",
...) {
check_grid(grid, "grid_col_checkbox")
stopifnot(is.character(column) & length(column) == 1)
column <- check_grid_column(grid, column)
grid_columns(
grid = grid,
columns = column,
...,
renderer = list(
type = JS("datagrid.renderer.checkbox"),
options = dropNulls(list(
class = class
))
)
)
}
56 changes: 0 additions & 56 deletions R/grid-columns.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,59 +145,3 @@ grid_columns_opts <- function(grid,
}





#' Display buttons in grid's column
#'
#' @param grid A table created with [datagrid()].
#' @param column The name of the column where to create buttons.
#' @param inputId The `input` slot that will be used to access the value.
#' @param label Label to display on button, if `NULL` use column's content.
#' @param icon Icon to display in button.
#' @param status Bootstrap status (color) of the button: default, primary, success, info, warning, danger, ...
#' A class prefixed by `btn-` will be added to the button.
#' @param btn_width Button's width.
#' @param ... Further arguments passed to [grid_columns()].
#'
#' @return A `datagrid` htmlwidget.
#' @export
#'
#' @example examples/ex-grid_col_button.R
grid_col_button <- function(grid,
column,
inputId,
label = NULL,
icon = NULL,
status = "default",
btn_width = "100%",
...) {
check_grid(grid, "grid_col_button")
stopifnot(is.character(status))
stopifnot(is.character(column) & length(column) == 1)
column <- check_grid_column(grid, column)
if (!is.null(icon)) {
icon_deps <- htmltools::findDependencies(icon)
grid$dependencies <- c(
grid$dependencies,
icon_deps
)
icon <- htmltools::doRenderTags(icon)
}
grid_columns(
grid = grid,
columns = column,
...,
renderer = list(
type = JS("datagrid.renderer.button"),
options = dropNulls(list(
status = status[1],
width = btn_width,
label = label,
inputId = inputId,
icon = icon
))
)
)
}

44 changes: 44 additions & 0 deletions examples/ex-grid_col_checkbox.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

library(toastui)
library(shiny)
library(bslib)

ui <- fluidPage(
theme = bslib::bs_theme(version = 5L),
tags$h2("Checkbox column grid demo"),
fluidRow(
column(
width = 8,
datagridOutput("grid"),
verbatimTextOutput("edited")
)
)
)

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

output$grid <- renderDatagrid({
data.frame(
month = month.name,
checkboxes = sample(c(TRUE, FALSE), 12, replace = TRUE),
switches = sample(c(TRUE, FALSE), 12, replace = TRUE)
) %>%
datagrid(data_as_input = TRUE) %>%
grid_col_checkbox(column = "checkboxes") %>%
grid_col_checkbox(
column = "switches",
# /!\ will only works with bslib::bs_theme(version = 5L)
class = "form-check form-switch d-flex justify-content-center my-1"
)

})

output$edited <- renderPrint({
input$grid_data # outputId + "_data
})

}

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

84 changes: 0 additions & 84 deletions examples/zzz-grid_editor-checkbox.R

This file was deleted.

2 changes: 1 addition & 1 deletion inst/htmlwidgets/datagrid.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion man/grid_col_button.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 74 additions & 0 deletions man/grid_col_checkbox.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 001e058

Please sign in to comment.