Skip to content
Advertisement

How to create function that creates new grid-item onClick? Vuex using vue-grid-layout

I am using vue-grid-layout to create a draggable tile system, which will keep track of the order of steps to be executed by a system in our research lab. In short, I can create as many grid-item elements as I want by hard coding them, but I need to be able to create a function that will create/remove additional grid-items. I am stumped as to how to approach this since I suck at JavaScript, to create a grid-item in HTML I can do the following:

//these are just the ‘settings’ for the grid items//

JavaScript

I am essentially needing to know how I can click to add as many of these as I want, but I am unsure of how to program this. If I currently have this one grid-item, I need to press a button that will create another grid-item at :

JavaScript

-and would increment from 0 up through 1,2,3 etc. I have the states for the grid-layouts as follows:

JavaScript

Advertisement

Answer

You need to use v-for to loop over your data and create each of the items.

In the example below I haven’t used grid-layout or grid-item but the principle is exactly the same.

JavaScript
JavaScript

So in your case it would be:

JavaScript

You could potentially simplify this down to:

JavaScript

Relevant documentation for rendering lists:

https://v2.vuejs.org/v2/guide/list.html
https://v2.vuejs.org/v2/api/#v-for

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement