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.
Selecting 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{ useRef, useEffect, useState }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);const[output, setOutput]=useState('');let getButtonClickCallback;useEffect(()=>{const hot = hotRef.current.hotInstance;getButtonClickCallback=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));}setOutput(JSON.stringify(data));};});return(<><HotTableref={hotRef}data={Handsontable.helper.createSpreadsheetData(10,10)}width="auto"height="auto"colWidths={100}rowHeights={23}rowHeaders={true}colHeaders={true}outsideClickDeselects={false}selectionMode="multiple"// 'single', 'range' or 'multiple',licenseKey="non-commercial-and-evaluation"/><outputclassName="console"id="output">{output}</output><divclassName="controls"><buttonid="getButton"onClick={(...args)=>getButtonClickCallback(...args)}>
Get data
</button></div></>);};
ReactDOM.render(<ExampleComponent/>, document.getElementById('example2'));
Modifying 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.
import{ useRef, useEffect }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 buttonClickCallback;useEffect(()=>{const hot = hotRef.current.hotInstance;buttonClickCallback=event=>{const selected = hot.getSelected()||[];const target = event.target.id;
hot.suspendRender();for(let index =0; index < selected.length; index +=1){const[row1, column1, row2, column2]= selected[index];const startRow = Math.max(Math.min(row1, row2),0);const endRow = Math.max(row1, row2);const startCol = Math.max(Math.min(column1, column2),0);const endCol = Math.max(column1, column2);for(let rowIndex = startRow; rowIndex <= endRow; rowIndex +=1){for(let columnIndex = startCol; columnIndex <= endCol; columnIndex +=1){
hot.setDataAtCell(rowIndex, columnIndex,'data changed');
hot.setCellMeta(rowIndex, columnIndex,'className','c-red');}}}
hot.render();
hot.resumeRender();};});return(<><HotTableref={hotRef}data={Handsontable.helper.createSpreadsheetData(10,10)}width="auto"height={272}colWidths={100}rowHeights={23}rowHeaders={true}colHeaders={true}outsideClickDeselects={false}selectionMode="multiple"// 'single', 'range' or 'multiple',licenseKey="non-commercial-and-evaluation"/><divclassName="controls"><buttonid="set-data-action"onClick={(...args)=>buttonClickCallback(...args)}>Change selected data and change the CSS class of the cell</button></div></>);};
ReactDOM.render(<ExampleComponent/>, document.getElementById('example3'));
.c-red{color: red;}
Styling the selection area
The background color can be easily changed using CSS styles. The main, light blue background color is defined in the .area class.
For non-adjacent selection, multiple classes are making each level a bit darker. These classes are called area-1, area-2, etc.
Unfortunately, there is no easy way to change the border color of the selection.
Jumping across the grid's edges
When you use keyboard navigation, and you cross an edge of the grid, you can set cell selection to jump to the opposite edge.
Jumping across vertical edges
To enable jumping across the left and right edges: