Class: Core

Core

handsontable/src/core.js, line 26

After Handsontable is constructed, you can modify the grid behavior using the available public methods.


How to call methods

These are 2 equal ways to call a Handsontable method:

// all following examples assume that you constructed Handsontable like this
var ht = new Handsontable(document.getElementById('example1'), options);

// now, to use setDataAtCell method, you can either:
ht.setDataAtCell(0, 0, 'new value');

Alternatively, you can call the method using jQuery wrapper (obsolete, requires initialization using our jQuery guide

  $('#example1').handsontable('setDataAtCell', 0, 0, 'new value');

Methods

handsontable/src/core.js, line 3031

addHook(key, callback)

Adds listener to the specified hook name (only for this Handsontable instance).

Parameters:
Name Type Description
key String

Hook name.

callback function | Array

Function or array of Functions.

See:
Example
hot.addHook('beforeInit', myCallback);
handsontable/src/core.js, line 3067

addHookOnce(key, callback)

Adds listener to specified hook name (only for this Handsontable instance).
After the listener is triggered, it will be automatically removed.

Parameters:
Name Type Description
key String

Hook name.

callback function | Array

Function or array of Functions.

See:
Example
hot.addHookOnce('beforeInit', myCallback);
handsontable/src/core.js, line 1775

alter(action, index, amount, source, keepEmptyRows)

Allows altering the table structure by either inserting/removing rows or inserting/removing columns:

Insert new row(s) above the row with a given index. If index is null or undefined, the new row will be
added after the last row.

var hot = new Handsontable(document.getElementById('example'));
hot.alter('insert_row', 10);

Insert new column(s) before the column with a given index. If index is null or undefined, the new column
will be added after the last column.

var hot = new Handsontable(document.getElementById('example'));
hot.alter('insert_col', 10);

Remove the row(s) at the given index.

var hot = new Handsontable(document.getElementById('example'));
hot.alter('remove_row', 10);

Remove the column(s) at the given index.

var hot = new Handsontable(document.getElementById('example'));
hot.alter('remove_col', 10);
Parameters:
Name Type Default Description
action String

See grid.alter for possible values: "insert_row", "insert_col", "remove_row", "remove_col"

index Number

Index of the row/column before which the new row/column will be inserted/removed.

amount Number 1 optional

Amound of rows/columns to be inserted/removed.

source String optional

Source indicator.

keepEmptyRows Boolean optional

Flag for preventing deletion of empty rows.

handsontable/src/core.js, line 1763

clear()

Clears the data from the grid. (The table settings remain intact.)

Since:
  • 0.11
handsontable/src/core.js, line 2673

colOffset(){Number}

Returns the index of the first rendered column.

Returns: {Number} Index of the first visible column.
handsontable/src/core.js, line 1845

colToProp(col){String|Number}

Returns the property name that corresponds with the given column index. DataMap#colToProp
If the data source is an array of arrays, it returns the columns index.

Parameters:
Name Type Description
col Number

Column index

Returns: {String|Number} Column property or column index.
handsontable/src/core.js, line 2611

countCols(){Number}

Returns the total number of columns in the grid.

Returns: {Number} Total number of columns.
handsontable/src/core.js, line 2758

countEmptyCols(ending){Number}

Returns the number of empty columns. If the optional ending parameter is true, returns the number of empty
columns at right hand edge of the table.

Parameters:
Name Type Description
ending Boolean optional

If true, will only count empty columns at the end of the data source row.

Returns: {Number} Count empty cols
handsontable/src/core.js, line 2728

countEmptyRows(ending){Number}

Returns the number of empty rows. If the optional ending parameter is true, returns the
number of empty rows at the bottom of the table.

Parameters:
Name Type Description
ending Boolean optional

If true, will only count empty rows at the end of the data source.

Fires:
Returns: {Number} Count empty rows
handsontable/src/core.js, line 2706

countRenderedCols(){Number}

Returns the number of rendered columns (including columns partially or fully rendered outside viewport).

Returns: {Number} Returns -1 if table is not visible.
handsontable/src/core.js, line 2684

countRenderedRows(){Number}

Returns the number of rendered rows (including rows partially or fully rendered outside viewport).

Returns: {Number} Returns -1 if table is not visible.
handsontable/src/core.js, line 2600

countRows(){Number}

Returns the total number of rows in the grid.

Returns: {Number} Total number in rows the grid.
handsontable/src/core.js, line 2578

countSourceCols(){Number}

Returns the total number of columns in the data source.

Since:
  • 0.26.1
Returns: {Number} Total number in columns in data source.
handsontable/src/core.js, line 2566

countSourceRows(){Number}

Returns the total number of rows in the data source.

Since:
  • 0.20.0
Returns: {Number} Total number in rows in data source.
handsontable/src/core.js, line 2717

countVisibleCols(){Number}

Returns the number of visible columns. Returns -1 if table is not visible

Returns: {Number} Number of visible columns or -1.
handsontable/src/core.js, line 2695

countVisibleRows(){Number}

Returns the number of visible rows (rendered rows that fully fit inside viewport).

Returns: {Number} Number of visible rows or -1.
handsontable/src/core.js, line 2885

deselectCell()

Deselects the current cell selection on grid.

handsontable/src/core.js, line 2935

destroy()

Removes grid from the DOM.

Fires:
handsontable/src/core.js, line 1250

destroyEditor(revertOriginal)

Destroys the current editor, renders and selects the current cell.

Parameters:
Name Type Description
revertOriginal Boolean optional

If != true, edited data is saved. Otherwise the previous value is restored.

handsontable/src/core.js, line 2996

getActiveEditor(){Object}

Returns the active editor object.

Returns: {Object} The active editor object.
handsontable/src/core.js, line 1817

getCell(row, col, topmost){Element}

Returns a TD element for the given row and col arguments, if it is rendered on screen.
Returns null if the TD is not rendered on screen (probably because that part of the table is not visible).

Parameters:
Name Type Description
row Number

Row index.

col Number

Column index.

topmost Boolean

If set to true, it returns the TD element from the topmost overlay. For example,
if the wanted cell is in the range of fixed rows, it will return a TD element from the top overlay.

Returns: {Element} The cell's TD element.
handsontable/src/core.js, line 2260

getCellEditor(row, col){Object}

Returns the cell editor by the provided row and col arguments.

Parameters:
Name Type Description
row Number

Row index.

col Number

Column index.

Returns: {Object} The Editor object.
handsontable/src/core.js, line 2146

getCellMeta(row, col){Object}

Returns the cell properties object for the given row and col coordinates.

Parameters:
Name Type Description
row Number

Row index.

col Number

Column index.

Fires:
Returns: {Object} The cell properties object.
handsontable/src/core.js, line 2244

getCellRenderer(row, col){function}

Returns the cell renderer function by given row and col arguments.

Parameters:
Name Type Description
row Number | Object

Row index or cell meta object.

col Number optional

Column index.

Since:
  • 0.11
Returns: {function} The renderer function.
handsontable/src/core.js, line 2271

getCellValidator(row, col){function|undefined}

Returns the cell validator by row and col, provided a validator is defined. If not - it doesn't return anything.

Parameters:
Name Type Description
row Number

Row index.

col Number

Column index.

Returns: {function|undefined} The validator function.
handsontable/src/core.js, line 2391

getColHeader(col){Array|String}

Returns an array of column headers (in string format, if they are enabled). If param col is given, it returns the header at the given column as a string.

Parameters:
Name Type Description
col Number optional

Column index.

Fires:
Returns: {Array|String} The column header(s).
handsontable/src/core.js, line 2490

getColWidth(col){Number}

Returns the width of the requested column.

Parameters:
Name Type Description
col Number

Column index.

Since:
  • 0.11
Fires:
Returns: {Number} Column width.
handsontable/src/core.js, line 1833

getCoords(elem){WalkontableCellCoords}

Returns the coordinates of the cell, provided as a HTML Element.

Parameters:
Name Type Description
elem Element

The HTML Element representing the cell.

Returns: {WalkontableCellCoords} Coordinates object.
handsontable/src/core.js, line 1486

getCopyableData(row, column){String}

Returns the data's copyable value at specified row and column index (DataMap#getCopyable).

Parameters:
Name Type Description
row Number

Row index.

column Number

Column index.

Since:
  • 0.19.0
Returns: {String}
handsontable/src/core.js, line 1469

getCopyableText(startRow, startCol, endRow, endCol){String}

Returns a string value of the selected range. Each column is separated by tab, each row is separated by a new line character.
DataMap#getCopyableText

Parameters:
Name Type Description
startRow Number

From row.

startCol Number

From column.

endRow Number

To row.

endCol Number

To column.

Since:
  • 0.11
Returns: {String}
handsontable/src/core.js, line 1445

getData(r, c, r2, c2){Array}

Returns the current data object (the same one that was passed by data configuration option or loadData method,
unless the modifyRow hook was used to trim some of the rows. If that's the case - use the Core#getSourceData method.).
Optionally you can provide cell range by defining row, col, row2, col2 to get only a fragment of grid data.

Note: getData functionality changed with the release of version 0.20. If you're looking for the previous functionality,
you should use the Core#getSourceData method.

Parameters:
Name Type Description
r Number optional

From row.

c Number optional

From column.

r2 Number optional

To row.

c2 Number optional

To column.

Returns: {Array} Array with the data.
handsontable/src/core.js, line 1870

getDataAtCell(row, col){String|Boolean|null}

Returns the cell value at row, col. row and col are the visible indexes (note, that if columns were reordered or sorted,
the currently visible order will be used).

Parameters:
Name Type Description
row Number

Row index.

col Number

Column index.

Returns: {String|Boolean|null} Data at cell.
handsontable/src/core.js, line 1898

getDataAtCol(col){Array}

Returns array of column values from the data source. col is the visible index of the column.
Note, that if columns were reordered or sorted, the currently visible order will be used.

Parameters:
Name Type Description
col Number

Column index.

Since:
  • 0.9-beta2
Returns: {Array} Array of cell values.
handsontable/src/core.js, line 1915

getDataAtProp(prop){Array}

Given the object property name (e.g. 'first.name'), returns an array of column's values from the data source.
You can also provide a column index as the first argument.

Parameters:
Name Type Description
prop String | Number

Property name / column index.

Since:
  • 0.9-beta2
Returns: {Array} Array of cell values.
handsontable/src/core.js, line 2002

getDataAtRow(row){Array}

Returns a single row of the data. The row argument is the visible index of the row.

Parameters:
Name Type Description
row Number

Row index.

Since:
  • 0.9-beta2
Returns: {Array} Array of row's cell data.
handsontable/src/core.js, line 1885

getDataAtRowProp(row, prop){*}

Return value at row, prop. (Uses DataMap#get)

Parameters:
Name Type Description
row Number

Row index.

prop String

Property name.

Returns: {*} Cell value.
handsontable/src/core.js, line 2018

getDataType(rowFrom, columnFrom, rowTo, columnTo){String}

Returns a data type defined in the Handsontable settings under the type key (Options#type).
If there are cells with different types in the selected range, it returns 'mixed'.

Parameters:
Name Type Description
rowFrom Number

From row index.

columnFrom Number

To row index.

rowTo Number

From column index.

columnTo Number

To column index.

Since:
  • 0.18.1
Returns: {String} Cell type (e.q: 'mixed', 'text', 'numeric', 'autocomplete').
handsontable/src/core.js, line 3020

getInstance(){Handsontable}

Returns the Handsontable instance.

Returns: {Handsontable} The Handsontable instance.
handsontable/src/core.js, line 3007

getPlugin(pluginName){*}

Returns plugin instance using the plugin name provided.

Parameters:
Name Type Description
pluginName String

The plugin name.

Since:
  • 0.15.0
Returns: {*} The plugin instance.
handsontable/src/core.js, line 2324

getRowHeader(row){Array|String}

Returns an array of row headers' values (if they are enabled). If param row was given, it returns the header of the given row as a string.

Parameters:
Name Type Description
row Number optional

Row index.

Fires:
Returns: {Array|String} Array of header values / single header value.
handsontable/src/core.js, line 2548

getRowHeight(row){Number}

Returns the row height.

Parameters:
Name Type Description
row Number

Row index.

Since:
  • 0.11
Fires:
Returns: {Number} The given row's height.
handsontable/src/core.js, line 1500

getSchema(){Object}

Returns schema provided by constructor settings. If it doesn't exist then it returns the schema based on the data
structure in the first row.

Since:
  • 0.13.2
Returns: {Object} Schema object.
handsontable/src/core.js, line 1335

getSelected(){Array}

Returns indexes of the currently selected cells as an array [startRow, startCol, endRow, endCol].

Start row and start col are the coordinates of the active cell (where the selection was started).

Returns: {Array} Array of the selection's indexes.
handsontable/src/core.js, line 1350

getSelectedRange(){WalkontableCellRange}

Returns the current selection as a WalkontableCellRange object.

Since:
  • 0.11
Returns: {WalkontableCellRange} Selected range object or undefined` if there is no selection.
handsontable/src/core.js, line 1752

getSettings(){Object}

Returns the object settings.

Returns: {Object} Object containing the current grid settings.
handsontable/src/core.js, line 1937

getSourceData(r, c, r2, c2){Array}

Returns the source data object (the same that was passed by data configuration option or loadData method).
Optionally you can provide a cell range by using the row, col, row2, col2 arguments, to get only a fragment of grid data.

Parameters:
Name Type Description
r Number optional

From row.

c Number optional

From column.

r2 Number optional

To row.

c2 Number optional

To column.

Since:
  • 0.20.0
Returns: {Array} Array of grid data.
handsontable/src/core.js, line 1988

getSourceDataAtCell(row, column){*}

Returns a single value from the data source.

Parameters:
Name Type Description
row Number

Row index.

column Number

Column index.

Since:
  • 0.20.0
Returns: {*} Cell data.
handsontable/src/core.js, line 1962

getSourceDataAtCol(column){Array}

Returns an array of column values from the data source. col is the index of the row in the data source.

Parameters:
Name Type Description
column Number

Column index.

Since:
  • 0.11.0-beta3
Returns: {Array} Array of the column's cell values.
handsontable/src/core.js, line 1975

getSourceDataAtRow(row){Array|Object}

Returns a single row of the data (array or object, depending on what you have). row is the index of the row in the data source.

Parameters:
Name Type Description
row Number

Row index.

Since:
  • 0.11.0-beta3
Returns: {Array|Object} Single row of data.
handsontable/src/core.js, line 1703

getValue(){*}

Get value from the selected cell.

Since:
  • 0.11
Returns: {*} Value of selected cell.
handsontable/src/core.js, line 2370

hasColHeaders(){Boolean}

Returns information about if this table is configured to display column headers.

Since:
  • 0.11
Returns: {Boolean} True if the instance has the column headers enabled, false otherwise.
handsontable/src/core.js, line 3049

hasHook(key){Boolean}

Check if for a specified hook name there are added listeners (only for this Handsontable instance).

Parameters:
Name Type Description
key String

Hook name

See:
Returns: {Boolean}
Example
var hasBeforeInitListeners = hot.hasHook('beforeInit');
handsontable/src/core.js, line 2358

hasRowHeaders(){Boolean}

Returns information about if this table is configured to display row headers.

Since:
  • 0.11
Returns: {Boolean} true if the instance has the row headers enabled, false otherwise.
handsontable/src/core.js, line 2798

isEmptyCol(col){Boolean}

Check if all cells in the the column declared by the col argument are empty.

Parameters:
Name Type Description
col Number

Column index.

Returns: {Boolean} true if the column at the given col is empty, false otherwise.
handsontable/src/core.js, line 2786

isEmptyRow(row){Boolean}

Check if all cells in the row declared by the row argument are empty.

Parameters:
Name Type Description
row Number

Row index.

Returns: {Boolean} true if the row at the given row is empty, false otherwise.
handsontable/src/core.js, line 1238

isListening(){Boolean}

Returns true if the current Handsontable instance is listening to keyboard input on document body.

Since:
  • 0.11
Returns: {Boolean} true if the instance is listening, false otherwise.
handsontable/src/core.js, line 1216

listen()

Listen to the keyboard input on document body.

Since:
  • 0.11
handsontable/src/core.js, line 1378

loadData(data)

Reset all cells in the grid to contain data from the data array.

Parameters:
Name Type Description
data Array

Array of arrays or array of objects containing data.

Fires:
handsontable/src/core.js, line 1261

populateFromArray(row, col, input, endRow, endCol, source, method, direction, deltas){Object|undefined}

Populate cells at position with 2D input array (e.g. [[1, 2], [3, 4]]).
Use endRow, endCol when you want to cut input when a certain row is reached.
Optional source parameter (default value "populateFromArray") is used to identify this call in the resulting events (beforeChange, afterChange).
Optional populateMethod parameter (default value "overwrite", possible values "shift_down" and "shift_right")
has the same effect as pasteMode option Options#pasteMode

Parameters:
Name Type Default Description
row Number

Start row

col Number

Start column

input Array

2d array

endRow Number optional

End row (use when you want to cut input when certain row is reached)

endCol Number optional

End column (use when you want to cut input when certain column is reached)

source String "populateFromArray" optional

Source string.

method String "overwrite" optional

Populate method. Possible options: shift_down, shift_right, overwrite.

direction String

Populate direction. (left|right|up|down)

deltas Array

Deltas array.

Since:
  • 0.9.0
Returns: {Object|undefined} The ending TD element in pasted area (only if any cells were changed).
handsontable/src/core.js, line 1858

propToCol(prop){Number}

Returns column index that corresponds with the given property. DataMap#propToCol

Parameters:
Name Type Description
prop String

Property name.

Returns: {Number} Column index.
handsontable/src/core.js, line 2074

removeCellMeta(row, col, key)

Remove a property defined by the key argument from the cell meta object for the provided row and col coordinates.

Parameters:
Name Type Description
row Number

Row index.

col Number

Column index.

key String

Property name.

handsontable/src/core.js, line 3086

removeHook(key, callback)

Removes the hook listener previously registered with Core#addHook.

Parameters:
Name Type Description
key String

Hook name.

callback function

Function which have been registered via Core#addHook.

See:
Example
hot.removeHook('beforeInit', myCallback);
handsontable/src/core.js, line 1364

render()

Rerender the table.

handsontable/src/core.js, line 2662

rowOffset(){Number}

Returns an index of the first rendered row.

Returns: {Number} Index of first rendered row.
handsontable/src/core.js, line 3104

runHooks(key, p1, p2, p3, p4, p5, p6){*}

Run the callbacks for the hook provided in the key argument using the parameters given in the other arguments.

Parameters:
Name Type Description
key String

Hook name.

p1 * optional

Argument passed to the callback.

p2 * optional

Argument passed to the callback.

p3 * optional

Argument passed to the callback.

p4 * optional

Argument passed to the callback.

p5 * optional

Argument passed to the callback.

p6 * optional

Argument passed to the callback.

See:
Returns: {*}
Example
hot.runHooks('beforeInit');
handsontable/src/core.js, line 2895

scrollViewportTo(row, column){Boolean}

Scroll viewport to coords specified by the row and column arguments.

Parameters:
Name Type Description
row Number | null

Row index.

column Number | null

Column index.

Since:
  • 0.24.3
Returns: {Boolean} true if scroll was successful, false otherwise.
handsontable/src/core.js, line 2810

selectCell(row, col, endRow, endCol, scrollToCell, changeListener){Boolean}

Select cell specified by row and col values or a range of cells finishing at endRow, endCol.
By default, viewport will be scrolled to selection.
After the selectCell method had finished, the instance will be listening to keyboard input on the document.

Parameters:
Name Type Default Description
row Number

Row index.

col Number

Column index.

endRow Number optional

End row index (if selecting a range).

endCol Number optional

End column index (if selecting a range).

scrollToCell Boolean true optional

If true, the viewport will be scrolled to the selection.

changeListener Boolean true optional

If false, Handsontable will not change keyboard events listener to himself.

Returns: {Boolean} true if selection was successful, false otherwise.
handsontable/src/core.js, line 2862

selectCellByProp(row, prop, endRow, endProp, scrollToCell){Boolean}

Select the cell specified by the row and prop arguments, or a range finishing at endRow, endProp.
By default, viewport will be scrolled to selection.

Parameters:
Name Type Default Description
row Number

Row index.

prop String

Property name.

endRow Number optional

End row index (if selecting a range).

endProp String optional

End property name (if selecting a range).

scrollToCell Boolean true optional

If true, viewport will be scrolled to the selection.

Returns: {Boolean} true if selection was successful, false otherwise.
handsontable/src/core.js, line 2113

setCellMeta(row, col, key, val)

Sets a property defined by the key object to the meta object of a cell corresponding to params row and col.

Parameters:
Name Type Description
row Number

Row index.

col Number

Column index.

key String

Property name.

val String

Property value.

Since:
  • 0.11
Fires:
handsontable/src/core.js, line 2092

setCellMetaObject(row, col, prop)

Set cell meta data object defined by prop to the corresponding params row and col.

Parameters:
Name Type Description
row Number

Row index.

col Number

Column index.

prop Object

Meta object.

Since:
  • 0.11
handsontable/src/core.js, line 1132

setDataAtCell(row, col, value, source)

Set new value to a cell. To change many cells at once, pass an array of changes in format [[row, col, value], ...] as
the only parameter. col is the index of a visible column (note that if columns were reordered,
the current visible order will be used). source is a flag for before/afterChange events. If you pass only array of
changes then source could be set as second parameter.

Parameters:
Name Type Description
row Number | Array

Row index or array of changes in format [[row, col, value], ...].

col Number

Column index.

value String

New value.

source String optional

String that identifies how this change will be described in the changes array (useful in onChange callback).

handsontable/src/core.js, line 1179

setDataAtRowProp(row, prop, value, source)

Set new value to a cell. To change many cells at once, pass an array of changes in format [[row, prop, value], ...] as
the only parameter. prop is the name of the object property (e.g. first.name). source is a flag for before/afterChange events.
If you pass only array of changes then source could be set as second parameter.

Parameters:
Name Type Description
row Number | Array

Row index or array of changes in format [[row, prop, value], ...].

prop String

Property name or the source string.

value String

Value to be set.

source String optional

String that identifies how this change will be described in changes array (useful in onChange callback).

handsontable/src/core.js, line 1293

spliceCol(col, index, amount, elements)

Adds/removes data from the column. This function is modelled after Array.splice.
Parameter col is the index of the column in which do you want to do splice.
Parameter index is the row index at which to start changing the array.
If negative, will begin that many elements from the end. Parameter amount, is the number of the old array elements to remove.
If the amount is 0, no elements are removed. Fourth and further parameters are the elements to add to the array.
If you don't specify any elements, spliceCol simply removes elements from the array.
DataMap#spliceCol

Parameters:
Name Type Description
col Number

Index of the column in which do you want to do splice.

index Number

Index at which to start changing the array. If negative, will begin that many elements from the end.

amount Number

An integer indicating the number of old array elements to remove. If amount is 0, no elements are removed.

elements * optional

The elements to add to the array. If you don't specify any elements, spliceCol simply removes elements from the array.

Since:
  • 0.9-beta2
handsontable/src/core.js, line 1314

spliceRow(row, index, amount, elements)

Adds/removes data from the row. This function works is modelled after Array.splice.
Parameter row is the index of row in which do you want to do splice.
Parameter index is the column index at which to start changing the array.
If negative, will begin that many elements from the end. Parameter amount, is the number of old array elements to remove.
If the amount is 0, no elements are removed. Fourth and further parameters are the elements to add to the array.
If you don't specify any elements, spliceCol simply removes elements from the array.
DataMap#spliceRow

Parameters:
Name Type Description
row Number

Index of column in which do you want to do splice.

index Number

Index at which to start changing the array. If negative, will begin that many elements from the end.

amount Number

An integer indicating the number of old array elements to remove. If amount is 0, no elements are removed.

elements * optional

The elements to add to the array. If you don't specify any elements, spliceCol simply removes elements from the array.

Since:
  • 0.11
handsontable/src/core.js, line 1227

unlisten()

Stop listening to keyboard input on the document body.

Since:
  • 0.11
handsontable/src/core.js, line 1513

updateSettings(settings, init)

Use it if you need to change configuration after initialization. The settings parameter is an object containing the new
settings, declared the same way as in the initial settings object.
Note, that although the updateSettings method doesn't overwrite the previously declared settings, it might reset
the settings made post-initialization. (for example - ignore changes made using the columnResize feature).

Parameters:
Name Type Description
settings Object

New settings object.

init Boolean
Fires:
Example
hot.updateSettings({
   contextMenu: true,
   colHeaders: true,
   fixedRowsTop: 2
});
handsontable/src/core.js, line 2282

validateCells(callback)

Validates all cells using their validator functions and calls callback when finished.

If one of the cells is invalid, the callback will be fired with 'valid' arguments as false - otherwise it would equal true.

Parameters:
Name Type Description
callback function optional

The callback function.