在reactable中有条件地调整填充

rjee0c15  于 2023-05-26  发布在  React
关注(0)|答案(1)|浏览(136)

我想调整reactable中列中特定单元格的左填充。我可以让它为color工作(基于这个answer),但不是填充。有什么想法吗

MWE

library(reactable)
library(dplyr)

df <- iris |> 
  group_by(Species) |> 
  slice(1:3) |> 
  select(Species, Sepal.Length)

reactable::reactable(df, 
                     columns = list(
    Species =  colDef(align = "left", 
                     style = function(value, index) {
                       if (df$Species[index] == "setosa") {
                         paddingLeft <- "padding-left: 10px;"
                         color <- "red"
                       } else if (df$Species[index] == "versicolor") {
                         paddingLeft <- "padding-left: 30px !important;"
                         color <- "green"
                       } else {
                         paddingLeft <- "padding-left: 1px;"
                         color <- "black"
                       }
                       list(paddingLeft = paddingLeft, color = color)
                     })
                     ))
8ulbf1ek

8ulbf1ek1#

color类似,只将值传递给paddingLeft

library(reactable)
library(dplyr)

df <- iris |>
  group_by(Species) |>
  slice(1:3) |>
  select(Species, Sepal.Length)

reactable::reactable(df,
  columns = list(
    Species = colDef(
      align = "left",
      style = function(value, index) {
        if (df$Species[index] == "setosa") {
          paddingLeft <- "10px"
          color <- "red"
        } else if (df$Species[index] == "versicolor") {
          paddingLeft <- "30px"
          color <- "green"
        } else {
          paddingLeft <- "1px"
          color <- "black"
        }
        list(paddingLeft = paddingLeft, color = color)
      }
    )
  )
)

相关问题