最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - Reactable expand sub-group if the number of sub-groups is 1 - Stack Overflow

programmeradmin1浏览0评论

In reactable, is there a way to automatically expand a sub-group when expanding a parent group if there is only one sub-group below it?

In this example, Quebec has two sub-groups so if I click the row, it should keep its default behavior and expand to reveal the two subgroups below it. If I click on Mississippi however, there is only one subgroup and so I would like it to automatically expand the Treatment sub-group to reveal the Plant subgroups.

    

Here is my code using the built in CO2 data. I commented out the two methods I tried.

library(reactable)

datasets::CO2 |> 
  head(60) |> 
  reactable(
    groupBy = c("Type", "Treatment", "Plant"),
    onClick = JS(
      # not working
      "function(rowInfo) {
        if (rowInfo.subRows.length == 1) {
          // rowInfo.subRows.toggleRowExpanded(true);
          // rowInfo[rowInfo.index + 1].toggleRowExpanded(true);
        }
      }"
    ),
    # extra formatting for visual aid
    defaultColDef = colDef(resizable = TRUE),
    rowStyle = JS(
      "function(rowInfo) { 
        return { background: (rowInfo.expanded === true) ? '#eee' : 'white' }; 
      }"
    )
  )

Some resources I used were:

  • This SO answer: React Table using hooks expand and collapse rows and their sandbox example. It seems like something here has the answer, I'm just not familiar enough with JS to apply the concepts
  • reactable documentation:
    • cookbook - .html#nested-tables
    • examples - .html#expandable-row-details
    • JS styling - .html#row-styling
发布评论

评论列表(0)

  1. 暂无评论