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

Skip to content

Commit a77d39b

Browse files
authored
Merge pull request gridstack#1807 from adumesny/master
better save() doc
2 parents 9d02b7d + 2299ca5 commit a77d39b

File tree

5 files changed

+21
-13
lines changed

5 files changed

+21
-13
lines changed

demo/nested.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ <h1>Nested grids demo</h1>
6565
y: Math.round(5 * Math.random()),
6666
w: Math.round(1 + 1 * Math.random()),
6767
h: Math.round(1 + 1 * Math.random()),
68-
content: count++
68+
content: String(count++)
6969
};
7070
subGrid.addWidget(node);
7171
return false;

doc/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Change log
6363

6464
* fix [#1784](https://github.com/gridstack/gridstack.js/issues/1784) `removable:true` working by itself (without needing `acceptWidgets:true`)
6565
* fix [#1791](https://github.com/gridstack/gridstack.js/pull/1791) removed drag flicker and scroll issue. Thanks [@nelsieborja](https://github.com/nelsieborja)
66+
* better doc for save [#1795](https://github.com/gridstack/gridstack.js/issues/1795)
6667

6768
## 4.2.5 (2021-5-31)
6869

doc/README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ gridstack.js API
5353
- [`removeWidget(el, removeDOM = true, triggerEvent = true)`](#removewidgetel-removedom--true-triggerevent--true)
5454
- [`removeAll(removeDOM = true)`](#removeallremovedom--true)
5555
- [`resizable(el, val)`](#resizableel-val)
56-
- [`save(saveContent = true): GridStackWidget[]`](#savesavecontent--true-gridstackwidget)
56+
- [`save(saveContent = true, saveGridOpt = false): GridStackWidget[] | GridStackOptions`](#savesavecontent--true-savegridopt--false-gridstackwidget--gridstackoptions)
5757
- [`setAnimation(doAnimate)`](#setanimationdoanimate)
5858
- [`setStatic(staticValue)`](#setstaticstaticvalue)
5959
- [`update(el: GridStackElement, opts: GridStackWidget)`](#updateel-gridstackelement-opts-gridstackwidget)
@@ -522,10 +522,13 @@ Enables/Disables user resizing of specific grid element. If you want all items,
522522
- `el` - widget to modify
523523
- `val` - if `true` widget will be resizable.
524524

525-
### `save(saveContent = true): GridStackWidget[]`
525+
### `save(saveContent = true, saveGridOpt = false): GridStackWidget[] | GridStackOptions`
526526

527-
- returns the layout of the grid (and optionally the html content as well) that can be serialized (list of item non default attributes, not just w,y,x,y but also min/max and id). See `load()`
528-
- see [example](http://gridstackjs.com/demo/serialization.html)
527+
saves the current layout returning a list of widgets for serialization which might include any nested grids.
528+
- `saveContent` if true (default) the latest html inside `.grid-stack-content` will be saved to `GridStackWidget.content` field, else it will be left unchanged for initial load values.
529+
- `saveGridOpt` if true (default `false`), save the grid options itself, so you can call the new `GridStack.addGrid()` to recreate everything from scratch. GridStackOptions.children would then contain the widget list instead.
530+
- returns list of widgets or full grid option, including .children list of widgets
531+
- see [serialization](http://gridstackjs.com/demo/serialization.html) and [nested](http://gridstackjs.com/demo/nested.html)
529532

530533
### `setAnimation(doAnimate)`
531534

src/gridstack-engine.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -664,24 +664,24 @@ export class GridStackEngine {
664664
return this;
665665
}
666666

667-
/** saves the current layout returning a list of widgets for serialization */
667+
/** saves a copy of the current layout returning a list of widgets for serialization */
668668
public save(saveElement = true): GridStackNode[] {
669-
let widgets: GridStackNode[] = [];
669+
let list: GridStackNode[] = [];
670670
this._sortNodes();
671671
this.nodes.forEach(n => {
672672
let w: GridStackNode = {};
673673
for (let key in n) { if (key[0] !== '_' && n[key] !== null && n[key] !== undefined ) w[key] = n[key]; }
674674
// delete other internals
675-
if (!saveElement) delete w.el;
676675
delete w.grid;
676+
if (!saveElement) delete w.el;
677677
// delete default values (will be re-created on read)
678678
if (!w.autoPosition) delete w.autoPosition;
679679
if (!w.noResize) delete w.noResize;
680680
if (!w.noMove) delete w.noMove;
681681
if (!w.locked) delete w.locked;
682-
widgets.push(w);
682+
list.push(w);
683683
});
684-
return widgets;
684+
return list;
685685
}
686686

687687
/** @internal called whenever a node is added or moved - updates the cached layouts */

src/gridstack.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,9 +440,13 @@ export class GridStack {
440440
}
441441

442442
/**
443-
* saves the current layout returning a list of widgets for serialization (with default to save content), which might include any nested grids.
444-
* Optionally you can also save the grid with options itself, so you can call the new GridStack.addGrid()
445-
* to recreate everything from scratch. GridStackOptions.children would then contain the widget list.
443+
/**
444+
* saves the current layout returning a list of widgets for serialization which might include any nested grids.
445+
* @param saveContent if true (default) the latest html inside .grid-stack-content will be saved to GridStackWidget.content field, else it will
446+
* be left unchanged for initial load values.
447+
* @param saveGridOpt if true (default false), save the grid options itself, so you can call the new GridStack.addGrid()
448+
* to recreate everything from scratch. GridStackOptions.children would then contain the widget list instead.
449+
* @returns list of widgets or full grid option, including .children list of widgets
446450
*/
447451
public save(saveContent = true, saveGridOpt = false): GridStackWidget[] | GridStackOptions {
448452
// return copied nodes we can modify at will...

0 commit comments

Comments
 (0)