Selection enables you to select a single cell or ranges of cells within Handsontable. Once selected, you can retrieve data from the cell, edit the cell's contents, or change the style of the cell.
Basic configuration
With this feature, you can select single cells or ranges of cells across a grid. Easily retrieve the coordinates of the selected cells to clear or change the cells' content.
Use Cmd on Mac or Ctrl on Windows to select non-adjacent ranges of cells.
Select ranges
There are different modes in which you can use this plugin. Choose between selecting a single cell, a range of adjacent cells, and multiple non-adjacent ranges of cells.
import Handsontable from'handsontable';import'handsontable/dist/handsontable.full.min.css';const output = document.querySelector('#output');const getButton = document.querySelector('#getButton');const container = document.querySelector('#example2');const hot =newHandsontable(container,{data:[['A1','B1','C1','D1','E1','F1','G1','H1','I1'],['A2','B2','C2','D2','E2','F2','G2','H2','I2'],['A3','B3','C3','D3','E3','F3','G3','H3','I3'],['A4','B4','C4','D4','E4','F4','G4','H4','I4'],['A5','B5','C5','D5','E5','F5','G5','H5','I5'],['A6','B6','C6','D6','E6','F6','G6','H6','I6'],['A7','B7','C7','D7','E7','F7','G7','H7','I7'],['A8','B8','C8','D8','E8','F8','G8','H8','I8'],['A9','B9','C9','D9','E9','F9','G9','H9','I9'],],width:'auto',height:'auto',colWidths:100,rowHeights:23,rowHeaders:true,colHeaders:true,outsideClickDeselects:false,selectionMode:'multiple',// 'single', 'range' or 'multiple',autoWrapRow:true,autoWrapCol:true,licenseKey:'non-commercial-and-evaluation'});
getButton.addEventListener('click',event=>{const selected = hot.getSelected()||[];const data =[];for(let i =0; i < selected.length; i +=1){const item = selected[i];
data.push(hot.getData(...item));}
output.innerText =JSON.stringify(data);});
<divid="example2"></div><outputclass="console"id="output">Here you will see the log</output><divclass="controls"><buttonid="getButton">Get data</button></div>
Modify the selected cells
You may want to delete, format, or otherwise change the selected cells. For example, you can change a value or add CSS classes to the selected cells using the demo below.