I have a Datatable with row_selectable=‘multi’. I created a callback that adds styling to rows when they are selected. I also have code in the same callback that removes the styling if a selected row is subsequently deselected. The initial table styling is held in a list named conditional_style. The code works, but if the user selects and then deselects rows the response time in changing the styling gets quite slow, making me think I’m doing something wrong. I would appreciate any suggestions on how to improve my code. Thanks! (Also, I am new to the site, so if you have suggestions on how to improve my post, I would appreciate those as well.)
Here is the code for the callback:
# callback to highlight selected rows
@callback(
Output('rsync_table', 'style_data_conditional'),
Input('rsync_table', 'selected_rows'),
State('rsync_table', 'style_data_conditional'),
prevent_initial_call=True,
)
def style_selected_rows(selected_rows, conditional_style):
# style selected rows
table_style = conditional_style
if selected_rows is not None:
table_style += [
{'if': {'row_index':i},
'box-shadow': '0px 0px 3px 3px #eb99ff inset'}
for i in selected_rows]
# remove styling for unselected rows
if selected_rows is not None:
for i in range(len(table_style)):
if i not in selected_rows:
for column_id in ['PID', 'contact', 'div_grp', 'location', 'last_sync']:
table_style.append({
'if': {'row_index': i, 'column_id': column_id},
'box-shadow': 'unset'
})
return table_style