Skip to content

Commit 9b455d6

Browse files
committed
#227 Add setGridWidth(gridWidth) method. Attempts to intelligently change existing widgets' x-coordinate and width to map to new width.
1 parent 308a33b commit 9b455d6

File tree

6 files changed

+48
-2
lines changed

6 files changed

+48
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ Changes
443443
- `'auto'` value for `cellHeight` option
444444
- fix `setStatic` method
445445
- add `setAnimation` method to API
446+
- add `setGridWidth` method ([#227](https://github.com/troolee/gridstack.js/issues/227))
446447

447448
#### v0.2.4 (2016-02-15)
448449

dist/gridstack.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,6 +1256,25 @@
12561256
}
12571257
};
12581258

1259+
GridStack.prototype._updateNodeWidths = function(oldWidth, newWidth) {
1260+
this.grid._sortNodes();
1261+
this.grid.batchUpdate();
1262+
var node = {};
1263+
for (var i = 0; i < this.grid.nodes.length; i++) {
1264+
node = this.grid.nodes[i];
1265+
this.update(node.el, Math.round(node.x * newWidth / oldWidth), undefined,
1266+
Math.round(node.width * newWidth / oldWidth), undefined);
1267+
}
1268+
this.grid.commit();
1269+
};
1270+
1271+
GridStack.prototype.setGridWidth = function(gridWidth) {
1272+
this.container.removeClass('grid-stack-' + this.opts.width);
1273+
this._updateNodeWidths(this.opts.width, gridWidth);
1274+
this.opts.width = gridWidth;
1275+
this.container.addClass('grid-stack-' + gridWidth);
1276+
};
1277+
12591278
// jscs:disable requireCamelCaseOrUpperCaseIdentifiers
12601279
GridStackEngine.prototype.batch_update = obsolete(GridStackEngine.prototype.batchUpdate);
12611280
GridStackEngine.prototype._fix_collisions = obsolete(GridStackEngine.prototype._fixCollisions,

dist/gridstack.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/gridstack.min.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

doc/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ gridstack.js API
4343
- [resize(el, width, height)](#resizeel-width-height)
4444
- [resizable(el, val)](#resizableel-val)
4545
- [setAnimation(doAnimate)](#setanimationdoanimate)
46+
- [setGridWidth(gridWidth)](#setgridwidthgridwidth)
4647
- [setStatic(staticValue)](#setstaticstaticvalue)
4748
- [update(el, x, y, width, height)](#updateel-x-y-width-height)
4849
- [willItFit(x, y, width, height, autoPosition)](#willitfitx-y-width-height-autoposition)
@@ -372,6 +373,12 @@ Toggle the grid animation state. Toggles the `grid-stack-animate` class.
372373

373374
- `doAnimate` - if `true` the grid will animate.
374375

376+
### setGridWidth(gridWidth)
377+
378+
(Experimental) Modify number of columns in the grid. Will attempt to update existing widgets to conform to new number of columns. Requires `gridstack-extra.css` or `gridstack-extra.min.css`.
379+
380+
- `gridWidth` - Integer between 1 and 12.
381+
375382
### setStatic(staticValue)
376383

377384
Toggle the grid static state. Also toggle the `grid-stack-static` class.

src/gridstack.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,6 +1256,25 @@
12561256
}
12571257
};
12581258

1259+
GridStack.prototype._updateNodeWidths = function(oldWidth, newWidth) {
1260+
this.grid._sortNodes();
1261+
this.grid.batchUpdate();
1262+
var node = {};
1263+
for (var i = 0; i < this.grid.nodes.length; i++) {
1264+
node = this.grid.nodes[i];
1265+
this.update(node.el, Math.round(node.x * newWidth / oldWidth), undefined,
1266+
Math.round(node.width * newWidth / oldWidth), undefined);
1267+
}
1268+
this.grid.commit();
1269+
};
1270+
1271+
GridStack.prototype.setGridWidth = function(gridWidth) {
1272+
this.container.removeClass('grid-stack-' + this.opts.width);
1273+
this._updateNodeWidths(this.opts.width, gridWidth);
1274+
this.opts.width = gridWidth;
1275+
this.container.addClass('grid-stack-' + gridWidth);
1276+
};
1277+
12591278
// jscs:disable requireCamelCaseOrUpperCaseIdentifiers
12601279
GridStackEngine.prototype.batch_update = obsolete(GridStackEngine.prototype.batchUpdate);
12611280
GridStackEngine.prototype._fix_collisions = obsolete(GridStackEngine.prototype._fixCollisions,

0 commit comments

Comments
 (0)