SelectChecked.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <template>
  2. <div class="select-checked">
  3. <!-- 下拉加多选框 -->
  4. <el-select
  5. v-model="value"
  6. multiple
  7. collapse-tags
  8. placeholder="请选择"
  9. :popper-append-to-body="false"
  10. @remove-tag="removeTag"
  11. size="small"
  12. style="width:384px;"
  13. >
  14. <el-option
  15. v-for="item in options"
  16. :key="item.value"
  17. :label="item.label"
  18. :value="item.value"
  19. >
  20. <el-checkbox v-model="item.check" @change="isCheck(item)">
  21. {{ item.label }}
  22. </el-checkbox>
  23. </el-option>
  24. </el-select>
  25. <!-- {{ value }} -->
  26. </div>
  27. </template>
  28. <script>
  29. export default {
  30. name: 'SelectChecked',
  31. components: {},
  32. props: {
  33. options: {
  34. type: Array
  35. }
  36. },
  37. data() {
  38. return {
  39. value: []
  40. }
  41. },
  42. watch:{
  43. options:{
  44. handler(newVal) {
  45. const allChecked = newVal.every(option => option.value == 'all' || option.check);
  46. const allOption = newVal.find(option => option.value == 'all');
  47. if (allOption) {
  48. allOption.check = allChecked;
  49. }
  50. },
  51. deep: true // 深度监听数组的内部对象变化
  52. }
  53. },
  54. methods: {
  55. // 多选框触发
  56. isCheck(item) {
  57. if (item.check && item.value == 'all') {
  58. this.value = []
  59. this.options.forEach(element => {
  60. element.check = true
  61. this.value.push(element.value)
  62. })
  63. } else if (!item.check && item.value == 'all') {
  64. this.value = []
  65. this.options.forEach(element => {
  66. element.check = false
  67. })
  68. }
  69. if (
  70. item.check &&
  71. this.value.indexOf(item.value) == -1 &&
  72. item.value !== 'all'
  73. ) {
  74. this.value.forEach((elm, idx) => {
  75. if (elm == 'all') {
  76. this.value.splice(idx, 1)
  77. }
  78. })
  79. this.value.push(item.value)
  80. if (this.value.length == this.options.length - 1) {
  81. this.options[0].check = true
  82. this.value.unshift('all')
  83. } else {
  84. this.options[0].check = false
  85. }
  86. } else if (!item.check && item.value !== 'all') {
  87. this.options[0].check = false
  88. this.value.forEach((elm, idx) => {
  89. if (elm == item.value || elm == 'all') {
  90. this.value.splice(idx, 1)
  91. }
  92. })
  93. }
  94. this.$emit('selectedVal', this.value)
  95. },
  96. // 多选模式下移除tag时触发
  97. removeTag(value) {
  98. if (value == 'all') {
  99. this.options.forEach((elm, idx) => {
  100. elm.check = false
  101. })
  102. this.value = []
  103. } else {
  104. this.options.forEach((elm, idx) => {
  105. if (elm.value == value || elm.value == 'all') {
  106. elm.check = false
  107. }
  108. })
  109. }
  110. this.$emit('selectedVal', this.value)
  111. },
  112. clearSelection() {
  113. this.value = [];
  114. this.options.forEach((elm, idx) => {
  115. elm.check = false
  116. })
  117. this.$emit('selectedVal', this.value);
  118. }
  119. }
  120. }
  121. </script>
  122. <style lang="scss">
  123. .select-checked {
  124. .el-select-dropdown.is-multiple .el-select-dropdown__item.selected::after {
  125. content: '';
  126. }
  127. .el-checkbox {
  128. width: 100%;
  129. padding: 0 30px;
  130. .el-checkbox__label {
  131. margin-left: 20px;
  132. }
  133. }
  134. .el-select-dropdown__item {
  135. padding: 0;
  136. }
  137. }
  138. </style>