当前位置:   article > 正文

el-select实现全选功能_el-select 全选

el-select 全选

思路:1、下拉选项增加全部,value为其余选项value的值用逗号拼接

 2、处理多选情况其他选项与全部选项的互斥功能

  1. <template>
  2. <div>
  3. <el-select v-model="selectedOptions" :multiple="multipleType" @change="selectedChange">
  4. <!-- <el-checkbox v-show="!selectAll" class="all-sele" v-model="selectAll" @change="toggleSelectAll">全部</el-checkbox> -->
  5. <el-option v-for="option in options" :key="option.value" :label="option.label" :value="option.value"></el-option>
  6. </el-select>
  7. </div>
  8. </template>
  9. <script>
  10. /* eslint-disable */
  11. export default {
  12. data() {
  13. return {
  14. // selectAll: false,
  15. multipleType:true,
  16. selectedOptions: [],
  17. options: [
  18. { label: '全部', value: '1,2,3' },
  19. { label: '选项1', value: '1' },
  20. { label: '选项2', value: '2' },
  21. { label: '选项3', value: '3' },
  22. ],
  23. };
  24. },
  25. computed: {
  26. },
  27. created(){
  28. },
  29. watch: { },
  30. methods: {
  31. selectedChange(val) {
  32. if(!this.multipleType){
  33. return false
  34. }
  35. let curData=this.options.find(a=>a.label=='全部')
  36. if(val.length>1&&val.includes(curData.value)){
  37. let allIndex=val.findIndex(a=>a==curData.value)
  38. if(allIndex==0){
  39. this.selectedOptions.splice(0,1)
  40. }else{
  41. this.selectedOptions=[curData.value]
  42. }
  43. }else if(val.join(',')==curData.value){
  44. this.selectedOptions=[curData.value]
  45. }
  46. }
  47. },
  48. };
  49. </script>
  50. <style lang="less" scoped>
  51. .all-sele /deep/ .el-checkbox__input{
  52. opacity: 0;
  53. }
  54. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/107264
推荐阅读