Skip to content

Commit

Permalink
style headers
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Jan 20, 2024
1 parent 0344545 commit 9cd52df
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 1 deletion.
6 changes: 6 additions & 0 deletions R/style_bootstrap.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ style_bootstrap <- function(x,
ival <- if (is.null(i)) seq_len(meta(x, "nrows")) else i
jval <- if (is.null(j)) seq_len(meta(x, "ncols")) else j

# only columns means we also want to style headers
if (is.null(i) && !is.null(j)) {
ival <- c(-1 * rev(seq_len(meta(x)$nhead) - 1), ival)
}


# order may be important for recycling
settings <- expand.grid(i = ival, j = jval, tabularray = "")
if (is.null(i) && !is.null(j)) {
Expand Down
8 changes: 8 additions & 0 deletions inst/templates/bootstrap.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@
var table = document.getElementById("$tinytable_TABLE_ID");
table.rows[i].cells[j].classList.add(css_id);
}
function styleHeaderCell(i, j, css_id) {
var table = document.getElementById("$tinytable_TABLE_ID");
var headerRow = table.tHead.rows[i];
var headerRow = table.querySelector("thead tr");
if (headerRow) {
headerRow.cells[j].classList.add(css_id);
}
}
function insertSpanRow(i, colspan, content) {
var table = document.getElementById('$tinytable_TABLE_ID');
var newRow = table.insertRow(i);
Expand Down
Binary file modified inst/tinytest/_tinysnapshot/html-heatmap.rds
Binary file not shown.
Binary file modified inst/tinytest/_tinysnapshot/html-issue58.rds
Binary file not shown.
Binary file added inst/tinytest/_tinysnapshot/html-issue88.rds
Binary file not shown.
Binary file added inst/tinytest/_tinysnapshot/html-issue92.rds
Binary file not shown.
Binary file modified inst/tinytest/_tinysnapshot/html-striped.rds
Binary file not shown.
Binary file modified inst/tinytest/_tinysnapshot/html-striped_orange.rds
Binary file not shown.
Binary file modified inst/tinytest/_tinysnapshot/html-vectorized_color_j.rds
Binary file not shown.
8 changes: 8 additions & 0 deletions inst/tinytest/test-html.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ tab <- tt(x, theme = "striped") |>
style_tt(color = "orange")
expect_equal_to_reference(clean(tab), "_tinysnapshot/html-striped_orange.rds")


# Issue #92: header alignment
k <- structure(list(Column1 = c("Some text", "123"), Column2 = c("Some text",
"456")), row.names = c(NA, -2L), class = "data.frame")
tab <- tt(k, width = 1) |> style_tt(j = 2, align = "r")
expect_equal_to_reference(clean(tab), "_tinysnapshot/html-issue92.rds")


# tutorial.qmd: vectorized settings
tab <- tt(x) |>
style_tt(
Expand Down
9 changes: 8 additions & 1 deletion vignettes/tutorial.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -448,12 +448,19 @@ colnames(k) <- NULL
tt(k)
```

The header is row 0, and can thus be styled as expected:
The first is row 0, and higher level headers (ex: column spanning labels) have negative indices like -1. They can be styled as expected:

```{r}
tt(x) |> style_tt(i = 0, color = "white", background = "black")
```

When styling columns without specifying `i`, the headers are styled in accordance with the rest of the column:

```{r}
tt(x) |> style_tt(j = 2:3, color = "white", background = "black")
```


## Conditional styling

We can use the standard `which` function from Base `R` to create indices and apply conditional stying on rows. And we can use a regular expression in `j` to apply conditional styling on columns:
Expand Down

0 comments on commit 9cd52df

Please sign in to comment.