@Ivan_Bachantcev

Как сделать так, чтобы определённый элемент перемещался?

Есть следующий код
<div id="app">
<div class='text_field_1'>
<input type='text'>
<button>вверх</button>
<div>
<div class='text_field_2'>
<input type='text'>
<button>вверх</button>
<div>
</div>

Вопрос такой: как при помощи Vue сделать возможность перемещения text_field_1 и text_field_2 с сохранением в них введённых значений?
  • Вопрос задан
  • 94 просмотра
Решения вопроса 1
0xD34F
@0xD34F Куратор тега Vue.js
Так, что ли:

data: () => ({
  items: [...Array(5).keys()],
}),
methods: {
  move(index, step) {
    const items = [...this.items];
    const newIndex = Math.min(items.length - 1, Math.max(0, index + step));
    [ items[index], items[newIndex] ] = [ items[newIndex], items[index] ];
    this.items = items;
  },

  // или

  move(index, step) {
    const { items } = this;
    const newIndex = Math.min(items.length - 1, Math.max(0, index + step));
    items.splice(index, 1, items.splice(newIndex, 1, items[index])[0]);
  },

  // или

  move(index, step) {
    const newIndex = Math.min(this.items.length - 1, Math.max(0, index + step));
    if (index !== newIndex) {
      const val = this.items[index];
      this.$set(this.items, index, this.items[newIndex]);
      this.$set(this.items, newIndex, val);
    }
  },
},

<div v-for="(n, i) in items">
  <input v-model="items[i]">
  <button @click="move(i, -1)">вверх</button>
  <button @click="move(i, +1)">вниз</button>
</div>

??
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы