自动生成标签点击变色多选

html代码
1 <div class="title-btn-div"> 2 <span class="btn-span">部门</span> 3 <el-button v-for="(item,index) of tags" 4 :key="index" size="mini" round 5 @click="selectTag(index,item.tagId)" :class="{buttonStyle1:arr[index]}" 6 >{{item.tagName}} 7 </el-button> 8 </div>
data部分
const newTags = [ {tagId: 1, tagName: "线路室"}, {tagId: 2, tagName: "变电室"}, {tagId: 3, tagName: "技经室"}, ];
data() { return { tags: newTags, arr: [], tagIds: [], lock: true, // 锁
}
}
js部分
1 mounted() { 2 this.getTags() 3 }, 4 methods: { 5 getTags() { 6 for (let i = 0; i < this.tags.length; i++) { 7 this.arr.push(false); 8 } 9 }, 10 selectTag(index, tagId) { 11 this.lock = true; 12 for (let i = 0; i < this.tagIds.length; i++) { 13 if (this.tagIds[i] == tagId) { 14 this.tagIds.splice(i, 1); 15 this.lock = false; 16 } 17 } 18 for (let i = 0; i < this.arr.length; i++) { 19 if (i == index) { 20 this.arr[index] ? this.arr.splice(index, 1, false) : this.arr.splice(index, 1, true); 21 } 22 } 23 if (this.lock) { 24 this.tagIds.push(tagId); 25 } 26 } 27 }
css部分
1 .title-btn-div .el-button { 2 font-family: Avenir; 3 margin-right: 10px; 4 width: 120px; 5 border: 1px dashed #1e87f0; 6 color: #1e87f0; 7 background-color: white 8 } 9 .buttonStyle1 { 10 font-family: Avenir; 11 margin-right: 10px!important; 12 width: 120px!important; 13 border: 1px solid #1e87f0!important; 14 color: white!important; 15 background-color: #1e87f0!important; 16 }

浙公网安备 33010602011771号