Skip to content

Instantly share code, notes, and snippets.

@jlsalmon
Created June 24, 2015 08:51
Show Gist options
  • Save jlsalmon/396813e4e41176a7bd3a to your computer and use it in GitHub Desktop.
Save jlsalmon/396813e4e41176a7bd3a to your computer and use it in GitHub Desktop.
Custom object-backed editor for Handsontable autocomplete fields
var CustomAutocompleteEditor = Handsontable.editors.AutocompleteEditor.prototype.extend();
CustomAutocompleteEditor.prototype.beginEditing = function(initialValue, event) {
Handsontable.editors.BaseEditor.prototype.beginEditing.call(this, initialValue, event);
if (this.originalValue === undefined || this.originalValue[this.cellProperties.viewModel] === null) {
this.setValue('');
} else {
this.setValue(this.originalValue);
}
};
CustomAutocompleteEditor.prototype.getValue = function() {
return this.customValue === undefined ? '' : this.customValue;
};
CustomAutocompleteEditor.prototype.setValue = function(value) {
if (this.cellProperties.viewModel && typeof value === 'object' && value[this.cellProperties.viewModel]) {
this.TEXTAREA.value = value[this.cellProperties.viewModel];
this.customValue = value;
} else {
this.TEXTAREA.value = value;
}
};
Handsontable.editors.registerEditor('CustomAutocompleteEditor', CustomAutocompleteEditor);
var CustomAutocompleteRenderer = function(instance, td, row, col, prop, value, cellProperties) {
if (cellProperties.viewModel && value && typeof value === 'object' && value.hasOwnProperty(cellProperties.viewModel)) {
td.innerHTML = value[cellProperties.viewModel] == null ? '' : value[cellProperties.viewModel];
} else if (value !== undefined) {
td.innerHTML = value;
}
};
Handsontable.CustomAutocompleteCell = {
editor: CustomAutocompleteEditor,
renderer: CustomAutocompleteRenderer,
validator: Handsontable.AutocompleteValidator,
dataType: 'customAutocomplete'
};
Handsontable.cellTypes.customAutocomplete = Handsontable.CustomAutocompleteCell;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment