Backend Paging with Sorting: preserving checklists in dash table

Hello Everyone!

In a dash app, with a table, the “backend paging with sorting” functionality is implemented. The code for which is referenced from Python-Driven Filtering, Paging, Sorting | Dash for Python Documentation | Plotly.

The app upon loading checks some of the rows with the following code in the layout of dash_table.DataTable(

row_selectable=‘multi’,
selected_rows=list_of_row_ids_values_for_checkbox,

)

The user can then select more rows by checklists. However, after applying the custom sort, the following code doesn’t preserve the checkboxes:

Output(‘table’, component_property=‘derived_virtual_selected_row_ids’),
Output(‘table’, “data”)],
[Input(‘table’, “page_current”),
Input(‘table’, “page_size”),
Input(‘table’, “sort_by”)],
[
State(‘table’, component_property=‘derived_virtual_selected_row_ids’)
]
)
def update_table(page_current, page_size, sort_by,table_checked_row_ids):
if len(sort_by):
dff = df.sort_values(
sort_by[0][‘column_id’],
ascending=sort_by[0][‘direction’] == ‘asc’,
inplace=False
)
else:
# No sort is applied
dff = df

When I print the list of selected/checked row-ids, “derived_virtual_selected_row_ids”. I can see all the selected/checked rows. but these checks are not preserved when the sorting is applied.

I am returning the following the function:

return table_checked_row_ids, dff.to_dict(‘records’)

Any help is much appreciate. Why are the checkboxes not returned to the rows of table as earlier.

Best!