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