Bring Drag & Drop to Dash with Dashboard Engine. 💫 Learn how at our next webinar!

Select all rows in Dash DataTable

Hi everyone!

I want to have select/deselect all rows functionality for a DataTable with either a checkbox or a button. I saw some old posts which suggested that a select-all checkbox used to be a part of DataTable but I couldn’t find any info on it in the documentation.

I’d appreciate any help with this. Thanks!

1 Like

Hi @shanv
I don’t think that is possible right now.

I managed to find a workaround using the selected_rows property and derived view port. I’ve made Select All and Deselect All buttons in the layout as inputs. For anyone else looking for the same functionality please refer to the snippet below:

    [Output('in-table', 'selected_rows'),],
    [Input('select-all-button', 'n_clicks'),
    Input('deselect-all-button', 'n_clicks')],
    [State('in-table', 'derived_virtual_data'),]
def select_deselect(selbtn, deselbtn, selected_rows):
    ctx = dash.callback_context
    if ctx.triggered:
        trigger = (ctx.triggered[0]['prop_id'].split('.')[0])
    if trigger == 'select-all-button':
        if selected_rows is None:
            return [[]]
            return [[i for i in range(len(selected_rows))]]
        return [[]]

@shanv Thank you for your workaround!

I’ve adapted your code to create a “reset_plot_button”

    [Output(component_id='main_datatable', component_property='selected_rows'),
     Output(component_id='main_datatable', component_property='sort_by')],
    [Input(component_id='button_reset_plot', component_property='n_clicks'),],)
def select_deselect(button_reset_plot_n_clicks):
    ctx = dash.callback_context
    # trigger = ""
    if ctx.triggered:
        trigger = ctx.triggered[0]["prop_id"].split(".")[0]
        if trigger == "button_reset_plot":
            return [], []