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

Skip to content

Commit c17ae60

Browse files
committed
add serialization demo
1 parent 39498e4 commit c17ae60

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

demo/serialization.html

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<!--[if lt IE 9]>
5+
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
6+
<![endif]-->
7+
8+
<meta charset="utf-8">
9+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
10+
<meta name="viewport" content="width=device-width, initial-scale=1">
11+
<title>Serialization demo</title>
12+
13+
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
14+
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/css/font-awesome.min.css"/>
15+
<link rel="stylesheet" href="../src/gridstack.css"/>
16+
17+
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
18+
<script src="//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.0/jquery-ui.js"></script>
19+
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
20+
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>
21+
<script src="../src/gridstack.js"></script>
22+
23+
<style type="text/css">
24+
.grid-stack {
25+
background: lightgoldenrodyellow;
26+
}
27+
28+
.grid-stack-item-content {
29+
color: #2c3e50;
30+
text-align: center;
31+
background-color: #18bc9c;
32+
}
33+
</style>
34+
</head>
35+
<body>
36+
<div class="container-fluid">
37+
<h1>Serialization demo</h1>
38+
39+
<div>
40+
<a class="btn btn-default" id="save-grid" href="#">Save Grid</a>
41+
<a class="btn btn-default" id="load-grid" href="#">Load Grid</a>
42+
<a class="btn btn-default" id="clear-grid" href="#">Clear Grid</a>
43+
</div>
44+
45+
<br/>
46+
47+
<div class="grid-stack">
48+
</div>
49+
50+
<hr/>
51+
52+
<textarea id="saved-data" cols="100" rows="20" readonly="readonly"></textarea>
53+
</div>
54+
55+
56+
<script type="text/javascript">
57+
$(function () {
58+
var options = {
59+
};
60+
$('.grid-stack').gridstack(options);
61+
62+
new function () {
63+
this.serialized_data = [
64+
{x: 0, y: 0, width: 2, height: 2},
65+
{x: 3, y: 1, width: 1, height: 2},
66+
{x: 4, y: 1, width: 1, height: 1},
67+
{x: 2, y: 3, width: 3, height: 1},
68+
{x: 1, y: 4, width: 1, height: 1},
69+
{x: 1, y: 3, width: 1, height: 1},
70+
{x: 2, y: 4, width: 1, height: 1},
71+
{x: 2, y: 5, width: 1, height: 1}
72+
];
73+
74+
this.grid = $('.grid-stack').data('gridstack');
75+
76+
this.load_grid = function () {
77+
this.grid.remove_all();
78+
var items = GridStackUI.Utils.sort(this.serialized_data);
79+
_.each(items, function (node) {
80+
this.grid.add_widget($('<div><div class="grid-stack-item-content" /><div/>'),
81+
node.x, node.y, node.width, node.height);
82+
}, this);
83+
}.bind(this);
84+
85+
this.save_grid = function () {
86+
this.serialized_data = _.map($('.grid-stack > .grid-stack-item:visible'), function (el) {
87+
el = $(el);
88+
var node = el.data('_gridstack_node');
89+
return {
90+
x: node.x,
91+
y: node.y,
92+
width: node.width,
93+
height: node.height
94+
};
95+
}, this);
96+
$('#saved-data').val(JSON.stringify(this.serialized_data, null, ' '));
97+
}.bind(this);
98+
99+
this.clear_grid = function () {
100+
this.grid.remove_all();
101+
}.bind(this);
102+
103+
$('#save-grid').click(this.save_grid);
104+
$('#load-grid').click(this.load_grid);
105+
$('#clear-grid').click(this.clear_grid);
106+
107+
this.load_grid();
108+
};
109+
});
110+
</script>
111+
</body>
112+
</html>

0 commit comments

Comments
 (0)