index.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <template>
  2. <div class="selectButton">
  3. <div class="selectButton-div">
  4. <slot>
  5. <el-select v-model="selectValue" :placeholder="placeholder" :size="size"
  6. :clearable="clearable" :disabled="disabled" :multiple="multiple" :filterable="filterable" @change="change">
  7. <el-option v-for="(item,index) in optionArr" :key="'myButton'+ index" :label="item[prop.label]" :value="item[prop.value]">
  8. </el-option>
  9. </el-select>
  10. </slot>
  11. <el-button @click="handleClick" type="primary" :disabled="disabled" :size="size" class="selectButton-button">
  12. <slot name="button">
  13. <i class="el-icon-search"></i>
  14. </slot>
  15. </el-button>
  16. </div>
  17. </div>
  18. </template>
  19. <script>
  20. export default {
  21. props: {
  22. // 数据源(选择)
  23. optionArr: {
  24. type: Array,
  25. default: ()=>[],
  26. },
  27. prop: {
  28. default: () => {
  29. return {
  30. label: 'label',
  31. value:'value'
  32. }
  33. }
  34. },
  35. //v-model传过来的值(双向绑定的值)
  36. value: {
  37. default:''
  38. },
  39. // 尺寸
  40. size: {
  41. type: String,
  42. default: 'small'
  43. },
  44. // 提示内容
  45. placeholder: {
  46. type: String,
  47. default: ''
  48. },
  49. // 单选清空或不清空
  50. clearable: {
  51. type: Boolean,
  52. default: false
  53. },
  54. // 是否禁用
  55. disabled: {
  56. type: Boolean,
  57. default: false
  58. },
  59. // 多选
  60. multiple: {
  61. type: Boolean,
  62. default: false
  63. },
  64. // 是否可搜索
  65. filterable: {
  66. type: Boolean,
  67. default: false
  68. }
  69. },
  70. data() {
  71. return {
  72. selectValue: this.value
  73. }
  74. },
  75. watch: {
  76. },
  77. mounted() {
  78. this.init()
  79. },
  80. methods: {
  81. //初始化
  82. init() {
  83. // if (this.multiple) {
  84. // if (typeof this.value == 'object') {
  85. // } else {
  86. // this.selectValue = []
  87. // this.selectValue.push(this.value)
  88. // console.log('不是Array');
  89. // }
  90. // } else {
  91. // if (typeof this.value == 'string') {
  92. // } else {
  93. // this.selectValue = ''
  94. // this.selectValue = this.value
  95. // console.log('不是String');
  96. // }
  97. // }
  98. },
  99. //切换下拉框的值
  100. change(val) {
  101. this.$emit('input', val)
  102. },
  103. // 按钮点击事件
  104. handleClick(e) {
  105. this.$emit('click', e)
  106. },
  107. },
  108. }
  109. </script>
  110. <style lang="scss">
  111. .selectButton {
  112. .el-select {
  113. display: inline-block;
  114. position: relative;
  115. width: 100%;
  116. }
  117. .el-input__inner {
  118. color: #606266 !important;
  119. border-radius: 5px 0px 0px 5px;
  120. }
  121. }
  122. </style>
  123. <style lang="scss" scoped>
  124. .selectButton {
  125. .selectButton-div {
  126. width: calc(100%);
  127. display: flex;
  128. align-items: center;
  129. }
  130. .selectButton-button {
  131. border-radius: 0px 5px 5px 0px;
  132. }
  133. }
  134. </style>