Hi @nsledford
Just cross posting from the solution in the GitHub issue. See the full discussion and another example:
Note this is for AG Grid version 32. For anyone upgrading from V31, you can use the custom component defined for V31 by setting dashGridOptions={"reactiveCustomComponents": False}
from dash import Dash, html
import dash_ag_grid as dag
import pandas as pd
app = Dash(__name__)
df = pd.read_csv(
"https://raw.githubusercontent.com/plotly/datasets/master/ag-grid/olympic-winners.csv"
)
rowData = df.to_dict('records')
columnDefs = [
{ 'field': 'age', 'filter': 'agNumberColumnFilter' },
{ 'field': 'country', 'minWidth': 150 },
{ 'field': 'year', 'filter': {'function': 'YearFilter'}},
{
'field': 'date',
'minWidth': 130,
'filter': 'agDateColumnFilter',
'filterParams': {
'comparator': {'function':'dateComparator'},
},
},
{ 'field': 'sport' },
{ 'field': 'gold', 'filter': 'agNumberColumnFilter' },
{ 'field': 'silver', 'filter': 'agNumberColumnFilter' },
{ 'field': 'bronze', 'filter': 'agNumberColumnFilter' },
{ 'field': 'total', 'filter': 'agNumberColumnFilter' },
]
defaultColDef = {
'editable': True,
'sortable': True,
'flex': 1,
'minWidth': 100,
'filter': True,
'resizable': True,
}
app.layout = html.Div(
[
html.H3("See the custom filter component in the Year column"),
dag.AgGrid(
id="grid",
columnDefs=columnDefs,
rowData=rowData,
defaultColDef=defaultColDef,
),
]
)
if __name__ == "__main__":
app.run(host="127.0.0.1", debug=True)
var dagfuncs = window.dashAgGridFunctions = window.dashAgGridFunctions || {};
const {useImperativeHandle, useState, useEffect, forwardRef} = React;
dagfuncs.YearFilter = forwardRef((props, ref) => {
const [year, setYear] = useState('All');
dash_ag_grid.useGridFilter({
doesFilterPass(params) {
return params.data.year >= 2010;
},
});
useEffect(() => {
props.onModelChange(year === "All" ? null : year)
}, [year]);
setProps = ({value}) => {
if (value) {
setYear(value)
}
}
return React.createElement(
window.dash_core_components.RadioItems,
{
options: [
{'label': 'All', 'value': 'All'},
{'label': 'Since 2010', 'value': '2010'},
],
value: year,
setProps
}
)
});
See the two examples on PyCafe:
And here is an example with the custom Autocomplete component in the filter