justpy-org / justpy

An object oriented high-level Python Web Framework that requires no frontend programming
https://justpy.io
Apache License 2.0
1.22k stars 96 forks source link

ag-grid return all updated data in grid #138

Closed rroyer-xyz closed 1 year ago

rroyer-xyz commented 4 years ago

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)
elimintz commented 4 years ago

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.

rroyer-xyz commented 4 years ago

Thanks, this should be good enough for now

WolfgangFahl commented 2 years ago

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

WolfgangFahl commented 1 year ago

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