Ag-Grid external filtering doesn't respond to state changes

依然范特西╮ 提交于 2021-01-28 11:37:09

问题


I've been struggling to get Ag-Grid's external filtering responding to state changes. When I update isExternalFiltered, the value of isExternalFiltered in the doesExternalFilterPass callback doesn't change. If I supply a key to the grid to force it to rerender each update, it seems to work, but it causes an expensive grid reinitialization.

I'll also note that I've tried calling the grid api's onFilterChanged method when the filter changes, but that doesn't seem to have any effect.

The docs don't seem to have any examples of external filtering with React (or any framework, for that matter), so I'm beginning to wonder if maybe it isn't supported?

    const Grid = () => {

        const [gridData, setGridData] = useState([]);
        const [columnDefs, setColumnDefs] = useState([]);
        const [isExternalFiltered, setExternalFiltered] = useState(false);

        /*
         Omitted for brevity
        */

        return (            
            <div className="ag-theme-material">
                <AgGridReact
                    // omitted for brevity
                    columnDefs={columnDefs}
                    rowData={gridData}
                    isExternalFilterPresent={() => true}
                    doesExternalFilterPass={node => {
                        return isExternalFiltered
                            ? filterFunction(node.data)
                            : true;
                    }}
                />
            </div>
        );
    };

回答1:


A bit late but this morning I ran into a similar issue and I'm going to post what I found to help people in the future with this same problem. This happens because ag-grid React never updates the callback for isExternalFilterPresent. I confirmed this by storing the callback in a variable, causing the grid to rerender, and then inspecting the callback for isExternalFilterPresent inside of the gridOptions gotten from the gridApi. These two callbacks are always the same which means that any values that your callback, isExternalFilterPresent, close over are never update. The way around this is to use a ref for any of the values that isExternalFilterPresent close over so you always access the most up to date value.



来源:https://stackoverflow.com/questions/56430096/ag-grid-external-filtering-doesnt-respond-to-state-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!