How to get the ag-grid filter values on justPy Python

286 views Asked by At

I created function for taking the ag-grid filter values using run_api class in justPy. The code looks like this:

import justpy as jp

grid_options = """
{
    rowSelection: 'multiple',
    defaultColDef: {
        filter: true,
        sortable: true,
        resizable: true,
        cellStyle: {textAlign: 'center'},
        headerClass: 'font-bold'
    }, 
      columnDefs: [
      {headerName: "Make", field: "make"},
      {headerName: "Model", field: "model"},
      {headerName: "Price", field: "price"}
    ],
      rowData: [
      {make: "Toyota", model: "Celica", price: 35000},
      {make: "Ford", model: "Mondeo", price: 32000},
      {make: "Porsche", model: "Boxter", price: 72000}
    ]
}
"""

def row_selected(self, msg):
    wp = msg.page
    if msg.selected:
        wp.selected_rows[msg.rowIndex] = msg.data
    else:
        wp.selected_rows.pop(msg.rowIndex)
    s = f'Selected rows {sorted(list(wp.selected_rows.keys()))}'
    for i in sorted(wp.selected_rows):
        s = f'{s}\n Row {i}  Data: {wp.selected_rows[i]}'
    if wp.selected_rows:
        wp.rows_div.text = s
    else:
        wp.rows_div.text = 'No row selected'

async def get_all_filter(self, msg):
    filterValues = await self.grid.run_api('getFilterModel()', msg.page)
    print(filterValues)
    msg.page.filterValues = filterValues

async def select_all_rows(self, msg):
    await self.grid.run_api('selectAll()', msg.page)


async def deselect_rows(self, msg):
    await self.grid.run_api('deselectAll()', msg.page)


def grid_test():
    wp = jp.WebPage()
    wp.selected_rows = {}  # Dictionary holding selected rows
    wp.filterValues = {} # Dictionary holding grid filter values
    grid = jp.AgGrid(a=wp, options=grid_options, style='height: 200px; width: 300px; margin: 0.25em')
    grid.options.columnDefs[0].checkboxSelection = True
    grid.on('rowSelected', row_selected)
    wp.rows_div = jp.Pre(text='Data will go here when you select rows', classes='border text-lg', a=wp)
    btn_deselect = jp.Button(text='Deselect rows', classes=jp.Styles.button_simple+' m-2', a=wp, click=deselect_rows)
    btn_deselect.grid = grid
    btn_select_all = jp.Button(text='Select all rows', classes=jp.Styles.button_simple+' m-2', a=wp, click=select_all_rows)
    btn_select_all.grid = grid
    btn_get_filter = jp.Button(text='Get apllied filter', classes=jp.Styles.button_simple+' m-2', a=wp, click=get_all_filter)
    btn_get_filter.grid = grid
    return wp


jp.justpy(grid_test)

The code is an example from justPy documentation. The grid's filter value will be put inside the dictionary msg.page.filterValues which I want to access later. When I run the code and click the button Get apllied filter and see the the console, the value filterValues is None. Can someone point out what is wrong in here? Thanks

0

There are 0 answers