Hi @nsledford
Just cross posting from the solution in the GitHub issue. See the full discussion and another example:
opened 05:06PM - 04 Nov 25 UTC
Hey,
I've been trying to create a custom filter with autocomplete. I have my fu… nction stored in /assets/dashAgGridComponentFunctions.js.
My dash app is able to find my custom react component. However, when I try to filter, I always get this error:
react-dom@16.v2_15_0…2638.14.0.min.js:32 Uncaught TypeError: Cannot read properties of undefined (reading 'doesFilterPass')
I've tried defining it in my function. I believe it comes from using the onModelChange callback from ag grid.
I'm defining it like this:
var dagcomponentfuncs = (window.dashAgGridComponentFunctions = window.dashAgGridComponentFunctions || {});
dagcomponentfuncs.customAutocompleteFilter = React.forwardRef((props, ref) => {
... my code ...
}
I've also tried defining it as a function, and not just a forwardRef.
Thanks in advance!
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