Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.
This repository was archived by the owner on Aug 29, 2025. It is now read-only.

style_data_condtional - "row_index" won't accept vector of values #608

@CanerIrfanoglu

Description

@CanerIrfanoglu

I am using dashTable 4.0.2 and below is an example of highlighting multiple dashDataTable rows in Dash for R.

library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)

# Load data
data(mtcars)

# Create DashTable
carsDashDT <- dashDataTable(
  id = "cars-table",
  columns = lapply(colnames(mtcars),
                   function(colName){
                     list(
                       id = colName,
                       name = colName
                     )
                   }),
  data = df_to_list(mtcars),
  row_selectable = "multi",
)

# App Start ------------------------------

# Initate Application
app <- Dash$new()

# Create Layout ------------------------------

app$layout(
  htmlDiv(list(
    carsDashDT
  ))
)

# Callback Start ------------------------------

# Highlight dashTable rows
app$callback(
  output(id = "cars-table", property = "style_data_conditional"),
params = list(
  input(id = "cars-table", property = "selected_rows")
),

  function(rows) {
    style_data_conditional <- NULL

        for (i in 1:length(rows)) {
          rowIndex <- rows[[i]]
          style_data_conditional_append = list(list(
            "if" =  list("row_index" = rowIndex),
            "backgroundColor" = "#B0BED9"
          ))
          style_data_conditional <- c(style_data_conditional,
                                      style_data_conditional_append)
        }

    return(style_data_conditional)
  }
)

app$run_server()

The example provided at https://dashr.plot.ly/datatable/style under Conditional Formatting - Highlighting Certain Rows demonstrates how to highlight a single row with a numeric input.

Based on the example provided in the above link, I experimented with multiple different syntax but couldn't achieve highlighting multiple rows without using a loop.

The documentation for dashDataTable > style_data_conditional mentions:

row_index (numeric | a value equal to: 'odd', 'even'; optional)

It seems like row_index is currently limited to accepting single numeric input. It would be very nice to be able to apply conditional styles to multiple rows by feeding a vector/list of values. Rather than the loop in the callback, something similar to:

 style_data_conditional_append = list(list(
    "if" =  list("row_index" = rows)
))

In case I am missing it, I would appreciate if you can provide the correct syntax. Thanks.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions