Methods - nandaabiz/jquery-handsontable-old-0.10 GitHub Wiki

NOTICE: This document can be out of date. Since Handsontable 0.15.0 documentation was moved to docs.handsontable.com.

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 exampleContainer1 = document.getElementById('example1');
var ht = new Handsontable(exampleContainer1, options);

//now, to use setDataAtCell method, you can call method on the instance returned by the constructor (RECOMMENDED)
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

This is the list of available public methods, grouped by their functionality:

General methods

updateSettings (options: Object)
Use it if you need to change configuration after initialization
getSettings ()
Returns an object containing the current grid settings
loadData (data: Array)
Reset all cells in the grid to contain data from the data array
listen ()
Listen to keyboard input on document body
version added: 0.11
getCellRenderer (row: Number, col: Number)
Returns renderer type
version added: 0.11
unlisten ()
Stop listening to keyboard input on document body
version added: 0.11
isListening ()
Returns true if current Handsontable instance is listening to keyboard input on document body
version added: 0.11
render ()
Rerender the table
destroy ()
Remove grid from DOM
validateCells (callback: Function)
Validates all cells using their validator functions and calls callback when finished. Does not render the view.

Get data methods

getData ()
getData (row: Number, col: Number, row2: Number, col2: Number)
Return the current data object (the same that was passed by data configuration option or loadData method). Optionally you can provide cell range row, col, row2, col2 to get only a fragment of grid data
getDataAtCell (row: Number, col: Number)
Return cell value at row, col. row and col are the visible indexes (note that if columns were reordered or sorted, the current order will be used)
getDataAtRowProp (row: Number, prop: String)
Same as above, except instead of col, you provide name of the object property (e.g. 'first.name')
getDataAtRow (row: Number)
Returns a single row of the data (array or object, depending on what you have). row is the visible index of the row
version added: 0.9-beta2
getSourceDataAtRow (row: Number)
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
version added: 0.11.0-beta3
getDataAtCol (col: Number)
Returns array of column values from the data source. col is the visible index of the column
version added: 0.9-beta2
getSourceDataAtCol (col: Number)
Returns array of column values from the data source. col is the index of the row in the data source
version added: 0.11.0-beta3
getDataAtProp (prop: String)
Given the object property name (e.g. 'first.name'), returns array of column values from the data source.
version added: 0.9-beta2
getCopyableData (startRow: Number, startCol: Number, endRow: Number, endCol: Number)
Get value of selected range. Each column is separated by tab, each row is separated by new line character
version added: 0.11
<dt>getValue ()</dt>
<dd>Returns value of selected cell
<br><small>version added: 0.11</small>
</dd>

Set data methods

setDataAtCell (row: Number, col: Number, value: Mixed, source: String (Optional))
setDataAtCell (changes: Array, source: String (Optional))
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 visible column (note that if columns were reordered, the current 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.
setDataAtRowProp (row: Number, prop: String, value: Mixed, source: String (Optional))
setDataAtRowProp (changes: Array, source: String (Optional))
Same as above, except instead of col, you provide name of the object property (e.g. [0, 'first.name', 'Jennifer']).
populateFromArray (row: Number, col: Number, input: Array, endRow: Number, endCol: Number, source: String (Optional), populateMethod: String (Optional))
Populate cells at position with 2D input array (e.g. [ [1, 2], [3, 4] ]).
Use endRow, endCol when you want to cut input when 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 pasteMethod option (see Options page)
version added: 0.9.0
spliceCol (col: Number, index: Number, amount: Number, element1: Mixed, ..., elementN: Mixed)
Adds/removes data from the column. This function works is modelled after Array.splice. Parameter col is the index of 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 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.
version added: 0.9-beta2
spliceRow (row: Number, index: Number, amount: Number, element1: Mixed, ..., elementN: Mixed)
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.
version added: 0.11

Alter grid (create, remove rows and columns)

alter ('insert_row', index: Number, amount: Number (Optional), source: String (Optional))
Insert new row(s) above the row at given index. If index is null or undefined, the new row will be added after the current last row. Default amount equals 1
alter ('insert_col', index: Number, amount: Number (Optional), source: String (Optional))
Insert new column(s) before the column at given index. If index is null or undefined, the new column will be added after the current last column. Default amount equals 1
alter ('remove_row', index: Number, amount: Number (Optional), source: String (Optional))
Remove the row(s) at given index. Default amount equals 1
alter ('remove_col', index: Number, amount: Number (Optional), source: String (Optional))
Remove the column(s) at given index. Default amount equals 1

Cell methods

getCell (row: Number, col: Number, topmost: Boolean)
Returns TD element for given row, col if it is rendered on screen.
Returns null if the TD is not rendered on screen (probably because that part of table is not visible).
If the topmost parameter is true, returns the topmost cell from all overlays (such as fixed rows/columns).
getCellMeta (row: Number, col: Number)
Return cell properties for given row, col coordinates
setCellMeta (row: Number, col: Number, key: String, val: String)
Sets cell meta data object key corresponding to params row, col
version added: 0.11
destroyEditor (revertOriginal: Boolean (Optional))
Destroys current editor, renders and selects current cell. If revertOriginal == false, edited data is saved. Otherwise previous value is restored

Selection methods

selectCell (row: Number, col: Number, row2: Number, col2: Number, scrollToSelection: Boolean (Optional))
Select cell row, col or range finishing at row2, col2. By default, viewport will be scrolled to selection
deselectCell ()
Deselect current selection
getSelected ()
Return index 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).
getSelectedRange ()
Returns current selection as a WalkontableCellRange object. Returns undefined if there is no selection.
version added: 0.11
clear ()
Clears grid
version added: 0.11

Grid information methods

countRows ()
Returns total number of rows in the grid
countCols ()
Returns total number of columns in the grid
colToProp (column: Number)
Returns property name that corresponds with the given column index
rowOffset ()
Returns index of first visible row
colOffset ()
Returns index of first visible column
countVisibleRows ()
Returns number of visible rows
countVisibleCols ()
Returns number of visible columns
countEmptyRows (ending: Boolean (Optional))
Returns number of empty rows. If the optional ending parameter is true, returns number of empty rows at the bottom of the table
countEmptyCols (ending: Boolean (Optional))
Returns number of empty columns. If the optional ending parameter is true, returns number of empty columns at right hand edge of the table
isEmptyRow (row: Number)
Returns true if the row at the given index is empty, false otherwise
isEmptyCol (col: Number)
Returns true if the column at the given index is empty, false otherwise
getRowHeader (row: Number)
Returns array of row headers (if they are enabled). If param row given, return header at given row as string
getColHeader (col: Number)
Returns array of col headers (if they are enabled). If param col given, return header at given col as string
hasRowHeaders ()
Returns information of this table is configured to display row headers
version added: 0.11
hasColHeaders ()
Returns information of this table is configured to display column headers
version added: 0.11
getColWidth (col: Number)
Return column width
version added: 0.11
getRowHeight (row: Number)
Return row height
version added: 0.11
propToCol (property: String)
Returns column index that corresponds with the given property

Undo/redo methods

clearUndo ()
Clear undo history
isUndoAvailable ()
Return true if undo can be performed, false otherwise
isRedoAvailable ()
Return true if redo can be performed, false otherwise
undo ()
Undo last edit
redo ()
Redo edit (used to reverse an undo)

Plugin specific methods

sort (column: Number, order: Boolean)
Sorts table content by cell values in given column, using order. column is a zero-based column index. Order of sorting can be either ascending (order = true) or descending (order = false).

Note I: This method is only available when columnSorting plugin is enabled. See column sorting demo for details.

Note II: Running this method will not alter the table data. Sorting takes place only in view layer.

⚠️ **GitHub.com Fallback** ⚠️