The Filters plugin allows filtering the data in the table's columns using a range of pre-defined conditions.
Basic configuration
To enable the plugin, set the filters configuration option to true and enable the filters dependency, which is the dropdownMenu plugin.
const container = document.querySelector('#example1');const hot =newHandsontable(container,{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]],columns:[{type:'text'},{type:'text'},{type:'text'},{type:'text'},{type:'date',dateFormat:'M/D/YYYY'},{type:'numeric'}],colHeaders:true,rowHeaders:true,dropdownMenu:true,filters:true,height:'auto',licenseKey:'non-commercial-and-evaluation'});
Custom filter menu
To display filters while hiding the other elements in the dropdown menu, pass the elements to be displayed as an array into the configuration.
const container = document.querySelector('#example2');const hot =newHandsontable(container,{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]],columns:[{type:'text'},{type:'text'},{type:'text'},{type:'text'},{type:'date',dateFormat:'M/D/YYYY'},{type:'numeric'}],colHeaders:true,rowHeaders:true,filters:true,dropdownMenu:['filter_by_condition','filter_action_bar'],height:'auto',licenseKey:'non-commercial-and-evaluation'});
Custom implementations
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.
// Event for `keydown` event. Add condition after delay of 200 ms which is counted from the time of last pressed key.const debounceFn = Handsontable.helper.debounce((colIndex, event)=>{const filtersPlugin = hot.getPlugin('filters');
filtersPlugin.removeConditions(colIndex);
filtersPlugin.addCondition(colIndex,'contains',[event.target.value]);
filtersPlugin.filter();},100);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));}};const container = document.querySelector('#example3');const hot =newHandsontable(container,{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(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.