justpy icon indicating copy to clipboard operation
justpy copied to clipboard

ag-grid return all updated data in grid

Open rroyer-xyz opened this issue 4 years ago • 3 comments

Hello,

I have a case where users are making updates to the grid (e.g., adding new blank rows via a button, updating cell values in a row by clicking on a cell and typing, re-ordering rows).

They also have a "Save" button to save the updates that have been made to the grid. Is there a way when this "Save" button is clicked to read in all the updated data in the grid?

I was looking into using the code below; however, I need to store this grid data as a python variable so that I can perform additional actions. Any thoughts on how best to return all grid data and store as a python variable?

async def save_clicked(self, msg):
    await self.grid.run_api('getDataAsCsv()', msg.page)

    ## need something like this...
    my_data = await self.grid.run_api('getDataAsCsv()', msg.page)

rroyer-xyz avatar Sep 10 '20 17:09 rroyer-xyz

I see why this would be useful. I will add this in a future version. Currently the run_api method does not allow getting values.

The work around I would recommend for now is capturing the cellValueChanged event and using it to update the grid on the python side.

Run the example below and edit fields. The event handler grid_change will run and in msg you will have all the information you need to update the grid data on the python side.

import justpy as jp

grid_options = """
{


    defaultColDef: {
        filter: true,
        sortable: true,
        resizable: true,
        cellStyle: {textAlign: 'center'},
        headerClass: 'font-bold'
    }, 
      columnDefs: [
      {headerName: "Make", field: "make", editable: true},
      {headerName: "Model", field: "model", editable: true},
      {headerName: "Price", field: "price", editable: true},
      {headerName: "Enabled", field: "enabled", cellRenderer: 'checkboxRenderer'}
    ],
      rowData: [
      {make: "Toyota", model: "Celica", price: 35000, enabled: false},
      {make: "Ford", model: "Mondeo", price: 32000, enabled: true},
      {make: "Porsche", model: "Boxter", price: 72000, enabled: false}
    ]
}
"""


def grid_change(self, msg):
    print(msg)


def grid_test():
    wp = jp.WebPage()
    grid = jp.AgGrid(a=wp, options=grid_options)
    grid.on('cellValueChanged', grid_change)
    return wp


jp.justpy(grid_test)

Please let me know if this is not clear.

elimintz avatar Sep 11 '20 17:09 elimintz

Thanks, this should be good enough for now

rroyer-xyz avatar Sep 14 '20 16:09 rroyer-xyz

Is this for Milestone 0.3.0 or should it be moved to ideas?

WolfgangFahl avatar Aug 20 '22 10:08 WolfgangFahl

In our implementations we kept a copy of the original list of dicts in background. See e.g. https://github.com/WolfgangFahl/pyCEURmake/blob/fbc35c01f5f8959b1f5fa6658b58f21e4d415976/ceurws/volumebrowser.py#L761

WolfgangFahl avatar Sep 20 '23 09:09 WolfgangFahl