The examples below show how to adjust the Filter plugin to your needs. They include customizing the UI components, changing the default behavior, and using filters outside the table.
Filter as you type
In this example, a basic input element has been placed inside a column’s header (A, B, C…). It is placed right below the column's label and is separated with a horizontal line for better visibility. The data is being filtered as you type - with a 100 ms delay. The filter element has been excluded from the selection event, so the column doesn’t get selected when clicked on.
Please note that this demo uses a Handsontable API to a great extent.
import{ useEffect, useRef }from'react';import Handsontable from'handsontable';import ReactDOM from'react-dom';import{ HotTable }from'@handsontable/react';import{ registerAllModules }from'handsontable/registry';import'handsontable/dist/handsontable.full.min.css';// register Handsontable's modulesregisterAllModules();constExampleComponent=()=>{const hotRef =useRef(null);let debounceFn =null;constaddEventListeners=(input, colIndex)=>{
input.addEventListener('keydown',event=>{debounceFn(colIndex, event);});};// Build elements which will be displayed in header.constgetInitializedElements=colIndex=>{const div = document.createElement('div');const input = document.createElement('input');
div.className ='filterHeader';addEventListeners(input, colIndex);
div.appendChild(input);return div;};// Add elements to header on `afterGetColHeader` hook.constaddInput=(col,TH)=>{// Hooks can return a value other than number (for example `columnSorting` plugin uses this).if(typeof col !=='number'){return col;}if(col >=0&&TH.childElementCount <2){TH.appendChild(getInitializedElements(col));}};useEffect(()=>{const hot = hotRef.current.hotInstance;// Event for `keydown` event. Add condition after delay of 200 ms which is counted from the time of last pressed key.
debounceFn = Handsontable.helper.debounce((colIndex, event)=>{const filtersPlugin = hot.getPlugin('filters');
filtersPlugin.removeConditions(colIndex);
filtersPlugin.addCondition(colIndex,'contains',[event.target.value]);
filtersPlugin.filter();},100);});return(<HotTableref={hotRef}data={[['Lorem','ipsum','dolor','sit','12/1/2015',23],['adipiscing','elit','Ut','imperdiet','5/12/2015',6],['Pellentesque','vulputate','leo','semper','10/23/2015',26],['diam','et','malesuada','libero','12/1/2014',98],['orci','et','dignissim','hendrerit','12/1/2016',8.5]]}height="auto"colHeaders={true}rowHeaders={true}className="as-you-type-demo"filters={true}colWidths={100}afterGetColHeader={addInput}beforeOnCellMouseDown={function(event, coords){// Deselect the column after clicking on input.if(coords.row ===-1&& event.target.nodeName ==='INPUT'){
event.stopImmediatePropagation();this.deselectCell();}}}licenseKey="non-commercial-and-evaluation"/>);};
ReactDOM.render(<ExampleComponent/>, document.getElementById('example3'));
Filter from the outside the table
The external Filter component is controlling the main table by passing values for particular columns. Only a fraction of the code is related to Handsontable API, for example, addConditionsByValue, filter, and removeConditions.
TIP
Note that selecting a column in the Filter component resets the state of the table. This implementation can filter only one column at a time.