N
N
nurdus2017-07-27 00:47:24
Vue.js
nurdus, 2017-07-27 00:47:24

Is it possible to not use getElementById in vue?

There is a 3 by 3 field (divs). each cell has x and y coordinates.
How to make it so that when you click on a cell, the cells adjacent to the edges change color.
Now I have it implemented like this (I use vue):
1) each cell has id = 'x_y' and has data-x, data-y;
2) when clicking on a div, I take x and y and calculate the coordinates of neighboring cells;
3) further on them (according to the coordinates of neighboring cells) I find the id and change the color.
It seems to me that it is not very 'correct' to access getElementById using vue.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Igor Koch, 2017-07-27
@nurdus

I would implement using a simple matrix, instead of a color, you can also take an object with many parameters {Color, size, etc.}
For example: https://jsfiddle.net/3tqmcg3a/2/

E
Evgeny Kulakov, 2017-07-27
@kulakoff Vue.js

Ignoring the markup, you should have something like this in your template:

<template v-for="cell in cells">
<cell :color="cell.color" @click="changeAround(cell)"></cell>
</template>

changeAround(cell) {
 // change cells color шт cells by using input cell

}

The meaning is this:
You have an array of cells cells.
You have a cell component and its input parameter is a color.
When a cell is clicked, a handler is called, which calculates, based on the cell data (serial number, position), neighboring cells whose color needs to be changed. Next, the template is reactively updated.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question