Jaša Dimič
Jaša Dimič

Reputation: 1

Ag-Grid delete selected rows, update the Ag-Grid table and save the selected rows to new DataFrame Streamlit

I’m trying to delete selected rows in the Ag-Grid table and after deletion update/refresh the Ag-Grid table. Also I would like to save the selected rows to the new DataFrame.

It that possible to do with just one button click (on click on the button the Ag-Grid table updates, deletes the selected rows and saves them to DataFrame)?

I've tried the way below but after selecting the checkbox row is deleted immediately and I do not know how (if possible) you could save it.

Here is the example code:

df = pd.read_excel('test.xlsx', sheet_name='base_table')

js = JsCode("""
    function(e) {
        let api = e.api;     
        let sel = api.getSelectedRows();
        api.applyTransaction({remove: sel});
    };
    """)

gb_base = GridOptionsBuilder.from_dataframe(df, enableRowGroup=True, editable=True, groupable = True)
gb_base.configure_pagination(enabled=True, paginationAutoPageSize = False, paginationPageSize=20)
gb_base.configure_selection('multiple', use_checkbox=True)
gb_base.configure_grid_options(onRowSelected = js, pre_selected_rows = []) 
grid_options_base = gb_base.build()
table_base = AgGrid(df, gridOptions=grid_options_base, allow_unsafe_jscode=True, fit_columns_on_grid_load=True)

Upvotes: 0

Views: 1195

Answers (1)

GreyBeardedGeek
GreyBeardedGeek

Reputation: 30088

I recommend that instead of regarding the grid as the holder of your data, you keep a data model in your code, and operate on that code.

The grid is a user interface component, it is not (should not be) your data store.

Once you have a data store that is not the grid, bind your grid to your data, e.g. the grid's "rows" property.

Then, in your function, find the selection (as your are already doing), save them off wherever you need to, and then remove that data from your data store (instead of using an AgGrid Transaction to do so).

The grid will then re-render to match the new state of your data store (though you may have to assign the remaining data 'rows' to a new array instance to trigger the re-render).

Upvotes: 0

Related Questions