Skip to content

Commit 31bae83

Browse files
authored
Merge pull request #38 from cynkra/f-17-sortable
F 17 sortable
2 parents efb6361 + 2a86182 commit 31bae83

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

R/cheetah.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#' @param search Whether to enable a search field on top of the table.
1313
#' Default to `disabled`. Use `exact` for exact matching
1414
#' or `contains` to get larger matches.
15+
#' @param sortable Logical. Whether to enable sorting on all columns. Defaults to TRUE.
1516
#'
1617
#' @return An HTML widget object of class 'cheetah' that can be:
1718
#' \itemize{
@@ -33,7 +34,8 @@ cheetah <- function(
3334
height = NULL,
3435
elementId = NULL,
3536
rownames = TRUE,
36-
search = c("disabled", "exact", "contains")
37+
search = c("disabled", "exact", "contains"),
38+
sortable = TRUE
3739
) {
3840
search <- match.arg(search)
3941
# Only show rownames if they are character strings (meaningful) and rownames is TRUE
@@ -49,6 +51,7 @@ cheetah <- function(
4951

5052
columns <-
5153
update_col_list_with_classes(data, columns) %>%
54+
make_table_sortable(sortable = sortable) %>%
5255
add_field_to_list()
5356

5457
data_json <- toJSON(data, dataframe = "rows")

R/utils.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,16 @@ check_action_type <- function(action = NULL, column_type = NULL) {
126126
stop("'inline_menu' action can only be used with 'menu' column type")
127127
}
128128
}
129+
130+
make_table_sortable <- function(columns, sortable = TRUE) {
131+
if (!sortable) {
132+
return(columns)
133+
} else {
134+
for (col_name in names(columns)) {
135+
if (is.null(columns[[col_name]]$sort)) {
136+
columns[[col_name]]$sort <- TRUE
137+
}
138+
}
139+
}
140+
columns
141+
}

man/cheetah.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/cheetahR.qmd

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,23 +121,20 @@ cheetah(
121121
)
122122
```
123123

124-
## Filtering data
125-
126-
You can filter data by setting `search` to either `exact` or `contains` when you call `cheetah()` like so:
124+
## Sorting option in cheetahR
127125

128-
```{r}
129-
cheetah(penguins, search = "contains")
126+
By default a cheetahR table is sortable. Otherwise, set `sortable = FALSE` in `cheetah()` to disable this functionality:
127+
```{r, eval=TRUE}
128+
cheetah(mtcars, rownames = FALSE, sortable = FALSE)
130129
```
131130

132-
<br/>
133-
134-
## Sortable columns
135131

136-
To make a column sortable, you can pass `sort = TRUE` to the `column_def()` function:
132+
However, to indivdually control the sorting option of each columns in the table, pass `sort = TRUE` to the `column_def()`:
137133

138134
```{r}
139135
cheetah(
140136
mtcars,
137+
sortable = FALSE,
141138
columns = list(
142139
rownames = column_def(
143140
width = 150,
@@ -154,6 +151,7 @@ If you want finer control over the sorting logic and provide your own, you can p
154151
```{r}
155152
cheetah(
156153
mtcars,
154+
sortable = FALSE,
157155
columns = list(
158156
rownames = column_def(
159157
width = 150,
@@ -167,6 +165,16 @@ cheetah(
167165
)
168166
```
169167

168+
## Filtering data
169+
170+
You can filter data by setting `search` to either `exact` or `contains` when you call `cheetah()` like so:
171+
172+
```{r}
173+
cheetah(penguins, search = "contains")
174+
```
175+
176+
<br/>
177+
170178

171179
## `cheetah()` usage in Shiny
172180
cheetahR works seamlessly in a Shiny app. You can use it in both the UI and server components. In the UI, simply call `cheetahR::cheetahOutput()` to create a placeholder for the grid. In the server, use `cheetahR::renderCheetah()` to render the grid with your data and options.

0 commit comments

Comments
 (0)