2022-08-22T00:38:07.png

记录一下实现标签多选并且选中后可以取消选中效果,可以直接复制代码使用(因为这个是vant显示弹窗,如果没安装请自行去掉van-popup

就是简单的索引判断是否有重复值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<template> <div class="interview"> <van-popup v-model="interviewFeeling" position="bottom" closeable custom-style="height: 20%;" bind:close="onClose" :round="true" > <div class="cancel-popup"> <div class="cancel-popup-title"> 面试记录<span>(求职方不可见)</span> </div> <div class="cancel-popup-content">标签</div> <div class="tags"> <!-- 主要代码 --> <div class="tags-select" :class="{ 'tags-select-active': spanIndex.indexOf(index) > -1 }" v-for="(item, index) in feelingData" :key="item.id" @click="feelingClick(item, index)" > {{ item.value }} </div> </div> </div> </van-popup> </div> </template> <script> export default { data() { return { spanIndex: [], interviewFeeling: true, feelingData: [ { value: '测试1', id: 1 }, { value: '测试2', id: 2 }, { value: '测试3', id: 3 } ], } }, methods: { feelingClick(item, index) { // 获取你点击时的索引,如果找不到这个索引会为-1 let arrIndex = this.spanIndex.indexOf(index) // 如果没有这个索引,则push进去 // 有这个索引那么删除掉(取消选中) // 上面:class选中颜色同理 if (arrIndex > -1) { this.spanIndex.splice(arrIndex, 1) } else { this.spanIndex.push(index) } }, } } </script> <style lang="scss" scoped> .cancel-popup { padding: 0.16rem; .tags { .tags-select { background: rgba(238, 238, 238, 0.5); border-radius: 0.03rem; padding: 0.08rem 0.16rem; display: inline-block; margin: 0 0.12rem 0.12rem 0; } .tags-select-active { background: rgba(0, 215, 110, 0.2) !important; color: #00d76e !important; } } .cancel-popup-content { font-size: 14px; font-weight: bold; color: #333333; margin: 0.16rem 0 0.12rem 0; } .cancel-popup-title { font-size: 0.16rem; font-weight: bold; color: #000000; span { margin-left: 0.04rem; font-size: 0.1rem; font-weight: 400; color: rgba(0, 0, 0, 0.25); } } } </style>

仅有一条评论

  1. 大佬带带我

发表评论