El-select多選択全選択





export default {
  name: 'selectMultiple',
  data () {
    return {
      array: [],
      selectVal: [],
      oldSelectVal: []
    }
  },
  mounted () {
    this.initSelect()
  },
  methods: {
    initSelect () {
      this.array = [{label: '  ', value: 'ALL'}]
      for (let i = 0; i < 10; i++) {
        this.array.push({
          label: '   ' + i,
          value: i
        })
      }
    },
    handleSelect (val) {
      let selectVal = this.selectVal
      let oldSelectVal = this.oldSelectVal
      let arr = this.array
      let allValues = []
      //      
      for (let item of arr) {
        allValues.push(item.value)
      }
      if (val.includes('ALL')) {
        if (!oldSelectVal.includes('ALL')) {
          //   
          selectVal = allValues
        } else {
          //     ,      ,    
          const index = val.indexOf('ALL')
          val.splice(index, 1)
          selectVal = val
        }
      } else {
        if (oldSelectVal.includes('ALL')) {
          //     
          selectVal = []
        } else if (allValues.length - 1 === val.length) {
          //     ,        ,    
          selectVal = allValues
        }
      }
      this.selectVal = selectVal
      this.oldSelectVal = selectVal
    }
  }
}