Example09DynamicAddRemove.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <template>
  2. <div>
  3. <div class="layoutJSON">
  4. Displayed as <code>[x, y, w, h]</code>:
  5. <div class="columns">
  6. <div class="layoutItem" v-for="item in layout" :key="item.i">
  7. <b>{{item.i}}</b>: [{{item.x}}, {{item.y}}, {{item.w}}, {{item.h}}]
  8. </div>
  9. </div>
  10. </div>
  11. <button @click="addItem">Add an item dynamically</button>
  12. <input type="checkbox" v-model="draggable" /> Draggable
  13. <input type="checkbox" v-model="resizable" /> Resizable
  14. <grid-layout :layout.sync="layout"
  15. :col-num="colNum"
  16. :row-height="30"
  17. :is-draggable="draggable"
  18. :is-resizable="resizable"
  19. :vertical-compact="true"
  20. :use-css-transforms="true"
  21. >
  22. <grid-item v-for="item in layout"
  23. :static="item.static"
  24. :x="item.x"
  25. :y="item.y"
  26. :w="item.w"
  27. :h="item.h"
  28. :i="item.i"
  29. >
  30. <span class="text">{{item.i}}</span>
  31. <span class="remove" @click="removeItem(item.i)">x</span>
  32. </grid-item>
  33. </grid-layout>
  34. </div>
  35. </template>
  36. <script>
  37. import { GridLayout, GridItem } from "vue-grid-layout"
  38. export default {
  39. components: {
  40. GridLayout,
  41. GridItem
  42. },
  43. data() {
  44. return {
  45. layout: [
  46. { x: 0, y: 0, w: 2, h: 2, i: "0" },
  47. { x: 2, y: 0, w: 2, h: 2, i: "1" },
  48. { x: 4, y: 0, w: 2, h: 2, i: "2" },
  49. { x: 6, y: 0, w: 2, h: 2, i: "3" },
  50. { x: 8, y: 0, w: 2, h: 2, i: "4" },
  51. ],
  52. draggable: true,
  53. resizable: true,
  54. colNum: 12,
  55. index: 0,
  56. }
  57. },
  58. mounted() {
  59. // this.$gridlayout.load();
  60. this.index = this.layout.length;
  61. },
  62. methods: {
  63. addItem: function () {
  64. // Add a new item. It must have a unique key!
  65. this.layout.push({
  66. x: (this.layout.length * 2) % (this.colNum || 12),
  67. y: this.layout.length + (this.colNum || 12), // puts it at the bottom
  68. w: 2,
  69. h: 2,
  70. i: this.index,
  71. });
  72. // Increment the counter to ensure key is always unique.
  73. this.index++;
  74. },
  75. removeItem: function (val) {
  76. const index = this.layout.map(item => item.i).indexOf(val);
  77. this.layout.splice(index, 1);
  78. },
  79. }
  80. }
  81. </script>
  82. <style>
  83. .layoutJSON {
  84. background: #ddd;
  85. border: 1px solid black;
  86. margin-top: 10px;
  87. padding: 10px;
  88. }
  89. .columns {
  90. -moz-columns: 120px;
  91. -webkit-columns: 120px;
  92. columns: 120px;
  93. }
  94. /*************************************/
  95. .remove {
  96. position: absolute;
  97. right: 2px;
  98. top: 0;
  99. cursor: pointer;
  100. }
  101. .vue-grid-layout {
  102. background: #eee;
  103. }
  104. .vue-grid-item:not(.vue-grid-placeholder) {
  105. background: #ccc;
  106. border: 1px solid black;
  107. }
  108. .vue-grid-item .resizing {
  109. opacity: 0.9;
  110. }
  111. .vue-grid-item .static {
  112. background: #cce;
  113. }
  114. .vue-grid-item .text {
  115. font-size: 24px;
  116. text-align: center;
  117. position: absolute;
  118. top: 0;
  119. bottom: 0;
  120. left: 0;
  121. right: 0;
  122. margin: auto;
  123. height: 100%;
  124. width: 100%;
  125. }
  126. .vue-grid-item .no-drag {
  127. height: 100%;
  128. width: 100%;
  129. }
  130. .vue-grid-item .minMax {
  131. font-size: 12px;
  132. }
  133. .vue-grid-item .add {
  134. cursor: pointer;
  135. }
  136. .vue-draggable-handle {
  137. position: absolute;
  138. width: 20px;
  139. height: 20px;
  140. top: 0;
  141. left: 0;
  142. background: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='10'><circle cx='5' cy='5' r='5' fill='#999999'/></svg>") no-repeat;
  143. background-position: bottom right;
  144. padding: 0 8px 8px 0;
  145. background-repeat: no-repeat;
  146. background-origin: content-box;
  147. box-sizing: border-box;
  148. cursor: pointer;
  149. }
  150. </style>