Skip to content

Commit

Permalink
bugfix: restored original behavior of setDataAtCell. Added new meth…
Browse files Browse the repository at this point in the history
…ods `setDataAtRowProp` and `getDataAtRowProp` (#284)
  • Loading branch information
warpech committed Mar 7, 2013
1 parent 55a0c92 commit 90e1b67
Show file tree
Hide file tree
Showing 13 changed files with 381 additions and 191 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## HEAD

Bugfix:
- fix very long standing inconsistency. Restored original `setDataAtCell` requirement of the second parameter to be a column number (as described in [README.md](https://github.com/warpech/jquery-handsontable) since always). This **may be** a breaking change for some applications. If you happen to experience an error in `setDataAtCell` after upgrade, change your usage of this method to the new method `setDataAtRowProp` ([#284](https://github.com/warpech/jquery-handsontable/pull/284))
- new methods `setDataAtRowProp` and `getDataAtRowProp` that set/get data according to the property name in data source. See [README.md](https://github.com/warpech/jquery-handsontable) for clarification

## [0.8.10](https://github.com/warpech/jquery-handsontable/tree/v0.8.10) (Mar 7, 2013)

Bugfix:
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ Thanks for understanding!
handsontable('updateSettings', options) | Method | Use it if you need to change configuration after initialization
handsontable('loadData', data) | Method | Reset all cells in the grid to contain data from the `data` array
handsontable('render') | Method | Rerender the table
handsontable('setDataAtCell', row, col, value) | Method | 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
handsontable('getDataAtCell', row, col) | Method | Return cell value at `row`, `col`
handsontable('setDataAtCell', row, col, value) | Method | 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)
handsontable('setDataAtRowProp', row, prop, value) | Method | Same as above, except instead of `col`, you provide name of the object property (eq. [0, 'first.name', 'Jennifer'])
handsontable('getDataAtCell', row, col) | Method | Return cell value at `row`, `col`. Col is the index of **visible** column (note that if columns were reordered, the current order will be used)
handsontable('getDataAtRowProp', row, prop) | Method | Same as above, except instead of `col`, you provide name of the object property (eq. [0, 'first.name'])
handsontable('countRows') | Method | Return total number of rows in the grid
handsontable('countCols') | Method | Return total number of columns in the grid
handsontable('rowOffset') | Method | Return index of first visible row
Expand Down
2 changes: 1 addition & 1 deletion dist/jquery.handsontable.full.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Licensed under the MIT license.
* http://handsontable.com/
*
* Date: Thu Mar 07 2013 10:13:24 GMT+0100 (Central European Standard Time)
* Date: Thu Mar 07 2013 19:36:46 GMT+0100 (Central European Standard Time)
*/

.handsontable {
Expand Down
183 changes: 122 additions & 61 deletions dist/jquery.handsontable.full.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Licensed under the MIT license.
* http://handsontable.com/
*
* Date: Thu Mar 07 2013 10:13:24 GMT+0100 (Central European Standard Time)
* Date: Thu Mar 07 2013 19:36:46 GMT+0100 (Central European Standard Time)
*/
/*jslint white: true, browser: true, plusplus: true, indent: 4, maxerr: 50 */

Expand All @@ -26,7 +26,7 @@ var Handsontable = { //class namespace
Handsontable.Core = function (rootElement, settings) {
this.rootElement = rootElement;

var priv, datamap, grid, selection, editproxy, autofill, validate, self = this;
var priv, datamap, grid, selection, editproxy, autofill, self = this;

priv = {
settings: {},
Expand Down Expand Up @@ -548,8 +548,7 @@ Handsontable.Core = function (rootElement, settings) {
break;
}
if (self.getCellMeta(current.row, current.col).isWritable) {
var p = datamap.colToProp(current.col);
setData.push([current.row, p, input[r][c]]);
setData.push([current.row, current.col, input[r][c]]);
}
current.col++;
if (end && c === clen - 1) {
Expand Down Expand Up @@ -828,7 +827,7 @@ Handsontable.Core = function (rootElement, settings) {
for (r = corners.TL.row; r <= corners.BR.row; r++) {
for (c = corners.TL.col; c <= corners.BR.col; c++) {
if (self.getCellMeta(r, c).isWritable) {
changes.push([r, datamap.colToProp(c), '']);
changes.push([r, c, '']);
}
}
}
Expand Down Expand Up @@ -1233,7 +1232,7 @@ Handsontable.Core = function (rootElement, settings) {
Handsontable.PluginHooks.run(self, 'afterInit');
};

validate = function (changes, source) {
function validateChanges(changes, source) {
var validated = $.Deferred();
var deferreds = [];

Expand Down Expand Up @@ -1311,7 +1310,7 @@ Handsontable.Core = function (rootElement, settings) {
});

return $.when(validated);
};
}

var fireEvent = function (name, params) {
if (priv.settings.asyncRendering) {
Expand Down Expand Up @@ -1343,55 +1342,106 @@ Handsontable.Core = function (rootElement, settings) {
};

/**
* Set data at given cell
* @public
* @param {Number|Array} row or array of changes in format [[row, col, value], ...]
* @param {Number} prop
* @param {String} value
* @param {String} [source='edit'] String that identifies how this change will be described in changes array (useful in onChange callback)
* Internal function to apply changes. Called after validateChanges
* @param {Array} changes Array in form of [row, prop, oldValue, newValue]
* @param {String} source String that identifies how this change will be described in changes array (useful in onChange callback)
*/
this.setDataAtCell = function (row, prop, value, source) {
var changes, i, ilen;
function applyChanges(changes, source) {
var i
, ilen;

if (typeof row === "object") { //is it an array of changes
changes = row;
for (i = 0, ilen = changes.length; i < ilen; i++) {
if (priv.settings.minSpareRows) {
while (changes[i][0] > self.countRows() - 1) {
datamap.createRow();
}
}
if (priv.dataType === 'array' && priv.settings.minSpareCols) {
while (datamap.propToCol(changes[i][1]) > self.countCols() - 1) {
datamap.createCol();
}
}
datamap.set(changes[i][0], changes[i][1], changes[i][3]);
}
else if ($.isPlainObject(value)) { //backwards compatibility
changes = value;
self.forceFullRender = true; //used when data was changed
grid.keepEmptyRows();
selection.refreshBorders();
fireEvent("datachange.handsontable", [changes, source || 'edit']);
}

function setDataInputToArray(arg0, arg1, arg2) {
if (typeof arg0 === "object") { //is it an array of changes
return arg0;
}
else if ($.isPlainObject(arg2)) { //backwards compatibility
return value;
}
else {
changes = [
[row, prop, value]
return [
[arg0, arg1, arg2]
];
}
}

for (i = 0, ilen = changes.length; i < ilen; i++) {
changes[i].splice(2, 0, datamap.get(changes[i][0], changes[i][1])); //add old value at index 2
/**
* Set data at given cell
* @public
* @param {Number|Array} row or array of changes in format [[row, col, value], ...]
* @param {Number} col
* @param {String} value
* @param {String} source String that identifies how this change will be described in changes array (useful in onChange callback)
*/
this.setDataAtCell = function (row, col, value, source) {
var input = setDataInputToArray(row, col, value)
, i
, ilen
, changes = []
, prop;

for (i = 0, ilen = input.length; i < ilen; i++) {
if(typeof input[i][1] !== 'number') {
throw new Exception('Method `setDataAtCell` accepts row and column number as its parameters. If you want to use object property name, use method `setDataAtRowProp`');
}
prop = datamap.colToProp(input[i][1]);
changes.push([
input[i][0],
prop,
datamap.get(input[i][0], prop),
input[i][2]
]);
}

validate(changes, source).then(function () { //when validate is resolved...
for (i = 0, ilen = changes.length; i < ilen; i++) {
row = changes[i][0];
prop = changes[i][1];
var col = datamap.propToCol(prop);
value = changes[i][3];
validateChanges(changes, source).then(function () {
applyChanges(changes, source);
});
};

if (priv.settings.minSpareRows) {
while (row > self.countRows() - 1) {
datamap.createRow();
}
}
if (priv.dataType === 'array' && priv.settings.minSpareCols) {
while (col > self.countCols() - 1) {
datamap.createCol();
}
}
datamap.set(row, prop, value);
}
self.forceFullRender = true; //used when data was changed
grid.keepEmptyRows();
selection.refreshBorders();
fireEvent("datachange.handsontable", [changes, source || 'edit']);

/**
* Set data at given row property
* @public
* @param {Number|Array} row or array of changes in format [[row, prop, value], ...]
* @param {Number} prop
* @param {String} value
* @param {String} source String that identifies how this change will be described in changes array (useful in onChange callback)
*/
this.setDataAtRowProp = function (row, prop, value, source) {
var input = setDataInputToArray(row, prop, value)
, i
, ilen
, changes = [];

for (i = 0, ilen = input.length; i < ilen; i++) {
changes.push([
input[i][0],
input[i][1],
datamap.get(input[i][0], input[i][1]),
input[i][2]
]);
}

validateChanges(changes, source).then(function () {
applyChanges(changes, source);
});
};

Expand Down Expand Up @@ -1690,16 +1740,27 @@ Handsontable.Core = function (rootElement, settings) {
};

/**
* Return cell value at `row`, `col`
* Return value at `row`, `col`
* @param {Number} row
* @param {Number} col
* @public
* @return {string}
* @return value (mixed data type)
*/
this.getDataAtCell = function (row, col) {
return datamap.get(row, datamap.colToProp(col));
};

/**
* Return value at `row`, `prop`
* @param {Number} row
* @param {Number} prop
* @public
* @return value (mixed data type)
*/
this.getDataAtRowProp = function (row, prop) {
return datamap.get(row, prop);
};

/**
* Returns cell meta data object corresponding to params row, col
* @param {Number} row
Expand Down Expand Up @@ -2314,7 +2375,7 @@ Handsontable.TableView.prototype.applyCellTypeMethod = function (methodName, td,
var prop = this.instance.colToProp(col)
, cellProperties = this.instance.getCellMeta(row, col);
if (cellProperties[methodName]) {
return cellProperties[methodName](this.instance, td, row, col, prop, this.instance.getDataAtCell(row, col), cellProperties);
return cellProperties[methodName](this.instance, td, row, col, prop, this.instance.getDataAtRowProp(row, prop), cellProperties);
}
};

Expand Down Expand Up @@ -2407,7 +2468,7 @@ Handsontable.UndoRedo.prototype.undo = function () {
for (i = 0, ilen = setData.length; i < ilen; i++) {
setData[i].splice(3, 1);
}
this.instance.setDataAtCell(setData, null, null, 'undo');
this.instance.setDataAtRowProp(setData, null, null, 'undo');
this.rev--;
}
};
Expand All @@ -2423,7 +2484,7 @@ Handsontable.UndoRedo.prototype.redo = function () {
for (i = 0, ilen = setData.length; i < ilen; i++) {
setData[i].splice(2, 1);
}
this.instance.setDataAtCell(setData, null, null, 'redo');
this.instance.setDataAtRowProp(setData, null, null, 'redo');
}
};

Expand Down Expand Up @@ -2577,10 +2638,10 @@ Handsontable.CheckboxRenderer = function (instance, td, row, col, prop, value, c
var $input = $(td).find('input:first');
$input.mousedown(function (event) {
if (!$(this).is(':checked')) {
instance.setDataAtCell(row, prop, cellProperties.checkedTemplate);
instance.setDataAtRowProp(row, prop, cellProperties.checkedTemplate);
}
else {
instance.setDataAtCell(row, prop, cellProperties.uncheckedTemplate);
instance.setDataAtRowProp(row, prop, cellProperties.uncheckedTemplate);
}
event.stopPropagation(); //otherwise can confuse mousedown handler
});
Expand Down Expand Up @@ -2993,7 +3054,7 @@ HandsontableAutocompleteEditorClass.prototype.createElements = function () {
this.typeahead.matcher = function () {
return true;
};
}
};

HandsontableAutocompleteEditorClass.prototype._bindEvents = HandsontableTextEditorClass.prototype.bindEvents;

Expand Down Expand Up @@ -3031,7 +3092,7 @@ HandsontableAutocompleteEditorClass.prototype.bindEvents = function () {
});

this._bindEvents();
}
};

HandsontableAutocompleteEditorClass.prototype._bindTemporaryEvents = HandsontableTextEditorClass.prototype.bindTemporaryEvents;

Expand All @@ -3047,7 +3108,7 @@ HandsontableAutocompleteEditorClass.prototype.bindTemporaryEvents = function (td
cellProperties.onSelect(row, col, prop, this.$menu.find('.active').attr('data-value'), this.$menu.find('.active').index());
}
else {
that.instance.setDataAtCell(row, prop, this.$menu.find('.active').attr('data-value'));
that.instance.setDataAtRowProp(row, prop, this.$menu.find('.active').attr('data-value'));
}
return output;
};
Expand Down Expand Up @@ -3086,7 +3147,7 @@ HandsontableAutocompleteEditorClass.prototype.bindTemporaryEvents = function (td
}

this.instance.view.wt.update('onCellDblClick', onDblClick);
}
};

HandsontableAutocompleteEditorClass.prototype._finishEditing = HandsontableTextEditorClass.prototype.finishEditing;

Expand All @@ -3095,7 +3156,7 @@ HandsontableAutocompleteEditorClass.prototype.finishEditing = function (isCancel
this.typeahead.select();
}
this._finishEditing(isCancelled, ctrlDown);
}
};

HandsontableAutocompleteEditorClass.prototype.isMenuExpanded = function () {
if (this.typeahead.$menu.is(":visible")) {
Expand All @@ -3104,7 +3165,7 @@ HandsontableAutocompleteEditorClass.prototype.isMenuExpanded = function () {
else {
return false;
}
}
};

/**
* Autocomplete editor
Expand All @@ -3126,11 +3187,11 @@ Handsontable.AutocompleteEditor = function (instance, td, row, col, prop, value,
}
};
function toggleCheckboxCell(instance, row, prop, cellProperties) {
if (Handsontable.helper.stringify(instance.getDataAtCell(row, prop)) === Handsontable.helper.stringify(cellProperties.checkedTemplate)) {
instance.setDataAtCell(row, prop, cellProperties.uncheckedTemplate);
if (Handsontable.helper.stringify(instance.getDataAtRowProp(row, prop)) === Handsontable.helper.stringify(cellProperties.checkedTemplate)) {
instance.setDataAtRowProp(row, prop, cellProperties.uncheckedTemplate);
}
else {
instance.setDataAtCell(row, prop, cellProperties.checkedTemplate);
instance.setDataAtRowProp(row, prop, cellProperties.checkedTemplate);
}
}

Expand Down
2 changes: 1 addition & 1 deletion jquery.handsontable.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Licensed under the MIT license.
* http://handsontable.com/
*
* Date: Thu Mar 07 2013 10:13:24 GMT+0100 (Central European Standard Time)
* Date: Thu Mar 07 2013 19:36:46 GMT+0100 (Central European Standard Time)
*/

.handsontable {
Expand Down
Loading

0 comments on commit 90e1b67

Please sign in to comment.