view Select 实现全选、反选、取消

本文展示了如何在Vue项目中利用view Select组件实现全选、反选和取消选择的功能。通过示例代码,详细解释了实现这一交互效果的关键步骤,帮助开发者更好地理解和应用Vue的双向数据绑定及事件处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

view Select 实现全选、反选、取消

<template>
  <Row>
    <Col style="padding-right:10px">
  <Select v-model="model" multiple style="width:260px" @change="changeSelect" @remove-tag="removeTag">
      <Button :value="'全选'" :key="'全选'" @click.native="checkedAll" type="primary" size="small" style="margin-left: 10px;"> 全选</Button>
      <Button :value="'反选'" :key="'反选'" @click.native="checkedFan" type="success" size="small" style="margin-left: 10px;"> 反选</Button>
      <Button :value="'取消'" :key="'取消'" @click.native="unChecked" type="dashed" size="small" style="margin-left: 10px;"> 取消</Button>
    <Option v-for="item in cityList" :value="item.value" :key="item.value" >{{ item.label }}</Option>
  </Select>
  <div><p>已选择:{{model}}</p></div>
    </Col>
  </Row>
</template>
<script>
export default {
  data () {
    return {
      cityList: [
        {
          value: 'New York',
          label: 'New York'
        },
        {
          value: 'London',
          label: 'London'
        },
        {
          value: 'Sydney',
          label: 'Sydney'
        },
        {
          value: 'Ottawa',
          label: 'Ottawa'
        },
        {
          value: 'Paris',
          label: 'Paris'
        },
        {
          value: 'Canberra',
          label: 'Canberra'
        }
      ],
      // 是否全选
      isCheckAll: false,
      // 是否反选
      isCheckInverse: false,
      model: [],
      click:{},
    }

  },
  methods : {

    // 全选/取消全选
    checkedAll () {
      if (this.model.length < this.cityList.length){
        this.model = [];
        this.cityList.map((item) =>{
          this.model.push(item.label)
        })
        //this.model.unshift('全选')
      }else {
        this.model = [];
      }
    },
    checkedFan () {
        let model2 = [];
        for(let mm of this.cityList){
          if (this.model.indexOf(mm.label) === -1){
            model2.push(mm.label)
          }
        }
      this.model = [];
      this.model=model2;
    },
    unChecked () {
      this.model = [];
    },
    changeSelect (val){
      if(!val.include('全选') && val.length === this.cityList.length){
        this.model.unshift('全选')
      }else if (val.include('全选') && (val.length - 1) < this.cityList.length){
        this.model = this.model.filter((item) => {
          return item !== '全选'
        })
      }
      if(!val.include('反选') ||  val.length === this.cityList.length){
        this.model.unshift('反选')
      }else if (val.include('反选')){
        this.model = this.model.filter((item) => {
          return item !== '反选'
        })
      }
    },
    removeTag (val){
      if (val === '全选'){
        this.model = [];
      }
    }
  }
}
</script>

效果图展示:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值