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
This example places a basic input element inside each column header (A, B, C), separated by a horizontal line. The data is being filtered as you type, with a 100 ms delay. The filter element is excluded from the selection event, so the column doesn’t get selected when clicked on.
The demo below is just a demonstration (e.g., you can't add more columns). We don't recommend using it in your production code.
import{ useEffect, useRef }from'react';import{ HotTable }from'@handsontable/react';import{ registerAllModules }from'handsontable/registry';import'handsontable/dist/handsontable.full.min.css';// register Handsontable's modulesregisterAllModules();functiondebounce(func, wait =200){let lastTimer =null;let result;function_debounce(...args){if(lastTimer){clearTimeout(lastTimer);}
lastTimer =setTimeout(()=>{
result =func.apply(this, args);}, wait);return result;}return _debounce;}exportconstExampleComponent=()=>{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 =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"/>);};
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.