el-select加一个全选功能
标签搜索

el-select加一个全选功能

洛尘
2022-07-23 / 2 评论 / 404 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2022年07月23日,已超过1049天没有更新,若内容或图片失效,请留言反馈。

html部分

<el-select v-model="ruleForm.staff_attendance_name" clearable multiple placeholder="请选择" @change="selectAll" @remove-tag="removeTag">
  <el-option label="全选" value="全选" ></el-option>
  <el-option v-if="ruleForm.staff_attendance_name!=['全选']" v-for="item in attendanceTeamInfo" :key="item.name" :label="item.name"
                      :value="item.name">
  </el-option>
</el-select>

js部分

//考勤地点点击全选
selectAll(val) {
  var inx=val.findIndex(item=>item=='全选');
  if(this.allCheck&&inx>-1){ //如果是全选,并且剩余的数组里有全选,就把全选删了
    if(inx>-1){
      val.splice(inx,1)
    }
    this.allCheck=false;
  }else{
    if(inx>-1){
      this.allCheck=true;
      var arr=['全选'];
      this.attendanceTeamInfo.forEach(ele=>{
        arr.push(ele.name);
        this.ruleForm.staff_attendance_name=arr;
      })
    }else if(val.length==this.attendanceTeamInfo.length&&this.allCheck){
      this.ruleForm.staff_attendance_name=[];
      this.allCheck=false;
    }else if(val.length==this.attendanceTeamInfo.length){
      this.allCheck=true;
      this.ruleForm.staff_attendance_name.unshift('全选')
    }
  }
},
//移除全选时,清空选项
removeTag(val){
  if(val=='全选'){
    this.ruleForm.staff_attendance_name=[]
  }
},
0

评论 (2)

取消
  1. 头像
    mxluoweknl
    Windows 10 · Google Chrome

    真好呢

    回复
  2. 头像
    qoacomvdil
    Windows 10 · Google Chrome

    真好呢

    回复