Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a501567

Browse files
committed
save to array sample
1 parent fdff2cd commit a501567

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Inspired by [gridster.js](http://gridster.net). Built with love.
4545
- [Touch devices support](#touch-devices-support)
4646
- [Use with knockout.js](#use-with-knockoutjs)
4747
- [Change grid width](#change-grid-width)
48+
- [Save grid to array](#save-grid-to-array)
4849
- [Load grid from array](#load-grid-from-array)
4950
- [Override resizable/draggable options](#override-resizabledraggable-options)
5051
- [IE8 support](#ie8-support)
@@ -472,6 +473,29 @@ For 4-column grid it should be:
472473

473474
and so on.
474475

476+
## Save grid to array
477+
478+
Because gridstack doesn't track any kind of user-defined widget id there is no reason to make serialization to be part
479+
of gridstack API. To serialize grid you can simply do something like this (let's say you store widget id inside `data-custom-id`
480+
attribute):
481+
482+
```javascript
483+
var res = _.map($('.grid-stack .grid-stack-item:visible'), function (el) {
484+
el = $(el);
485+
var node = el.data('_gridstack_node');
486+
return {
487+
id: el.attr('data-custom-id'),
488+
x: node.x,
489+
y: node.y,
490+
width: node.width,
491+
height: node.height
492+
};
493+
});
494+
alert(JSON.stringify(res));
495+
```
496+
497+
You can also use `onchange` event if you need to save only changed widgets right away they have been changed.
498+
475499
## Load grid from array
476500

477501
```javascript
@@ -497,6 +521,8 @@ _.each(serialization, function (node) {
497521
});
498522
```
499523

524+
If you're using knockout there is no need for such method at all.
525+
500526
## Override resizable/draggable options
501527

502528
You can override default `resizable`/`draggable` options. For instance to enable other then bottom right resizing handle

0 commit comments

Comments
 (0)