当前位置:   article > 正文

el-select 实现多选和全选_el-select 多选

el-select 多选

实现效果:

打开页面时默认全选

代码:

  1. <template>
  2. <div>
  3. <el-form ref="form" label-width="80px" :model="test">
  4. <el-row :gutter="20">
  5. <el-col :span="6">
  6. <el-form-item label="测试">
  7. <el-select
  8. v-model="test.code"
  9. multiple
  10. collapse-tags
  11. v-bind="$attrs"
  12. v-on="$listeners"
  13. >
  14. <el-checkbox
  15. v-model="check"
  16. class="m-l-20"
  17. :indeterminate="test.code.length !== options.length"
  18. @change="selectAll"
  19. >
  20. 全选
  21. </el-checkbox>
  22. <el-option
  23. v-for="item in options"
  24. :key="item.value"
  25. :label="item.label"
  26. :value="item.value"
  27. />
  28. </el-select>
  29. </el-form-item>
  30. </el-col>
  31. </el-row>
  32. </el-form>
  33. </div>
  34. </template>
  35. ...
  36. data () {
  37. return {
  38. test: {
  39. code: ['选项1', '选项2', '选项3', '选项4', '选项5']
  40. },
  41. options: [
  42. { value: '选项1', label: '黄金糕' },
  43. { value: '选项2', label: '双皮奶' },
  44. { value: '选项3', label: '蚵仔煎' },
  45. { value: '选项4', label: '龙须面' },
  46. { value: '选项5', label: '北京烤鸭' }
  47. ]
  48. }
  49. },
  50. computed: {
  51. check: {
  52. get () {
  53. if (this.test.code.length === this.options.length) {
  54. return true
  55. }
  56. return false
  57. },
  58. set () {}
  59. }
  60. },
  61. methods: {
  62. ...
  63. selectAll (checked) {
  64. if (checked) {
  65. this.test.code = this.options.map(d => d.value)
  66. } else {
  67. this.test.code = []
  68. }
  69. }
  70. }

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