09-dynamic-add-remove.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. var testLayout = [
  2. { x: 0, y: 0, w: 2, h: 2, i: "0" },
  3. { x: 2, y: 0, w: 2, h: 2, i: "1" },
  4. { x: 4, y: 0, w: 2, h: 2, i: "2" },
  5. { x: 6, y: 0, w: 2, h: 2, i: "3" },
  6. { x: 8, y: 0, w: 2, h: 2, i: "4" },
  7. ];
  8. // var GridLayout = VueGridLayout.GridLayout;
  9. // var GridItem = VueGridLayout.GridItem;
  10. new Vue({
  11. el: "#app",
  12. // components: {
  13. // "GridLayout": GridLayout,
  14. // "GridItem": GridItem
  15. // },
  16. data: {
  17. layout: testLayout,
  18. draggable: true,
  19. resizable: true,
  20. responsive: true,
  21. colNum: 12,
  22. index: 0,
  23. },
  24. mounted: function () {
  25. this.index = this.layout.length;
  26. },
  27. methods: {
  28. addItem: function () {
  29. // Add a new item. It must have a unique key!
  30. this.layout.push({
  31. x: (this.layout.length * 2) % (this.colNum || 12),
  32. y: this.layout.length + (this.colNum || 12), // puts it at the bottom
  33. w: 2,
  34. h: 2,
  35. i: this.index,
  36. });
  37. // Increment the counter to ensure key is always unique.
  38. this.index++;
  39. },
  40. removeItem: function (val) {
  41. const index = this.layout.map(item => item.i).indexOf(val);
  42. this.layout.splice(index, 1);
  43. },
  44. },
  45. });