# Keep correct DataTable conditional formatting after sort

**URL:** <https://community.plotly.com/t/keep-correct-datatable-conditional-formatting-after-sort/80235>\
**Category:** Dash Python\
**Created:** [November 19, 2023, 5:39pm UTC](https://community.plotly.com/t/keep-correct-datatable-conditional-formatting-after-sort/80235 "2023-11-19T17:39:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Walrus](https://avatars.discourse-cdn.com/v4/letter/w/51bf81/32.png) [@Walrus](https://community.plotly.com/u/Walrus)\
**Post date:** [November 19, 2023, 5:39pm UTC](https://community.plotly.com/t/keep-correct-datatable-conditional-formatting-after-sort/80235/1 "2023-11-19T17:39:40Z")

</div>

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](https://dash.plotly.com/datatable/conditional-formatting)

> `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?

---

<div class="post-metadata">

**Author:** ![Walrus](https://avatars.discourse-cdn.com/v4/letter/w/51bf81/32.png) [@Walrus](https://community.plotly.com/u/Walrus)\
**Post date:** [November 19, 2023, 6:14pm UTC](https://community.plotly.com/t/keep-correct-datatable-conditional-formatting-after-sort/80235/2 "2023-11-19T18:14:05Z")

</div>

Got it.

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