英文:
How to implement expand/collapse rows in HTML table
问题
以下是翻译的内容:
我想要在点击时展开某些行,即通过点击箭头来展开。在 reactable
中是否有一种方法可以添加特定行的 JavaScript 代码?在 reactable
中存在一个展开/折叠所有行的选项(请参阅这里),但它不完全符合我的需求。是否有任何了解 reactable 和 JavaScript 的专家能够提出如何实现这个需求的想法?
所需的输出是使用 kableExtra
进行设计,但我认为我的问题更适合使用 reactable
。
df_open <- tibble::tibble(
Category = c("Fruits ▼︎", "Apple", "Banana",
"Kiwi", "Vegetable ▼︎", "Carrots ▼︎", "Red Carrots", "Orange Carrots", "Diary"),
Value_sum = c(1:9),
Value_one = c(10:18),
Value_two = c(200:208)
)
df_closed <- tibble::tibble(
Category = c("Fruits ▶︎","Vegetable ▶&#FE0E;", "Diary"),
Value_sum = c(1, 5,9),
Value_one = c(10,14,18),
Value_two = c(200, 204,208)
)
library(kableExtra)
# 所有折叠,全部显示
kbl(df_open, escape = FALSE) %>%
kable_styling(bootstrap_options = "bordered") %>%
row_spec(row = c(2:4, 6), extra_css = "padding-left:30px;") %>%
row_spec(row = c(7,8), extra_css = "padding-left:50px")
# 完全折叠的表格
kbl(df_closed, escape = FALSE) %>%
kable_styling(bootstrap_options = "bordered")
# 原始数据
df <- tibble::tibble(
Category = c("Fruits", "Apple", "Banana", "Kiwi", "Vegetable", "Carrots", "Red Carrots", "Orange Carrots", "Diary"),
Value_sum = c(1:9),
Value_one = c(10:18),
Value_two = c(200:208),
Another_column = "ABC"
)
# 我的尝试,使用这个博客帖子
library(dplyr)
library(reactable)
top_level <- df %>%
mutate(id = c(1,1,1,1,2,2, 2,2,3),
id_large = c(1, NA, NA, NA,2, NA, NA, NA, 3)) %>%
filter(!is.na(id_large)) %>%
relocate(id, .before = 1) %>%
select(-id_large)
second_level <- df %>%
mutate(id = c(1,1,1,1,2,2, 2,2,3),
id_large = c(1, NA, NA, NA,2, NA, NA, NA, 3)) %>%
filter(is.na(id_large)) %>%
relocate(id, .before = 1) %>%
select(-id_large)
reactable(
data = top_level,
compact = TRUE,
striped = TRUE,
resizable = TRUE,
columns = list(
id = colDef(name = "ID", show = FALSE)),
details = function(index) { # index is the row number of current row.
sec_lvl = second_level[second_level$id == top_level$id[index], ]
reactable(data = sec_lvl,
compact = TRUE,
bordered = TRUE,
resizable = TRUE,
columns = list(
id = colDef(name = "ID", show = FALSE))
)
}
)
希望这些翻译对您有所帮助。
英文:
I would like to make some rows expand on click, i.e. by clicking on the arrow. Is there a way in reactable
to add java script code to specific rows? There exists and option for expanding/collapsing all rows on click in reactable
(see here), but it does not do exactly what I need. Is there any reactable/javascript guru out there who has an idea how this might be feasible.
The required output is designed with kableExtra
but I think my problem is more suited for reactable
.
df_open <- tibble::tibble(
Category = c("Fruits &#x25bc;&#xFE0E;", "Apple", "Banana",
"Kiwi", "Vegetable &#x25bc;&#xFE0E;", "Carrots &#x25bc;&#xFE0E;", "Red Carrots", "Orange Carrots", "Diary"),
Value_sum = c(1:9),
Value_one = c(10:18),
Value_two = c(200:208)
)
df_closed <- tibble::tibble(
Category = c("Fruits &#x25b6;&#xFE0E","Vegetable &#x25b6;&#xFE0E;", "Diary"),
Value_sum = c(1, 5,9),
Value_one = c(10,14,18),
Value_two = c(200, 204,208)
)
library(kableExtra)
# all collapsed, everything shown
kbl(df_open, escape = FALSE) |>
kable_styling(bootstrap_options = "bordered") |>
row_spec(row = c(2:4, 6), extra_css = "padding-left:30px;") |>
row_spec(row = c(7,8), extra_css = "padding-left:50px")
# all closed
kbl(df_closed, escape = FALSE) |>
kable_styling(bootstrap_options = "bordered")
Completely folded table:
Raw Data:
df <- tibble::tibble(
Category = c("Fruits", "Apple", "Banana", "Kiwi", "Vegetable", "Carrots", "Red Carrots", "Orange Carrots", "Diary"),
Value_sum = c(1:9),
Value_one = c(10:18),
Value_two = c(200:208),
Another_column = "ABC"
)
My attempt using this blog post
library(dplyr)
library(reactable)
top_level <- df |>
mutate(id = c(1,1,1,1,2,2, 2,2,3),
id_large = c(1, NA, NA, NA,2, NA, NA, NA, 3)) |>
filter(!is.na(id_large)) |>
relocate(id, .before = 1) |>
select(-id_large)
second_level <- df |>
mutate(id = c(1,1,1,1,2,2, 2,2,3),
id_large = c(1, NA, NA, NA,2, NA, NA, NA, 3)) |>
filter(is.na(id_large)) |>
relocate(id, .before = 1) |>
select(-id_large)
reactable(
data = top_level,
compact = TRUE,
striped = TRUE,
resizable = TRUE,
columns = list(
id = colDef(name = "ID", show = FALSE)),
details = function(index) { # index is the row number of current row.
sec_lvl = second_level[second_level$id == top_level$id[index], ]
reactable(data = sec_lvl,
compact = TRUE,
bordered = TRUE,
resizable = TRUE,
colna
columns = list(
id = colDef(name = "ID", show = FALSE))
)
}
)
答案1
得分: 1
我不确定{reactable}目前是否支持这个功能,但看起来有一个相关的功能请求:https://github.com/glin/reactable/issues/147
{DT}包可能能够实现类似的效果。使用这篇博客文章中的代码:https://laustep.github.io/stlahblog/posts/DT_childTables.html,您可以创建类似的内容。在下面的示例中,NestedData
函数和callback
直接从博客文章中复制。
思路是将您的数据存储为嵌套列表。理想情况下,您的df
数据框中应该有一个列来标识表格的级别,并使用它来进行子集操作,而不是像我在这里手动创建不同的数据框。
main <- tibble::tribble(
~Category, ~Value_sum, ~Value_one, ~Value_two, ~Another_column,
"Fruits", 1, 10, 200, "ABC",
"Vegetable", 5, 14, 204, "ABC",
"Dairy", 9, 18, 208, "ABC",
)
fruits <- tibble::tribble(
~Category, ~Value_sum, ~Value_one, ~Value_two, ~Another_column,
"Apple", 2, 11, 200, "ABC",
"Banana", 3, 12, 201, "ABC",
"Kiwi", 4, 13, 202, "ABC",
)
veg <- tibble::tribble(
~Category, ~Value_sum, ~Value_one, ~Value_two, ~Another_column,
"Carrots", 6, 15, 205, "ABC"
)
carrots <- tibble::tribble(
~Category, ~Value_sum, ~Value_one, ~Value_two, ~Another_column,
"Red Carrots", 2, 11, 200, "ABC",
"Orange Carrots", 3, 12, 201, "ABC"
)
Dat <- NestedData(
dat = main,
children = list(
fruits,
list(
veg,
children = list(
carrots
)
),
data.frame(NULL)
)
)
library(DT)
datatable(
Dat,
callback = callback, rownames = rowNames, escape = -colIdx-1,
options = list(
paging = FALSE,
searching = FALSE,
columnDefs = list(
list(
visible = FALSE,
targets = ncol(Dat)-1+colIdx
),
list(
orderable = FALSE,
className = "details-control",
targets = colIdx
),
list(
className = "dt-center",
targets = "_all"
)
)
)
)
产生类似这样的效果:
英文:
I'm not sure this is currently possible with {reactable} - but it looks like an open feature request: https://github.com/glin/reactable/issues/147
The {DT} package might be able to do something like this. Using the code in this blog post: https://laustep.github.io/stlahblog/posts/DT_childTables.html you can create something similar. In the example below, the NestedData
function and callback
are copied directly from the blog post.
The idea is to store your data as a nested list. Ideally, you'd have some column in your df
data frame that identifies what level the table is and use that to subset it, rather than manually creating the different data frames as I've done here.
main <- tibble::tribble(
~Category, ~Value_sum, ~Value_one, ~Value_two, ~Another_column,
"Fruits", 1, 10, 200, "ABC",
"Vegetable", 5, 14, 204, "ABC",
"Dairy", 9, 18, 208, "ABC",
)
fruits <- tibble::tribble(
~Category, ~Value_sum, ~Value_one, ~Value_two, ~Another_column,
"Apple", 2, 11, 200, "ABC",
"Banana", 3, 12, 201, "ABC",
"Kiwi", 4, 13, 202, "ABC",
)
veg <- tibble::tribble(
~Category, ~Value_sum, ~Value_one, ~Value_two, ~Another_column,
"Carrots", 6, 15, 205, "ABC"
)
carrots <- tibble::tribble(
~Category, ~Value_sum, ~Value_one, ~Value_two, ~Another_column,
"Red Carrots", 2, 11, 200, "ABC",
"Oraneg Carrots", 3, 12, 201, "ABC"
)
Dat <- NestedData(
dat = main,
children = list(
fruits,
list(
veg,
children = list(
carrots
)
),
data.frame(NULL)
)
)
library(DT)
datatable(
Dat,
callback = callback, rownames = rowNames, escape = -colIdx-1,
options = list(
paging = FALSE,
searching = FALSE,
columnDefs = list(
list(
visible = FALSE,
targets = ncol(Dat)-1+colIdx
),
list(
orderable = FALSE,
className = "details-control",
targets = colIdx
),
list(
className = "dt-center",
targets = "_all"
)
)
)
)
gives something like this:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论