Skip to content

Instantly share code, notes, and snippets.

@pezon
Last active March 30, 2018 06:34
Show Gist options
  • Save pezon/2b6ee291f1d286461d97 to your computer and use it in GitHub Desktop.
Save pezon/2b6ee291f1d286461d97 to your computer and use it in GitHub Desktop.
Handsontable plugin - Observe dblClick event
function ObserveDblClick() {
var plugin = this;
// public methods
this.init = function() {
bindHeaders.call(this);
};
this.dblclickColHeader = function(col) {
Handsontable.hooks.run(this, 'dblclickColHeader', col);
};
this.dblclickRowHeader = function(row) {
Handsontable.hooks.run(this, 'dblclickRowHeader', row);
};
this.dblclickCell = function(row, col) {
Handsontable.hooks.run(this, 'dblclickCell', row, col);
};
// private methods
var bindHeaders = function() {
var instance = this,
eventManager = Handsontable.eventManager(instance);
eventManager.addEventListener(instance.rootElement, 'dblclick', function(e) {
if (Handsontable.Dom.hasClass(e.target, 'relative')) {
var col = getColumn(e.target),
row = getRow(e.target);
col -= countRowHeaders(instance);
if (col == 0 & row == 0 && isCorner(e.target)) {
return;
} else if (col == 0) {
plugin.dblclickRowHeader(row);
} else if (row == 0) {
plugin.dblclickColHeader(col);
} else {
plugin.dblclickCell(row, col);
}
}
});
};
function getColumn(target) {
var TH = Handsontable.Dom.closest(target, 'TH');
return Handsontable.Dom.index(TH);
}
function getRow(target) {
var TR = Handsontable.Dom.closest(target, 'TR');
return Handsontable.Dom.index(TR);
}
function isCorner(target) {
var TR = Handsontable.Dom.closest(target, 'TR');
return !TR.querySelector('.rowHeader');
}
function countRowHeaders(instance) {
var THs = instance.view.TBODY.querySelector('tr').querySelectorAll('th');
return THs.length;
}
};
var observeDblClick = new ObserveDblClick();
Handsontable.hooks.add('afterInit', observeDblClick.init);
Handsontable.hooks.register('dblclickColHeader');
Handsontable.hooks.register('dblclickRowHeader');
Handsontable.hooks.register('dblclickCell');
Handsontable.plugins.ObserveDblClick = ObserveDblClick;
var hot = new Handsontable(container, settings);
hot.loadData(data);
hot.hooks.add('dblclickCell', function(row, cell) {
var cellData = hot.getDataAtCell(row, col),
cellElement = hot.getCell(row, col),
cellMeta = hot.getCellMeta(row, col);
// etc
});
hot.hooks.add('dblclickRowHeader', function(row) {
var rowSourceData = hot.getSourceDataAtRow(row),
rowData = hot.getDataAtRow(row);
// etc
});
@titu00
Copy link

titu00 commented Sep 15, 2015

Does this work for you?

@tromss
Copy link

tromss commented Jan 5, 2016

Hi Pezon! I have a error when i use it.
this error is :
Handsontable.eventManager is not a function

do you have the same error?
maybe the file eventManager.js is missing?

thank you for your help!
Romain.

@jimmyye
Copy link

jimmyye commented Mar 30, 2018

Updated the gist to work with Handsontable v0.38.1
https://gist.github.com/jimmyye/46b2ee89c4c0385f29ed5b737fe70da7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment