af92aa25373e40662ec33e6dba0c5ae72bd3534e.svn-base 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. if (this.option.length != 0 && this.value in this.option[0] && this.label in this.option[0]) {
  47. this.selectOption.push({
  48. value: 'all',
  49. label: '全部'
  50. })
  51. this.option.map(item => {
  52. this.selectOption.push({
  53. value: item[this.value],
  54. label: item[this.label]
  55. })
  56. })
  57. this.selectOption.map(item => {
  58. this.searchSelect.push(item.value)
  59. this.oldSearchSelect.push(item.value)
  60. })
  61. }
  62. }
  63. },
  64. mounted() {
  65. // console.log(this.opeion);
  66. },
  67. methods: {
  68. // 下拉框选择
  69. selectChange() {
  70. // console.log(this.searchSelect);
  71. const oldIndexOfValue = this.oldSearchSelect.indexOf('all')
  72. const indexOfValue = this.searchSelect.indexOf('all')
  73. if (oldIndexOfValue != -1 && indexOfValue != -1) { // 两个都有ALL,则表示只是去掉了莫一项
  74. this.searchSelect.splice(indexOfValue, 1)
  75. this.isAll = false
  76. this.saveNewSearchSelect()
  77. } else if (oldIndexOfValue == -1 && indexOfValue != -1) { // 老数据没有all,新的有的,表明需要全选
  78. this.searchSelect.splice(0)
  79. this.oldSearchSelect.splice(0)
  80. this.isAll = true
  81. this.selectOption.map(item => {
  82. this.searchSelect.push(item.value)
  83. this.oldSearchSelect.push(item.value)
  84. })
  85. } else if (oldIndexOfValue != -1 && indexOfValue == -1) { // 老数据有all,但是新的数据没有all,表明需要全不选
  86. this.searchSelect.splice(0)
  87. this.oldSearchSelect.splice(0)
  88. this.isAll = false
  89. } else if (oldIndexOfValue == -1 && indexOfValue == -1) { // 两个都没有ALL,表示只是选择了一下,需要判断是不是都选上
  90. const isAllSelected = false
  91. const allOption = []
  92. this.selectOption.map(item => {
  93. if (item.value != 'all') {
  94. allOption.push(item.value)
  95. }
  96. })
  97. if (allOption.length == this.searchSelect.length) {
  98. this.isAll = true
  99. // this.searchSelect.push("all");
  100. this.searchSelect.splice(0, 0, 'all')
  101. this.saveNewSearchSelect()
  102. }
  103. }
  104. },
  105. // 保存原来的数据
  106. saveNewSearchSelect() {
  107. this.oldSearchSelect.splice(0)
  108. this.searchSelect.map(item => {
  109. this.oldSearchSelect.push(item)
  110. })
  111. },
  112. vselectChange() {
  113. console.log(this.searchSelect, '====777')
  114. this.$emit('searchSelect', this.searchSelect)
  115. }
  116. }
  117. }
  118. </script>
  119. <style lang="scss">
  120. #mySelect{
  121. .el-select__tags{
  122. height: 26px;
  123. overflow: hidden;
  124. // text-overflow: ellipsis;
  125. &>span{
  126. white-space: nowrap;
  127. overflow: hidden;
  128. display: block;
  129. }
  130. }
  131. .el-select .el-select__tags>span{
  132. overflow: hidden;
  133. text-overflow: ellipsis;
  134. display: inline-block!important;
  135. // width: 70%;
  136. max-width: 90%;
  137. }
  138. .el-tag.el-tag--info .el-tag__close{
  139. display: none;
  140. }
  141. }
  142. .isAll{
  143. .el-select .el-select__tags>span{
  144. span:nth-child(n+2){
  145. display: none;
  146. }
  147. }
  148. }
  149. </style>