Keep correct DataTable conditional formatting after sort

I have a DataTable with sort_action=“native”. I use conditional formatting in a callback, in which a row is highlighted as below:

[ {'if': {'row_index': row_index},'backgroundColor': 'green'} ]

here, row_index = active_cell[‘row’]

The problem is that when sort is performed, the formatting does not move with the sorting action.

I’ve found the following in Conditional Formatting | Dash for Python Documentation | Plotly

id is a special hidden column that can be used as an alternative to row_index for highlighting data by index. Since each row has a unique id, the conditional formatting associated with this id will remain associated with that data after sorting and filtering.

So I’ve added a column called ‘id’ to the dataframe used to create the DataTable and indeed I have the field “row_ID” available in the active_cell data in callback ( row_ID = active_cell[‘row_id’] )

I didn’t however understand how to use this id for the conditional formatting. I’ve tried

[ {'if': {'row_ID': row_iD},'backgroundColor': 'green'} ]

and

[ {'if': {'id': row_ID},'backgroundColor': 'green'} ]

but I get “Failed component prop type” error for both of those, specifying that:

Valid keys: [
“column_id”,
“column_type”,
“filter_query”,
“state”,
“row_index”,
“column_editable”
]

How can I use the ‘id’ column then in order to format the whole row in a way that will keep correct format after sorting?

Got it.

{'if': {'filter_query': f"{{id}} = {row_ID}"},'backgroundColor': 'green'}