index.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <template>
  2. <div class="wrap selcetAll">
  3. <el-tooltip
  4. v-if="isShow"
  5. class="item"
  6. effect="dark"
  7. :content="labelStr"
  8. placement="top"
  9. ref="tooltip"
  10. >
  11. <div class="showDev"></div>
  12. </el-tooltip>
  13. <div class="select-all">
  14. <el-select
  15. multiple
  16. clearable
  17. filterable
  18. collapse-tags
  19. class="select"
  20. v-bind="{ ...$attrs, ...$props }"
  21. v-on:input="(value) => $emit('input', value)"
  22. @change="onChange"
  23. ref="select"
  24. >
  25. <slot />
  26. </el-select>
  27. <el-button @click="selectAll" class="btn" v-bind="{ ...$attrs, ...$props }">
  28. 全选
  29. </el-button>
  30. </div>
  31. </div>
  32. </template>
  33. <script>
  34. export default {
  35. name: "select-all",
  36. data() {
  37. return {
  38. isShow: false,
  39. labelStr: null,
  40. };
  41. },
  42. mounted() {
  43. this.$nextTick((res) => {
  44. this.isShow =
  45. (this.$refs?.select?.disabled || false) && this.labelStr?.length > 0;
  46. });
  47. },
  48. // computed: {
  49. // labelStr() {
  50. // const { selected } = this.$refs.select;
  51. // let labelStr = selected
  52. // .map((item) => (item.label ? item.label : item.value))
  53. // .join(",");
  54. // return labelStr;
  55. // },
  56. // },
  57. watch: {
  58. "$attrs.value": {
  59. immediate: true,
  60. handler(newVal, oldVal) {
  61. // 优化页面使用多组件时,tooltip不显示的问题
  62. this.$nextTick(() => {
  63. const selected = this.$refs.select?.selected;
  64. let labelStr = "";
  65. if (newVal && newVal.length > 0 && selected) {
  66. labelStr = selected
  67. .map((item) => (item.label ? item.label : item.value))
  68. .join(",");
  69. this.labelStr = labelStr;
  70. this.isShow =
  71. (this.$refs?.select?.disabled || false) &&
  72. this.labelStr?.length > 0;
  73. } else {
  74. this.labelStr = "";
  75. this.isShow =
  76. (this.$refs?.select?.disabled || false) &&
  77. this.labelStr?.length > 0;
  78. }
  79. });
  80. },
  81. },
  82. },
  83. methods: {
  84. selectAll() {
  85. const { options } = this.$refs.select;
  86. const values = options
  87. .filter((item) => !item.disabled && item.value)
  88. .map((item) => item.value);
  89. this.$emit("input", values);
  90. this.$emit("change", values);
  91. },
  92. onChange(val) {
  93. this.$emit("change", val);
  94. },
  95. },
  96. };
  97. </script>
  98. <style lang="scss">
  99. .el-tooltip__popper {
  100. max-width: 300px;
  101. line-height: 180%;
  102. }
  103. </style>
  104. <style lang="less" scoped>
  105. .wrap {
  106. position: relative;
  107. }
  108. .select-all {
  109. display: flex;
  110. flex-direction: row;
  111. align-items: center;
  112. .select {
  113. flex: 1;
  114. /deep/ .el-input__inner {
  115. border-radius: 4px 0 0 4px;
  116. }
  117. }
  118. }
  119. .btn {
  120. width: 50px;
  121. // padding: 0 10px;
  122. border-left: none;
  123. border-radius: 0 4px 4px 0;
  124. // height: 40px;
  125. }
  126. .item {
  127. width: 100%;
  128. position: absolute;
  129. z-index: 1000;
  130. // height: 40px;
  131. .showDev {
  132. width: 100%;
  133. }
  134. }
  135. </style>