vue3 el-select 增加全选和取消全选

最近有一个el-option增加全选的需求,后面网上找到了关于vue2的写法,这里我用的是vue3的写法。首先,先上一下效果图

vue3 el-select 增加全选和取消全选_第1张图片vue3 el-select 增加全选和取消全选_第2张图片

选中和未选中效果图已经上传,现在贴上代码。

全选功能

const selectedArray = ref([])
const checked = ref(false)
const options = [
  { name: '哈哈哈', label: 'one' },
  { name: '好好好', label: 'tow' },
  { name: '你好', label: 'three' },
  { name: '嘎嘎', label: 'four' },
  { name: '么么么', label: 'five' }
]
const selectAll = () => {
  if (selectedArray.value.length {
      selectedArray.value.push(item.name)
    })
    selectedArray.value.unshift('全选')
  } else {
    selectedArray.value = []
  }
}
const changeSelect = (val) => {
  console.log(val)
  console.log(options,'1231')
  if (!val.includes('全选') && val.length === options.length) {
    selectedArray.value.unshift('全选')
  } else if (val.includes('全选') && (val.length - 1) < options.length) {
    selectedArray.value = selectedArray.value.filter((item) => {
      return item !== '全选'
    })
  }
}
const removeTag = (val) => {
  if (val === '全选') {
    selectedArray.value = []
  }
}

你可能感兴趣的:(前端,vue.js,javascript)