Skip to content
Snippets Groups Projects
Commit e6553ad8 authored by Julian's avatar Julian
Browse files

Use v-model instead of defining items inside grid

parent 4853a40a
No related branches found
No related tags found
1 merge request!2Resolve "Implement basic functionality"
...@@ -4,7 +4,7 @@ import Grid from "../../src/Grid.vue"; ...@@ -4,7 +4,7 @@ import Grid from "../../src/Grid.vue";
<template> <template>
<div id="app"> <div id="app">
<Grid :rows="10" :cols="5" :pos-validation="blockField"> <Grid :rows="10" :cols="5" :pos-validation="blockField" v-model="items">
<div id="blocker"> <div id="blocker">
This field is blocked because it's filled, the next one programmatically This field is blocked because it's filled, the next one programmatically
</div> </div>
...@@ -22,6 +22,28 @@ export default { ...@@ -22,6 +22,28 @@ export default {
return key !== "obj8"; return key !== "obj8";
}, },
}, },
data() {
return {
items: [
{ x: 1, y: 3, w: 1, h: 1, key: "obj1", data: {} },
{ x: 2, y: 1, w: 1, h: 1, key: "obj2", data: {} },
{ x: 3, y: 1, w: 2, h: 2, key: "obj3", data: {} },
{ x: 5, y: 2, w: 1, h: 1, key: "obj4", data: {} },
{ x: 1, y: 1, w: 1, h: 2, key: "obj5", data: {} },
{ x: 5, y: 1, w: 1, h: 1, key: "obj6", data: {} },
{ x: 2, y: 2, w: 1, h: 3, key: "obj7", data: {} },
{
x: 1,
y: 4,
w: 1,
h: 1,
key: "obj8",
data: { title: "I'm blocked from moving!" },
},
{ x: 5, y: 3, w: 1, h: 1, key: "obj9", data: {} },
],
};
},
}; };
</script> </script>
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
<div id="highlight" ref="highlight"></div> <div id="highlight" ref="highlight"></div>
</slot> </slot>
<DragContainer <DragContainer
v-for="item in items" v-for="item in value"
:key="item.key" :key="item.key"
:drag-i-d="item.key" :drag-i-d="item.key"
:x="item.x" :x="item.x"
...@@ -30,6 +30,7 @@ export default { ...@@ -30,6 +30,7 @@ export default {
components: { components: {
DragContainer, DragContainer,
}, },
emits: ["input"],
props: { props: {
rows: { rows: {
type: Number, type: Number,
...@@ -43,6 +44,10 @@ export default { ...@@ -43,6 +44,10 @@ export default {
type: Function, type: Function,
required: false, required: false,
}, },
value: {
type: Array,
required: true,
},
}, },
methods: { methods: {
positionAllowed(x, y, key) { positionAllowed(x, y, key) {
...@@ -50,7 +55,7 @@ export default { ...@@ -50,7 +55,7 @@ export default {
if (x > this.cols) return false; if (x > this.cols) return false;
if (y > this.rows) return false; if (y > this.rows) return false;
for (let item of this.items) { for (let item of this.value) {
if (key === item.key) continue; if (key === item.key) continue;
if (x >= item.x && x < item.x + item.w) { if (x >= item.x && x < item.x + item.w) {
if (y >= item.y && y < item.y + item.h) { if (y >= item.y && y < item.y + item.h) {
...@@ -107,8 +112,9 @@ export default { ...@@ -107,8 +112,9 @@ export default {
if (!newPositionValid) return; if (!newPositionValid) return;
this.items.splice( let valueCopy = structuredClone(this.value);
this.items.findIndex((i) => { valueCopy.splice(
valueCopy.findIndex((i) => {
return i.key === element.key; return i.key === element.key;
}), }),
1 1
...@@ -116,7 +122,8 @@ export default { ...@@ -116,7 +122,8 @@ export default {
element.x = coords.x; element.x = coords.x;
element.y = coords.y; element.y = coords.y;
this.items.push(element); valueCopy.push(element);
this.$emit("input", valueCopy);
}, },
getCoords(x, y) { getCoords(x, y) {
return { return {
...@@ -125,21 +132,6 @@ export default { ...@@ -125,21 +132,6 @@ export default {
}; };
}, },
}, },
data() {
return {
items: [
{ x: 1, y: 3, w: 1, h: 1, key: "obj1", data: {} },
{ x: 2, y: 1, w: 1, h: 1, key: "obj2", data: {} },
{ x: 3, y: 1, w: 2, h: 2, key: "obj3", data: {} },
{ x: 5, y: 2, w: 1, h: 1, key: "obj4", data: {} },
{ x: 1, y: 1, w: 1, h: 2, key: "obj5", data: {} },
{ x: 2, y: 3, w: 1, h: 1, key: "obj6", data: {} },
{ x: 2, y: 2, w: 1, h: 3, key: "obj7", data: {} },
{ x: 1, y: 4, w: 1, h: 1, key: "obj8", data: {} },
{ x: 5, y: 3, w: 1, h: 1, key: "obj9", data: {} },
],
};
},
}; };
</script> </script>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment