<template> <div> <span style="margin-left:30px;font-weight:bolder;">教练: <el-select v-model="staffId" placeholder="请选择" multiple collapse-tags @change="changeStaff" style="width:180px"> <el-option v-for="(item, key) in staff" :key="key" :label="item.label" :value="item.value"> </el-option> </el-select> </span> </div> </template>
<script> export default { data() { return { staffId: [-1], isContainAll: true, staffNmae: [], staff: [ { 'value': -1, 'nameCn': '所有' }, { 'value': 1, 'nameCn': '张三' }, { 'value': 2, 'nameCn': '李四'}, { 'value': 3, 'nameCn': '王五' }, { 'value': 4, 'nameCn': '潇潇' }, { 'value': 5, 'nameCn': '小美'}, { 'value': 6, 'nameCn': '赵琴' }, { 'value': 7, 'nameCn': '张玲' } ] } }, methods: { // 定义一个变量,用来存储当前下拉框是否选中了所有 if (this.isContainAll) { // 只有下拉框的值发生了变化才会进入此方法 // 若是以前选中了所有,则变化后确定不包含所有了 this.isContainAll = false // 则删除第一个所有 this.staffId.splice(0, 1) } else { // 若是以前没有选中所有 // 判断这次是否选中了所有 this.isContainAll = this.staffId.some(value => value === -1) // 若是这次选中了所有 if (this.isContainAll) { // 则去除其余,只保留所有,默认value=-1 是所有 this.staffId = [-1] } else { // 若是当前不包含所有,则判断是否其余的七个日期全选了 if (this.staffId.length === this.staff.length - 1) { // 若是其余员工全选了,则也将当前置为所有 this.staffId = [-1] this.isContainAll = true } } } // 当没有选中任何教练时,将当前置为所有 if (this.staffId.length === 0) { this.staffId = [-1] this.isContainAll = true } // 若是选择所有 if (this.isContainAll === true) { this.staffName = ['所有'] } else { // 得到选中教练的姓名 for (let i = 0; i < this.staffId.length; i++) { let obj = this.staff.find((item) => { return item.value === this.staffId[i] }) this.$set(this.staffName, i, obj.label) } } } </script>