|
4 | 4 | <meta charset="utf-8"> |
5 | 5 | <meta http-equiv="X-UA-Compatible" content="IE=edge"> |
6 | 6 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
7 | | - <title>Nested grids demo</title> |
8 | | - |
| 7 | + <title>Nested grids demo (ES6)</title> |
9 | 8 | <link rel="stylesheet" href="demo.css"/> |
10 | 9 | <script src="../dist/gridstack-h5.js"></script> |
11 | 10 | <style type="text/css"> |
12 | 11 | .grid-stack .grid-stack { |
13 | | - /*margin: 0 -10px;*/ |
14 | 12 | background: rgba(255, 255, 255, 0.3); |
15 | 13 | } |
16 | 14 | .grid-stack .grid-stack .grid-stack-item-content { |
|
21 | 19 | <body> |
22 | 20 | <div class="container-fluid"> |
23 | 21 | <h1>Nested grids demo</h1> |
24 | | - <a class="btn btn-primary" onClick="addNewWidget(grid1)" href="#">Add Widget Grid1</a> |
25 | | - <a class="btn btn-primary" onClick="addNewWidget(grid2)" href="#">Add Widget Grid2</a> |
| 22 | + <p>This example uses new v3.1 API to load the entire nested grid from JSON, and shows dragging between nested grid items (pink) vs dragging higher grid items (green)</p> |
| 23 | + <!-- <p>Note: initial v3.0.0 HTML5 release doesn't support 'dragOut:false' constrain (always true) so this uses JQ version for now.</p> --> |
| 24 | + <a class="btn btn-primary" onClick="addNewWidget('.nested1')" href="#">Add Widget Grid1</a> |
| 25 | + <a class="btn btn-primary" onClick="addNewWidget('.nested2')" href="#">Add Widget Grid2</a> |
26 | 26 | <br><br> |
27 | 27 |
|
28 | 28 | <div class="grid-stack"></div> |
29 | 29 | </div> |
30 | 30 |
|
31 | 31 | <script type="text/javascript"> |
32 | | - let sub1 = [ {w: 3}, {w: 3}, {w: 3}, {w: 3}, {w: 3}, {w: 3}]; |
33 | | - let sub2 = [ {w: 3}, {x:0, y: 1, w: 3}]; |
| 32 | + let sub1 = [ {w:3}, {w:3}, {w:3}, {w:3}, {w:3}, {w:3}]; |
| 33 | + let sub2 = [ {w:3}, {x:0, y:1, w:3}]; |
34 | 34 | let count = 0; |
35 | 35 | [...sub1, ...sub2].forEach(d => d.content = String(count++)); |
36 | 36 | let subOptions = { |
| 37 | + itemClass: 'sub', // style sub items differently and use to prevent dragging in/out |
37 | 38 | acceptWidgets: '.grid-stack-item.sub', // only pink sub items can be inserted, otherwise grid-items causes all sort of issues |
38 | 39 | disableOneColumnMode: true, // nested are small, but still want N columns |
39 | | - itemClass: 'sub', |
40 | 40 | margin: 1 |
41 | 41 | }; |
42 | | - let items = [ |
| 42 | + let layout = {cellHeight: 70, children: [ |
43 | 43 | {w:1, content: 'regular item'}, |
44 | 44 | {x:1, w:4, h:4, content: 'nested 1 - can drag items out', subGrid: {children: sub1, dragOut: true, class: 'nested1', ...subOptions}}, |
45 | | - {x:5, w:4, h:4, content: 'nested 2 - constrained to parent (default)', subGrid: {children: sub2, class: 'nested2', ...subOptions}}, |
46 | | - ]; |
| 45 | + {x:5, w:4, h:4, content: 'nested 2 - constrained to parent (default)', subGrid: {children: sub2, dragOut: false, class: 'nested2', ...subOptions}}, |
| 46 | + ]}; |
47 | 47 |
|
48 | | - let grid = GridStack.init({cellHeight: 70}); |
49 | | - grid.load(items); |
| 48 | + GridStack.addGrid(document.querySelector('.container-fluid'), layout); |
50 | 49 |
|
51 | | - addNewWidget = function(grid) { |
| 50 | + addNewWidget = function(selector) { |
| 51 | + grid = document.querySelector(selector).gridstack; |
52 | 52 | let node = { |
53 | 53 | x: Math.round(12 * Math.random()), |
54 | 54 | y: Math.round(5 * Math.random()), |
55 | 55 | w: Math.round(1 + 3 * Math.random()), |
56 | | - h: Math.round(1 + 3 * Math.random()) |
| 56 | + h: Math.round(1 + 3 * Math.random()), |
| 57 | + content: count++ |
57 | 58 | }; |
58 | | - // Note: we have additional style .sub here so add the HTML passed directly... |
59 | | - grid.addWidget('<div class="grid-stack-item sub"><div class="grid-stack-item-content">' + count++ + '</div></div>', node); |
| 59 | + grid.addWidget(node); |
60 | 60 | return false; |
61 | 61 | }; |
62 | 62 | </script> |
|
0 commit comments