Hi, Guys!
I am using advanced filters on dash Ag-Grid by enabaling the enterprise components (as suggested on the link given below) and I am trying to capture and save the filter expression being written or built using the visual builder.
I am aware that with the column level filters, it is possibe to get the current state of filters using filterModel property of Ag-Grid. But, it does not return anything in this case of advanced filters.
Is there any other property or another way to do this? Basically, I want to save the filters to know what was the exact filter expression applied on the data and display the same on another output page to enable the users to reproduce the same output using the filter expression.
PFA the screenshots and my code file.
Looking forward to resolve this issue together, ASAP!
Using column level filters with filterModel property
using advanced filters with filtermodel property
from dash import Dash, dcc, html, Input, Output, State
import pandas as pd
import dash_ag_grid
import dash_mantine_components as dmc
style_cell = {
"fontFamily": "Inter",
"fontSize": 14,
"height": "30vh",
"minWidth": "100%",
"width": "100%",
"maxWidth": "100%",
"padding": "0.5rem",
"textAlign": "center",
"fontWeight": "normal",
}
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eva', 'Frank', 'Grace', 'Hannah', 'Ivy', 'Jack'],
'City': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix', 'Philadelphia', 'San Antonio', 'San Diego', 'Dallas', 'San Jose'],
'Age': [25, 30, 35, 40, 28, 32, 27, 45, 29, 31],
'Gender': ['Female', 'Male', 'Male', 'Male', 'Female', 'Male', 'Female', 'Female', 'Female', 'Male'],
'Salary': [70000, 80000, 120000, 90000, 75000, 85000, 95000, 105000, 60000, 78000]
}
df = pd.DataFrame(data)
grid = dash_ag_grid.AgGrid(
id='ag-grid',
columnDefs=[{"headerName": c, "field": c} for c in df.columns],
rowData=df.to_dict("records"),
className="ag-theme-balham color-setting",
rowStyle={"defaultStyle": style_cell},
dashGridOptions={
"rowHeight": 25,
"pagination": True,
"paginationPageSize": 5,
"icons": {
"columnGroupOpened": '<i class="fa-regular fa-square-check"></i>',
"columnGroupClosed": '<i class="fa-regular fa-square"></i>',
},
"domLayout": "autoHeight",
"tooltipShowDelay": 100,
"postSortRows": {"function": "postSort(params)"},
"enableAdvancedFilter": True
},
enableEnterpriseModules=True,
columnSize="sizeToFit",
style={"height": "100%", "width": "100%"},
defaultColDef={"editable": True, "filter": True, "floatingFilter": True, "filterParams": {"maxNumConditions": 6, "buttons": ["reset"], "closeOnApply": True}, "wrapText": True, "autoHeight": True, "cellStyle": {"wordBreak": "normal", "lineHeight": "unset"}},
)
app = Dash(__name__)
app.layout = html.Div([
html.Div(id='filter-expression-display'),
dmc.Space(h=30),
grid,
])
# Add a callback to display the filter expression
@app.callback(
Output('filter-expression-display', 'children'),
Input('ag-grid', 'filterModel') # this property needs to be replaced with something similar for advanced filters
)
def display_filter_expression(data):
print(data)
if data:
return f"Current Filter Expression: {data}"
return "No filters applied."
if __name__ == '__main__':
app.run_server(debug=True)