Last active
March 30, 2018 06:34
-
-
Save pezon/2b6ee291f1d286461d97 to your computer and use it in GitHub Desktop.
Handsontable plugin - Observe dblClick event
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}); |
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
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.