如何在HTML表格中实现展开/折叠行

huangapple go评论73阅读模式
英文:

How to implement expand/collapse rows in HTML table

问题

以下是翻译的内容:

我想要在点击时展开某些行,即通过点击箭头来展开。在 reactable 中是否有一种方法可以添加特定行的 JavaScript 代码?在 reactable 中存在一个展开/折叠所有行的选项(请参阅这里),但它不完全符合我的需求。是否有任何了解 reactable 和 JavaScript 的专家能够提出如何实现这个需求的想法?

所需的输出是使用 kableExtra 进行设计,但我认为我的问题更适合使用 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;&#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 &lt;- tibble::tibble(
Category = c(&quot;Fruits &amp;#x25bc;&amp;#xFE0E;&quot;, &quot;Apple&quot;, &quot;Banana&quot;,
&quot;Kiwi&quot;, &quot;Vegetable &amp;#x25bc;&amp;#xFE0E;&quot;, &quot;Carrots &amp;#x25bc;&amp;#xFE0E;&quot;, &quot;Red Carrots&quot;, &quot;Orange Carrots&quot;, &quot;Diary&quot;), 
Value_sum = c(1:9), 
Value_one = c(10:18),
Value_two = c(200:208)
)
df_closed &lt;- tibble::tibble(
Category = c(&quot;Fruits &amp;#x25b6;&amp;#xFE0E&quot;,&quot;Vegetable &amp;#x25b6;&amp;#xFE0E;&quot;,  &quot;Diary&quot;), 
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) |&gt; 
kable_styling(bootstrap_options = &quot;bordered&quot;) |&gt; 
row_spec(row = c(2:4, 6), extra_css = &quot;padding-left:30px;&quot;) |&gt; 
row_spec(row = c(7,8), extra_css = &quot;padding-left:50px&quot;)

Completely unfolded table:
如何在HTML表格中实现展开/折叠行

# all closed
kbl(df_closed, escape = FALSE) |&gt; 
kable_styling(bootstrap_options = &quot;bordered&quot;) 

Completely folded table:

如何在HTML表格中实现展开/折叠行

Raw Data:

df &lt;- tibble::tibble(
Category = c(&quot;Fruits&quot;, &quot;Apple&quot;, &quot;Banana&quot;, &quot;Kiwi&quot;, &quot;Vegetable&quot;, &quot;Carrots&quot;, &quot;Red Carrots&quot;, &quot;Orange Carrots&quot;, &quot;Diary&quot;), 
Value_sum = c(1:9), 
Value_one = c(10:18),
Value_two = c(200:208), 
Another_column = &quot;ABC&quot;
)

My attempt using this blog post

library(dplyr)
library(reactable)
top_level &lt;- df |&gt; 
mutate(id = c(1,1,1,1,2,2, 2,2,3),
id_large = c(1, NA, NA, NA,2, NA, NA, NA, 3)) |&gt; 
filter(!is.na(id_large)) |&gt; 
relocate(id, .before = 1) |&gt; 
select(-id_large)
second_level &lt;- df |&gt; 
mutate(id = c(1,1,1,1,2,2, 2,2,3),
id_large = c(1, NA, NA, NA,2, NA, NA, NA, 3)) |&gt; 
filter(is.na(id_large)) |&gt; 
relocate(id, .before = 1) |&gt; 
select(-id_large)
reactable(
data       = top_level,
compact    = TRUE, 
striped    = TRUE, 
resizable  = TRUE, 
columns    = list(
id   = colDef(name = &quot;ID&quot;, 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 = &quot;ID&quot;, show = FALSE))
)
}
)

如何在HTML表格中实现展开/折叠行

答案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"
      )
    )
  )
)

产生类似这样的效果:

如何在HTML表格中实现展开/折叠行

英文:

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 &lt;- tibble::tribble(
  ~Category,          ~Value_sum, ~Value_one, ~Value_two, ~Another_column,
  &quot;Fruits&quot;,                 1,        10,       200, &quot;ABC&quot;, 
  &quot;Vegetable&quot;,              5,        14,       204, &quot;ABC&quot;,  
  &quot;Dairy&quot;,                  9,        18,       208, &quot;ABC&quot;,  
)

fruits &lt;- tibble::tribble(
  ~Category,          ~Value_sum, ~Value_one, ~Value_two, ~Another_column,
  &quot;Apple&quot;,                 2,        11,       200, &quot;ABC&quot;, 
  &quot;Banana&quot;,                3,        12,       201, &quot;ABC&quot;,  
  &quot;Kiwi&quot;,                  4,        13,       202, &quot;ABC&quot;,  
) 

veg &lt;- tibble::tribble(
  ~Category,          ~Value_sum, ~Value_one, ~Value_two, ~Another_column,
  &quot;Carrots&quot;,                 6,        15,       205, &quot;ABC&quot;
)

carrots &lt;- tibble::tribble(
  ~Category,          ~Value_sum, ~Value_one, ~Value_two, ~Another_column,
  &quot;Red Carrots&quot;,                 2,        11,       200, &quot;ABC&quot;, 
  &quot;Oraneg Carrots&quot;,                3,        12,       201, &quot;ABC&quot;
  ) 

Dat &lt;- 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 = &quot;details-control&quot;, 
        targets = colIdx
      ),
      list(
        className = &quot;dt-center&quot;, 
        targets = &quot;_all&quot;
      )
    )
  )
)

gives something like this:

如何在HTML表格中实现展开/折叠行

huangapple
  • 本文由 发表于 2023年2月19日 04:13:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496120.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定