aec43fd8f3e99de7a68a7be53023e63f62d08fc2.svn-base 4.3 KB

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