I have a multi-page table where rows can be selected. I have it set-up to take the input of selected rows and highlight them with a different color. However, when I go to the next page, the highlighting from the first page remains.
This is surprising to me because in style_data_conditional
the input for row_index
seems to be the absolute row number for the table (at least for selected rows), rather than the row number tied to the viewport. Is the style_data_conditional
value of row_index
actually tied to the viewport index, rather than the actual row index? What is the best way to likely address this issue?
Using dash version 1.16.3 and dash_table version 4.10.1.
Below is an example code that highlights the issue I’m seeing. If you select 1 or 2 rows to highlight them on the first page, when you move to the next page that highlighting remains (incorrect behavior), though those rows aren’t flagged as being selected (which is correct behavior)
#Package Imports
#App Components
import dash
import dash_table
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import pandas as pd
#Start App
app = dash.Dash(__name__) #Assumes all assets are in "./assets subdirectory"
#Test Data
df = pd.DataFrame({'a': [1,3,6,10,11,15,6], 'b': [5,5,6,8,29,30,100], 'c': [10,23,100,20,53,63,21]})
#Default Cond Style
defaultCondStyle = [
{'if': {'row_index': 'odd'},
'backgroundColor': '#B6EBFE'},
{'if': {'row_index': 'even'},
'backgroundColor': '#D6C7FB'}
]
#Setup App Layout
app.layout = html.Div([
html.Div(children=[
dash_table.DataTable(
id='table',
row_selectable='multi',
columns=[{"name": c, "id": c} for c in df.columns],
data=df.to_dict('records'),
page_size=4
)],
id='table-container'
)
])
#Add Highlight Functionality
@app.callback(
Output('table', 'style_data_conditional'),
Input('table', 'selected_rows'),
Input('table', 'page_current')
)
def style_selected_rows(selRows, pg):
if selRows is None:
return defaultCondStyle
else:
return defaultCondStyle + [{'if': {'row_index': selRows}, 'backgroundColor': '#FBFAAC'}]
#Code to Run the App from the commandline
if __name__ == '__main__':
app.run_server(debug=True)