Vue 实现单选和多选

日常工作中,UI 库中 单选/多选 组件并不一定非常符合业务需求。这时候,不妨自己动手搞一个。

单选

效果

在这里插入图片描述

思路

选中标识值与对象标识匹配时(本例使用索引),给所在标签添加选中样式;点击标签时,更改选中标识值。

代码

<template>
  <ul>
    <li 
      v-for="(city, index) in cityList"
      :key="index"
      :class="{ checked: index === checkedIndex }" 
      @click="changChecked(index)"
    >
    {{ city }}
    </li>
  </ul>
</template>

<script>
export default {
  name: 'Home',
  data () {
    return {
      checkedIndex: 0,
      cityList: ['上海', '北京', '广州', '西安']
    }
  },
  methods: {
    changChecked (index) {
      this.checkedIndex = index
    }
  }
}
</script>

<style lang="scss" scoped>
ul {
  padding: 0;

  li {
    display: inline-block;
    border: 1px solid #000;
    border-radius: 8px;
    margin-left: 5px;
    width: 80px;
    height: 50px;
    text-align: center;
    line-height: 50px;
    cursor: pointer;
    transition: all 0.3s linear;

    &.checked {
      border: 1px solid #fff;
      color: #fff;
      background-color: #0cf;
    }

    &:hover {
      border: 1px solid #fff;
      color: #fff;
      background-color: #0cf;
    }
  }
}
</style>

多选(2 种方法)

效果

在这里插入图片描述

方法一

思路

选中标识值为数组,标识值包含当前对像标识时(本例使用索引),给所在标签添加选中样式;点击标签时,若不包含选中标识,添加到数组;否则从数组从移除。

代码
<template>
  <ul>
    <li
      v-for="(city, index) in cityList" 
      :key="index"
      :class="{ checked: checkedList.includes(index) }"
      @click="changChecked(index)"
    >
      {{ city }}
    </li>
  </ul>
</template>

<script>
export default {
  name: 'Home2',
  data () {
    return {
      checkedList: [],
      cityList: ['上海', '北京', '广州', '西安'],
    }
  },
  methods: {
    changChecked (index) {
      if (this.checkedList.includes(index)) {
        this.checkedList = this.checkedList.filter(item => {
          return item !== index
        })
      } else {
        this.checkedList.push(index)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
ul {
  padding: 0; 
  list-style: none;

  li {
    display: inline-block;
    margin-left: 5px;
    width: 50px; 
    height: 30px; 
    text-align: center; 
    line-height: 30px;
    cursor: pointer;

    &::before { 
      display: inline-block; 
      margin-right: 2px; 
      border: 1px solid #000; 
      width: 10px; 
      height: 10px; 
      line-height: 10px; 
      content: ""; 
      transition: all 0.3s linear;
    }    

    &.checked { 
      color: #0CF;
    }

    &.checked::before { 
      border: 1px solid #fff;
      background-color: #0CF; 
    }
  }
}
</style>

方法二

思路

通过引入辅助变量实现。每个对象添加一个是否选中标识,若标识为 true,给所在标签添加选中样式;点击标签时,取反所在对象标识。

代码
<template>
  <ul>
    <li
      v-for="(item, index) in cityList" 
      :key="index"
      :class="{ checked: item.checked }"
      @click="changChecked(index)"
    >
      {{ item.city }}
    </li>
  </ul>
</template>

<script>
export default {
  name: 'Home3',
  data () {
    return {
      cityList: [
        {
          city: '上海',
          checked: false
        },
        {
          city: '北京',
          checked: false
        },
        {
          city: '广州',
          checked: false
        },
        {
          city: '西安',
          checked: false
        }
      ]
    }
  },
  methods: {
    changChecked (index) {
      this.cityList[index].checked = !this.cityList[index].checked
    }
  }
}
</script>

<style lang="scss" scoped>
ul {
  padding: 0; 
  list-style: none;

  li {
    display: inline-block;
    margin-left: 5px;
    width: 50px; 
    height: 30px; 
    text-align: center; 
    line-height: 30px;
    cursor: pointer;

    &::before { 
      display: inline-block; 
      margin-right: 2px; 
      border: 1px solid #000; 
      width: 10px; 
      height: 10px; 
      line-height: 10px; 
      content: ""; 
      transition: all 0.3s linear;
    }    

    &.checked { 
      color: #0CF;
    }

    &.checked::before { 
      border: 1px solid #fff;
      background-color: #0CF; 
    }
  }
}
</style>
  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值