AgGrid - Selection clears after clicking checkbox

Hello! I’m having an issue with the AgGrid dynamic table. I want users to be able to select multiple rows of a data frame, but when clicking on selectbox, the table reloads and clears the selection. The issue happens from time to time - sometimes I’m able to select 3-4 rows and then after selecting another one, the whole table clears the previous selection. I tried it on the latest versions of the streamlit and aggrid as well as on previous versions. I know that I can change the update mode to manual and it solves the problem but I don’t really want users to click that update button and wondering if there is an easy way to fix this.

Did someone encounter such issue?
Thanks in advance!

Here’s my code:

# define path to excel data source
path = os.path.dirname(__file__)
my_file = path+'/data.xlsx'


# function to load excel data to pandas dataframe
@st.cache(allow_output_mutation=True)
def load_data():
    df = pd.read_excel(my_file)
    return df

#load source data
data = load_data()

gb = GridOptionsBuilder.from_dataframe(data)
gb.configure_selection(selection_mode="multiple", use_checkbox=True)
gridOptions = gb.build()

response = AgGrid(
    data,
    gridOptions=gridOptions,
    enable_enterprise_modules=False,
    height=600,
    update_mode=GridUpdateMode.SELECTION_CHANGED,
    data_return_mode=DataReturnMode.FILTERED_AND_SORTED,
    fit_columns_on_grid_load=False
)

response_df = pd.DataFrame(response["selected_rows"])
st.dataframe(response_df)
1 Like

Did you try : update_mode=‘MODEL_CHANGED’

I had same issue and used session_state:

if "id_row" not in st.session_state:
    st.session_state["id_row"] = ''
    selected_rows = []
else:
    selected_rows = (list(range(len(st.session_state[id_row].get('selectedRows')))))
gb.configure_selection(selection_mode="multiple", use_checkbox=True,pre_selected_rows=selected_rows)

yes I did try that, unfortunately the same issue.

Hi @Oleksandr_Arsentiev,

  1. You will need at least ‘SELECTION_CHANGED’ is you want to act on / do something further with selected rows.
  2. When selecting AGgrid rows, if you select too fast, your previous selections get sometimes unselected. Try selecting a row and pausing until you see the tick mark, and thereafter select the 2nd row.

Cheers

The session state kind of works but not entirely for my purpose. When I add a key argument to the AgGrid object, for some reason, I cannot filter the underlying dataframe by the other widgets (multiselect boxes)

Hello. Thanks for the reply. I was using ‘SELECTION_CHANGED’ from the very beginning. I think speed of selection is not the case here because the table “resets” even if I click the checkboxes with enough delay.

1 Like