Skip to content

Commit

Permalink
feature: now the third parameter to the alter method tells the amou…
Browse files Browse the repository at this point in the history
…nt of rows/columns to be inserted/removed (#368)
  • Loading branch information
warpech committed Mar 8, 2013
1 parent e72ca49 commit 67fe67c
Show file tree
Hide file tree
Showing 8 changed files with 678 additions and 288 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
## HEAD

Features:
- this may be a **breaking change** for some applications. Now the third parameter to the `alter` method tells the amount of rows/columns to be inserted/removed. This adds more consistency to the API. ([#368](https://github.com/warpech/jquery-handsontable/issues/368))

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))
- again, this may be a **breaking change** for some applications. 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). 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
- merge pull request [#266](https://github.com/warpech/jquery-handsontable/pull/266) - fix countCols for arrays with column settings

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ Thanks for understanding!
handsontable('clear') | Method | Empty all cells
handsontable('clearUndo') | Method | Clear undo history
handsontable('getData', [r, c, r2, c2]) | Method | Return the current data object (the same that was passed by `data` configuration option or `loadData` method). Optionally you can provide cell range `r`, `c`, `r2`, `c2` to get only a fragment of grid data
handsontable('alter', 'insert_row', index) | Method | Insert new row above the row at given index
handsontable('alter', 'insert_col', index) | Method | Insert new column before the column at given index
handsontable('alter', 'remove_row', index, [toIndex]) | Method | Remove the row at given index [optionally to another index]
handsontable('alter', 'remove_col', index, [toIndex]) | Method | Remove the column at given index [optionally to another index]
handsontable('alter', 'insert_row', amount) | Method | Insert new row(s) above the row at given `index`. Default `amount` equals 1
handsontable('alter', 'insert_col', amount) | Method | Insert new column(s) before the column at given `index`. Default `amount` equals 1
handsontable('alter', 'remove_row', index, amount) | Method | Remove the row(s) at given `index`. Default `amount` equals 1
handsontable('alter', 'remove_col', index, amount) | Method | Remove the column(s) at given `index`. Default `amount` equals 1
handsontable('getCell', row, col) | Method | Return <td> element for given `row,col`
handsontable('getCellMeta', row, col) | Method | Return cell properties for given `row`, `col` coordinates
handsontable('selectCell', r, c, [r2, c2, scrollToSelection=true]) | Method | Select cell `r,c` or range finishing at `r2,c2`. By default, viewport will be scrolled to selection
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 23:13:52 GMT+0100 (Central European Standard Time)
* Date: Fri Mar 08 2013 01:14:54 GMT+0100 (Central European Standard Time)
*/

.handsontable {
Expand Down
141 changes: 70 additions & 71 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 23:13:52 GMT+0100 (Central European Standard Time)
* Date: Fri Mar 08 2013 01:14:54 GMT+0100 (Central European Standard Time)
*/
/*jslint white: true, browser: true, plusplus: true, indent: 4, maxerr: 50 */

Expand Down Expand Up @@ -142,9 +142,9 @@ Handsontable.Core = function (rootElement, settings) {

/**
* Creates row at the bottom of the data array
* @param {Object} [coords] Optional. Coords of the cell before which the new row will be inserted
* @param {Number} [index] Optional. Index of the row before which the new row will be inserted
*/
createRow: function (coords) {
createRow: function (index) {
var row;
if (priv.dataType === 'array') {
row = [];
Expand All @@ -155,31 +155,31 @@ Handsontable.Core = function (rootElement, settings) {
else {
row = $.extend(true, {}, datamap.getSchema());
}
if (!coords || coords.row >= self.countRows()) {
if (typeof index !== 'number' || index >= self.countRows()) {
if (priv.settings.onCreateRow) {
priv.settings.onCreateRow(self.countRows(), row);
}
priv.settings.data.push(row);
}
else {
if (priv.settings.onCreateRow) {
priv.settings.onCreateRow(coords.row, row);
priv.settings.onCreateRow(index, row);
}
priv.settings.data.splice(coords.row, 0, row);
priv.settings.data.splice(index, 0, row);
}
self.forceFullRender = true; //used when data was changed
},

/**
* Creates col at the right of the data array
* @param {Object} [coords] Optional. Coords of the cell before which the new column will be inserted
* @param {Object} [index] Optional. Index of the column before which the new column will be inserted
*/
createCol: function (coords) {
createCol: function (index) {
if (priv.dataType === 'object' || priv.settings.columns) {
throw new Error("Cannot create new column. When data source in an object, you can only have as much columns as defined in first data row, data schema or in the 'columns' setting");
}
var r = 0, rlen = self.countRows();
if (!coords || coords.col >= self.countCols()) {
if (typeof index !== 'number' || index >= self.countCols()) {
for (; r < rlen; r++) {
if (typeof priv.settings.data[r] === 'undefined') {
priv.settings.data[r] = [];
Expand All @@ -189,47 +189,45 @@ Handsontable.Core = function (rootElement, settings) {
}
else {
for (; r < rlen; r++) {
priv.settings.data[r].splice(coords.col, 0, '');
priv.settings.data[r].splice(index, 0, '');
}
}
self.forceFullRender = true; //used when data was changed
},

/**
* Removes row at the bottom of the data array
* @param {Object} [coords] Optional. Coords of the cell which row will be removed
* @param {Object} [toCoords] Required if coords is defined. Coords of the cell until which all rows will be removed
* Removes row from the data array
* @param {Number} [index] Optional. Index of the row to be removed. If not provided, the last row will be removed
* @param {Number} [amount] Optional. Amount of the rows to be removed. If not provided, one row will be removed
*/
removeRow: function (coords, toCoords) {
if (!coords || coords.row === self.countRows() - 1) {
priv.settings.data.pop();
removeRow: function (index, amount) {
if (!amount) {
amount = 1;
}
else {
priv.settings.data.splice(coords.row, toCoords.row - coords.row + 1);
if (typeof index !== 'number') {
index = -amount;
}
priv.settings.data.splice(index, amount);
self.forceFullRender = true; //used when data was changed
},

/**
* Removes col at the right of the data array
* @param {Object} [coords] Optional. Coords of the cell which col will be removed
* @param {Object} [toCoords] Required if coords is defined. Coords of the cell until which all cols will be removed
* Removes column from the data array
* @param {Number} [index] Optional. Index of the column to be removed. If not provided, the last column will be removed
* @param {Number} [amount] Optional. Amount of the columns to be removed. If not provided, one column will be removed
*/
removeCol: function (coords, toCoords) {
removeCol: function (index, amount) {
if (priv.dataType === 'object' || priv.settings.columns) {
throw new Error("cannot remove column with object data source or columns option specified");
}
var r = 0;
if (!coords || coords.col === self.countCols() - 1) {
for (; r < self.countRows(); r++) {
priv.settings.data[r].pop();
}
if (!amount) {
amount = 1;
}
else {
var howMany = toCoords.col - coords.col + 1;
for (; r < self.countRows(); r++) {
priv.settings.data[r].splice(coords.col, howMany);
}
if (typeof index !== 'number') {
index = -amount;
}
for (var r = 0, rlen = self.countRows(); r < rlen; r++) {
priv.settings.data[r].splice(index, amount);
}
self.forceFullRender = true; //used when data was changed
},
Expand Down Expand Up @@ -340,22 +338,29 @@ Handsontable.Core = function (rootElement, settings) {

grid = {
/**
* Alter grid
* Inserts or removes rows and columns
* @param {String} action Possible values: "insert_row", "insert_col", "remove_row", "remove_col"
* @param {Object} coords
* @param {Object} [toCoords] Required only for actions "remove_row" and "remove_col"
* @param {Number} index
* @param {Number} amount
*/
alter: function (action, coords, toCoords) {
var oldData, newData, changes, r, rlen, c, clen;
alter: function (action, index, amount) {
var oldData, newData, changes, r, rlen, c, clen, delta;
oldData = $.extend(true, [], datamap.getAll());

switch (action) {
case "insert_row":
if (self.countRows() < priv.settings.maxRows) {
datamap.createRow(coords);
if (priv.selStart.exists() && priv.selStart.row() >= coords.row) {
priv.selStart.row(priv.selStart.row() + 1);
selection.transformEnd(1, 0); //will call render() internally
if (!amount) {
amount = 1;
}
delta = 0;
while (delta < amount && self.countRows() < priv.settings.maxRows) {
datamap.createRow(index);
delta++;
}
if (delta) {
if (priv.selStart.exists() && priv.selStart.row() >= index) {
priv.selStart.row(priv.selStart.row() + delta);
selection.transformEnd(delta, 0); //will call render() internally
}
else {
selection.refreshBorders(); //it will call render and prepare methods
Expand All @@ -364,11 +369,18 @@ Handsontable.Core = function (rootElement, settings) {
break;

case "insert_col":
if (self.countCols() < priv.settings.maxCols) {
datamap.createCol(coords);
if (priv.selStart.exists() && priv.selStart.col() >= coords.col) {
priv.selStart.col(priv.selStart.col() + 1);
selection.transformEnd(0, 1); //will call render() internally
if (!amount) {
amount = 1;
}
delta = 0;
while (delta < amount && self.countCols() < priv.settings.maxCols) {
datamap.createCol(index);
delta++;
}
if (delta) {
if (priv.selStart.exists() && priv.selStart.col() >= index) {
priv.selStart.col(priv.selStart.col() + delta);
selection.transformEnd(0, delta); //will call render() internally
}
else {
selection.refreshBorders(); //it will call render and prepare methods
Expand All @@ -377,16 +389,20 @@ Handsontable.Core = function (rootElement, settings) {
break;

case "remove_row":
datamap.removeRow(coords, toCoords);
datamap.removeRow(index, amount);
grid.keepEmptyRows();
selection.refreshBorders(); //it will call render and prepare methods
break;

case "remove_col":
datamap.removeCol(coords, toCoords);
datamap.removeCol(index, amount);
grid.keepEmptyRows();
selection.refreshBorders(); //it will call render and prepare methods
break;

default:
throw Error('There is no such action "' + action + '"');
break;
}

changes = [];
Expand Down Expand Up @@ -1681,31 +1697,14 @@ Handsontable.Core = function (rootElement, settings) {
};

/**
* Alters the grid
* Inserts or removes rows and columns
* @param {String} action See grid.alter for possible values
* @param {Number} from
* @param {Number} [to] Optional. Used only for actions "remove_row" and "remove_col"
* @param {Number} index
* @param {Number} amount
* @public
*/
this.alter = function (action, from, to) {
if (typeof to === "undefined") {
to = from;
}
switch (action) {
case "insert_row":
case "remove_row":
grid.alter(action, {row: from, col: 0}, {row: to, col: 0});
break;

case "insert_col":
case "remove_col":
grid.alter(action, {row: 0, col: from}, {row: 0, col: to});
break;

default:
throw Error('There is no such action "' + action + '"');
break;
}
this.alter = function (action, index, amount) {
grid.alter(action, index, amount);
};

/**
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 23:13:52 GMT+0100 (Central European Standard Time)
* Date: Fri Mar 08 2013 01:14:54 GMT+0100 (Central European Standard Time)
*/

.handsontable {
Expand Down
Loading

0 comments on commit 67fe67c

Please sign in to comment.