Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions R/cheetah.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ cheetah <- function(
is_named_list(columns) & names(columns) %in% colnames(data)
)

columns <-
update_col_list_with_classes(data, columns) %>%
add_field_to_list() %>%
toJSON(auto_unbox = TRUE)
columns <- update_col_list_with_classes(data, columns) %>%
add_field_to_list()

data_json <- toJSON(data, dataframe = "rows")
# forward options using x
Expand Down
8 changes: 6 additions & 2 deletions R/cheetah_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#' \item \code{"radio"} for radio action columns.
#' }
#' @param style Column style.
#' @param sort Whether to sort the column. Default to FALSE. May also be
#' a JS callback to create custom logic (does not work yet).
#'
#' @export
#' @return A list of column options to pass to the JavaScript API.
Expand All @@ -36,7 +38,8 @@ column_def <- function(
max_width = NULL,
column_type = NULL,
action = NULL,
style = NULL
style = NULL,
sort = FALSE
) {
check_column_type(column_type)
list(
Expand All @@ -46,6 +49,7 @@ column_def <- function(
maxWidth = max_width,
columnType = column_type,
action = action,
style = style
style = style,
sort = sort
)
}
6 changes: 5 additions & 1 deletion man/column_def.Rd

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

3 changes: 2 additions & 1 deletion tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ test_that("test utils", {
"maxWidth",
"columnType",
"action",
"style"
"style",
"sort"
)
)
})
Expand Down
40 changes: 39 additions & 1 deletion vignettes/cheetahR.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
library(dplyr)
```

```{r setup}
library(cheetahR)
library(dplyr)
```

## Your first table
Expand Down Expand Up @@ -91,3 +91,41 @@ head(airquality, 10) %>%
)
)
```

## Sortable columns

To make a column sortable, you can pass `sort = TRUE` to the `column_def()` function:

```{r}
cheetah(
mtcars,
columns = list(
rownames = column_def(
width = 150,
sort = TRUE
)
)
)
```

### Coming soon (TBD)

If you want finer control over the sorting logic and provide your own, you can pass a `htmlwidgets::JS` callback instead:

```r
cheetah(
mtcars,
columns = list(
rownames = column_def(
width = 150,
sort = htmlwidgets::JS(
"sort(order, col, grid) {
console.log(order);
console.log(col);
console.log(grid);
}"
)
)
)
)
```