JS library allows you to select cells of HTML tables as well as cleaning, copying, cutting and pasting data of table.
Supported copy/cut/paste to spreadsheets such as Microsoft Excel, Google Docs, LibreOffice and OpenOffice.
See demo.
Include the js-files which you can find in the dist folder.
<script src="dist/tcs.bundle.min.js"></script>Install module using npm:
npm install js-table-cell-selector
or using yarn:
yarn add js-table-cell-selector
and import:
import TableCellSelector from "js-table-cell-selector";
var table = document.getElementById("tcs-table");
var options = {deselectOutTableClick: false, enableChanging: true};
var buffer = new TableCellSelector.Buffer();
var tcs = new TableCellSelector(table, options, buffer);
tcs.onStartSelect = function (event, cell) {
console.log("start select");
};
tcs.enableHotkeys = true;- Ctrl + A — select all
- Ctrl + C — copy
- Ctrl + V — paste
- Ctrl + X — cut
- Ctrl + (Backspace or Delete) — clear
Note: operations for the browser buffer only work when TableCellSelector.Buffer is initialized.
| Name | Type | Default | Description |
|---|---|---|---|
| deselectOutTableClick | Bool | true | Deselect when clicking is outside the table |
| enableChanging | Bool | false | The flag allows changing the table using key combinations |
| enableHotkeys | Bool | true | The flag of hotkeys enable |
| getCellFn | Function | function (cell, coord) { return cell.innerText; } | Getting cell value |
| ignoreClass | String | 'tcs-ignore' | You can add this class to a TR or TD |
| ignoreTfoot | Bool | false | Ignore tag TFOOT and its contents |
| ignoreThead | Bool | false | Ignore tag THEAD and its contents |
| mergePastingGlue | String | ' ' | The gluing string when pasted into merged cells |
| mouseBlockSelection | Bool | true | Must be disabled when editing cell contents when contenteditable is true for selection to work |
| selectClass | String | 'tcs-select' | Class added to the cell when selecting |
| selectIgnoreClass | Bool | true | Select ignored cells. The actions of changing them will not work anyway |
| setCellFn | Function | function (cell, data, coord) { cell.innerText = data; } | Setting cell value |
| tableClass | String | 'tcs' | Class added to the table when initializing |
The readout of positions is from the upper left corner of the table as [y, x]
Clear selected cell
@param c1 - starting position [0, 0]
@param c2 - end position [1, 1]
@returns {boolean}
Copy with browser buffer support
@param c1 - starting position [0, 0]
@param c2 - end position [1, 1]
@returns {array[][] | false}
Cut with browser buffer support
@param c1 - starting position [0, 0]
@param c2 - end position [1, 1]
@returns {array[][] | false}
Remove selection
@returns {number}
Get selection coordinates
@returns {array[][] | false}
initialize or re-initialize the size matrix
@param data - array[][]
@param c1 - starting position [0, 0]
@param c2 - end position [1, 1]
@param c1 - starting position [0, 0]
@param c2 - end position [1, 1]
@returns {boolean}
Select all cells
@returns {number}
Occurs at the starting of a selection
Occurs when a new cell is selected, starting from the second cell (triggered on each cell in the selection). The first is triggered onStartSelect
prevState - flag that is true if the cell has already been selected otherwise false
Occurs when deselecting a cell
Occurs at the finishing of a selection
Apache 2.0