Home >Web Front-end >JS Tutorial >vue.js method to move the array position and update the view at the same time

vue.js method to move the array position and update the view at the same time

亚连
亚连Original
2018-05-31 16:49:211713browse

Below I will share with you a method of moving the array position and updating the view in vue.js. It has a good reference value and I hope it will be helpful to everyone.

Use vue.js v-for to bind several options, and you need to sort the options and move them up and down.

It is necessary to exchange the position of the array in options. It is usually written like this:

Assume moving forward one:

var index = this.options.indexOf(option); //获取当前选项对象在数组里面的索引。
var tempOption = this.options[index-1]; //存储前一个
this.options[index-1] = option;(this.options[index])
this.options[index] = tempOption;

This does change the order of the array, but the view is not updated and moved. For details, see the description of the array on the vue official website.

One of the solutions is to change its object and use the set method of vue:

var index = options.indexOf(option); 
var tempOption = options[index - 1]; 
Vue.set(options, index - 1, options[index]); 
Vue.set(options, index, tempOption);

The above is what I compiled for everyone. , I hope it will be helpful to everyone in the future.

Related articles:

Implementing todolist through vue vuex (detailed tutorial)

Use vue to implement how to display in the table , take the id of each row (detailed tutorial)

How to implement the QQ side menu using the vue mobile UI framework (detailed tutorial)

The above is the detailed content of vue.js method to move the array position and update the view at the same time. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn