|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="utf-8"> |
| 5 | + <meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| 6 | + <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 7 | + <title>Nested grids demo (ES6)</title> |
| 8 | + <link rel="stylesheet" href="demo.css"/> |
| 9 | + <link rel="stylesheet" href="../dist/gridstack-extra.min.css"/> |
| 10 | + <script src="../dist/gridstack-h5.js"></script> |
| 11 | + <style type="text/css"> |
| 12 | + .grid-stack.grid-stack-nested { |
| 13 | + background: rgba(255, 255, 255, 0.3); |
| 14 | + } |
| 15 | + .grid-stack-item.sub .grid-stack-item-content { |
| 16 | + background: lightpink; |
| 17 | + } |
| 18 | + </style> |
| 19 | +</head> |
| 20 | +<body> |
| 21 | + <div class="container-fluid"> |
| 22 | + <h1>Advanced Nested grids demo</h1> |
| 23 | + <p>This example shows sub-grids only accepting pink items, while parent accept all.</p> |
| 24 | + <a class="btn btn-primary" onClick="addNested()" href="#">Add Widget</a> |
| 25 | + <a class="btn btn-primary" onClick="addNewWidget('.sub1')" href="#">Add Widget Grid1</a> |
| 26 | + <a class="btn btn-primary" onClick="addNewWidget('.sub2')" href="#">Add Widget Grid2</a> |
| 27 | + <span>entire save/re-create:</span> |
| 28 | + <a class="btn btn-primary" onClick="save()" href="#">Save</a> |
| 29 | + <a class="btn btn-primary" onClick="destroy()" href="#">Destroy</a> |
| 30 | + <a class="btn btn-primary" onClick="load()" href="#">Create</a> |
| 31 | + <span>partial save/load:</span> |
| 32 | + <a class="btn btn-primary" onClick="save(true, false)" href="#">Save list</a> |
| 33 | + <a class="btn btn-primary" onClick="save(false, false)" href="#">Save no content</a> |
| 34 | + <a class="btn btn-primary" onClick="destroy(false)" href="#">Clear</a> |
| 35 | + <a class="btn btn-primary" onClick="load(false)" href="#">Load</a> |
| 36 | + <br><br> |
| 37 | + <!-- grid will be added here --> |
| 38 | + </div> |
| 39 | + |
| 40 | + <script type="text/javascript"> |
| 41 | + let sub1 = [ {x:0, y:0}, {x:1, y:0}, {x:2, y:0}, {x:3, y:0}, {x:0, y:1}, {x:1, y:1}]; |
| 42 | + let sub2 = [ {x:0, y:0}, {x:0, y:1, w:2}]; |
| 43 | + let count = 0; |
| 44 | + [...sub1, ...sub2].forEach(d => d.content = String(count++)); |
| 45 | + let subOptions = { |
| 46 | + cellHeight: 50, |
| 47 | + column: 'auto', // size to match container. make sure to include gridstack-extra.min.css |
| 48 | + itemClass: 'sub', // style sub items differently and use to prevent dragging in/out |
| 49 | + acceptWidgets: '.grid-stack-item.sub', // only pink sub items can be inserted |
| 50 | + margin: 2, |
| 51 | + minRow: 1, // don't collapse when empty |
| 52 | + }; |
| 53 | + let options = { // main grid options |
| 54 | + cellHeight: 50, |
| 55 | + margin: 5, |
| 56 | + minRow: 2, // don't collapse when empty |
| 57 | + acceptWidgets: true, |
| 58 | + id: 'main', |
| 59 | + children: [ |
| 60 | + {y:0, content: 'regular item'}, |
| 61 | + {x:1, w:4, h:4, content: 'nested 1 - can drag items out', subGrid: {children: sub1, dragOut: true, class: 'sub1', ...subOptions}}, |
| 62 | + {x:5, w:4, h:4, content: 'nested 2 - constrained to parent (JQ only)', subGrid: {children: sub2, class: 'sub2', ...subOptions}}, |
| 63 | + ] |
| 64 | + }; |
| 65 | + |
| 66 | + // create and load it all from JSON above |
| 67 | + let grid = GridStack.addGrid(document.querySelector('.container-fluid'), options); |
| 68 | + |
| 69 | + addNested = function() { |
| 70 | + grid.addWidget({x:0, y:100, content:"new item"}); |
| 71 | + } |
| 72 | + |
| 73 | + addNewWidget = function(selector) { |
| 74 | + let subGrid = document.querySelector(selector).gridstack; |
| 75 | + let node = { |
| 76 | + x: Math.round(6 * Math.random()), |
| 77 | + y: Math.round(5 * Math.random()), |
| 78 | + w: Math.round(1 + 1 * Math.random()), |
| 79 | + h: Math.round(1 + 1 * Math.random()), |
| 80 | + content: String(count++) |
| 81 | + }; |
| 82 | + subGrid.addWidget(node); |
| 83 | + return false; |
| 84 | + }; |
| 85 | + |
| 86 | + save = function(content = true, full = true) { |
| 87 | + options = grid.save(content, full); |
| 88 | + console.log(options); |
| 89 | + // console.log(JSON.stringify(options)); |
| 90 | + } |
| 91 | + destroy = function(full = true) { |
| 92 | + if (full) { |
| 93 | + grid.destroy(); |
| 94 | + grid = undefined; |
| 95 | + } else { |
| 96 | + grid.removeAll(); |
| 97 | + } |
| 98 | + } |
| 99 | + load = function(full = true) { |
| 100 | + if (full) { |
| 101 | + grid = GridStack.addGrid(document.querySelector('.container-fluid'), options); |
| 102 | + } else { |
| 103 | + grid.load(options); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + </script> |
| 108 | +</body> |
| 109 | +</html> |
0 commit comments