Dynamic list + dynamic style (code) based on Vue bidirectional binding implementation

Source: Internet
Author: User
This article brings the content is about the Vue bidirectional binding implementation of dynamic List + dynamic style (code), there is a certain reference value, the need for friends can refer to, I hope to help you.

First on

Note: The following values can be obtained from other places, this way I'm writing dead.

On the logical diagram

Then the code
Template section

<template>    <div >        <div>            <span> selected option:</span>            <span v-for= "item in Selectlistlabel "style=" margin-left:10px; " >{{item}}</span>        </div>        //choose event bindings written in the outermost application of the JS event delegate, if you do not know can refer to my an article about the event delegation        < div @click = "Choose" v-for= "Item in List" >            <div  v-bind:class= "[Selectlistvalue.indexof (Item.value) >-1?activeclass: "]": Label= "Item.label": value= "Item.value" style= "margin:5px auto;width:100px;height:38px; border:1px solid #e9eaec; line-height:38px;border-radius:5px; " >                {{item.label}}            </div>        </div>            </div></template>

Script section

<script>export Default {name: ' HelloWorld ', data () {return {Selectlistlab                      El:[],//To show the value of the option selectlistvalue:[],//Display option list: [ In fact, this part of the data for background acquisition, now for the convenience of writing a few demo with {value: ' New York ', Label: ' New York '}, {value: ' L Ondon ', Label: ' London '}, {value: ' Sydney ', Label: ' Sydney '}, {value: ' Ottawa ', Label:                ' Ottawa '}, {value: ' Paris ', Label: ' Paris '}, {value: ' Canberra ', Label: ' Canberra '} ],}}, computed:{activeclass:function () {return ' Activ                    E '},}, methods:{Choose:function (e) {let dom = E.target;                    Gets the data bound on the DOM var domvalue = Dom.getattribute ("value"); var Domlabel = Dom.getattribute ("LabEl ");                    If the point is to a blank place if (Dom.getattribute ("label") = = null) {return; }//If the value of the clicked object is already inside the array, remove it from the array//or add him to the array if (Dom.getattribu                            Te ("class") = = "Active") {for (Let i = 0;i<this.selectlistvalue.length;i++) {                            if (this.selectlistvalue[i] = = Domvalue) {this.selectlistvalue.splice (i,1)                            }} for (Let i = 0;i<this.selectlistlabel.length;i++) { if (this.selectlistlabel[i] = = Domlabel) {this.selectlistlabel.spli CE (i,1)}}}else{This.select   Listvalue.push (Domvalue) This.selectlistlabel.push (Domlabel)}},    }}</script> 

Style section

<style scoped>   . active{        background-color: #0ccfbf;        Color:white;    } </style>

Note: Detailed description of the callout code,
Pits to note:

1.activeclass needs to return it in computed, otherwise it will not load the style. 2. How to operate an array, simply use the variant supported by Vue (otherwise vue cannot detect array changes and cannot dynamically bind) the official website cut a small section of the map

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: