index.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <template>
  2. <div id="mySelect" :class="isAll?'isAll':''">
  3. <el-select
  4. v-model="searchSelect"
  5. multiple
  6. filterable
  7. placeholder="请选择"
  8. popper-class="cheng"
  9. @change="selectChange"
  10. @visible-change="vselectChange"
  11. >
  12. <el-option v-for="(item,index) in selectOption" :key="index" :label="item.label" :value="item.value" />
  13. </el-select>
  14. <!-- <el-option v-for="(item,index) in selectOption" :key="item.label+index" :label="item.label" :value="item.value" /> -->
  15. </div>
  16. </template>
  17. <script>
  18. export default {
  19. name: 'MySelect',
  20. components: {},
  21. props: {
  22. option: { // 传输过来的数组
  23. type: Array,
  24. default: []
  25. },
  26. label: { // 做一个字典,显示的值
  27. type: String,
  28. default: 'label'
  29. },
  30. value: { // 做一个字典,实际的值
  31. type: String,
  32. default: 'value'
  33. },
  34. },
  35. data() {
  36. return {
  37. searchSelect: [], // 下拉选择框中的内容
  38. oldSearchSelect: [], // 原来的下拉框中的内容
  39. selectOption: [],
  40. isAll: true, // 是不是全选中了
  41. checkedIds: []
  42. }
  43. },
  44. computed: {},
  45. watch: {
  46. option() {
  47. if (this.option.length != 0 && this.value in this.option[0] && this.label in this.option[0]) {
  48. this.selectOption = []
  49. this.selectOption.push({
  50. value: 'all',
  51. label: '全部'
  52. })
  53. this.option.map(item => {
  54. this.selectOption.push({
  55. value: item[this.value],
  56. label: item[this.label]
  57. })
  58. })
  59. let arrs = []
  60. let oldarrs = []
  61. this.selectOption.map(item => {
  62. arrs.push(item.value)
  63. oldarrs.push(item.value)
  64. })
  65. this.searchSelect = arrs
  66. this.oldSearchSelect = oldarrs
  67. }
  68. }
  69. },
  70. mounted() {
  71. // console.log(this.opeion);
  72. },
  73. methods: {
  74. // 下拉框选择
  75. selectChange() {
  76. // console.log(this.searchSelect);
  77. const oldIndexOfValue = this.oldSearchSelect.indexOf('all')
  78. const indexOfValue = this.searchSelect.indexOf('all')
  79. if (oldIndexOfValue != -1 && indexOfValue != -1) { // 两个都有ALL,则表示只是去掉了莫一项
  80. this.searchSelect.splice(indexOfValue, 1)
  81. this.isAll = false
  82. this.saveNewSearchSelect()
  83. } else if (oldIndexOfValue == -1 && indexOfValue != -1) { // 老数据没有all,新的有的,表明需要全选
  84. this.searchSelect.splice(0)
  85. this.oldSearchSelect.splice(0)
  86. this.isAll = true
  87. this.selectOption.map(item => {
  88. this.searchSelect.push(item.value)
  89. this.oldSearchSelect.push(item.value)
  90. })
  91. } else if (oldIndexOfValue != -1 && indexOfValue == -1) { // 老数据有all,但是新的数据没有all,表明需要全不选
  92. this.searchSelect.splice(0)
  93. this.oldSearchSelect.splice(0)
  94. this.isAll = false
  95. } else if (oldIndexOfValue == -1 && indexOfValue == -1) { // 两个都没有ALL,表示只是选择了一下,需要判断是不是都选上
  96. const isAllSelected = false
  97. const allOption = []
  98. this.selectOption.map(item => {
  99. if (item.value != 'all') {
  100. allOption.push(item.value)
  101. }
  102. })
  103. if (allOption.length == this.searchSelect.length) {
  104. this.isAll = true
  105. // this.searchSelect.push("all");
  106. this.searchSelect.splice(0, 0, 'all')
  107. this.saveNewSearchSelect()
  108. }
  109. }
  110. },
  111. // 保存原来的数据
  112. saveNewSearchSelect() {
  113. this.oldSearchSelect.splice(0)
  114. this.searchSelect.map(item => {
  115. this.oldSearchSelect.push(item)
  116. })
  117. },
  118. vselectChange() {
  119. // console.log(this.searchSelect, '====777')
  120. this.$emit('searchSelect', this.searchSelect)
  121. }
  122. }
  123. }
  124. </script>
  125. <style lang="scss">
  126. #mySelect{
  127. .el-select__tags{
  128. height: 26px;
  129. overflow: hidden;
  130. // text-overflow: ellipsis;
  131. &>span{
  132. white-space: nowrap;
  133. overflow: hidden;
  134. display: block;
  135. }
  136. }
  137. .el-select .el-select__tags>span{
  138. overflow: hidden;
  139. text-overflow: ellipsis;
  140. display: inline-block!important;
  141. // width: 70%;
  142. max-width: 90%;
  143. }
  144. .el-tag.el-tag--info .el-tag__close{
  145. display: none;
  146. }
  147. }
  148. .isAll{
  149. .el-select .el-select__tags>span{
  150. span:nth-child(n+2){
  151. display: none;
  152. }
  153. }
  154. }
  155. </style>