product-component.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <template>
  2. <div>
  3. <el-select
  4. v-model="value"
  5. ref="select"
  6. placeholder="请选择"
  7. clearable
  8. filterable
  9. remote
  10. :remote-method="remoteMethod"
  11. @change = "onChange"
  12. @focus="cancelReadOnly"
  13. @hook:mounted="cancelReadOnly"
  14. @visible-change="cancelReadOnly">
  15. <el-option
  16. v-for="item in options"
  17. :key="item.value"
  18. :label="item.label"
  19. :value="item.value">
  20. </el-option>
  21. <el-option v-if="options.length > 0" label="加载更多" style="font-style: italic; color: #8a979e" value="undefined" @click.native="handleClick()"></el-option>
  22. </el-select>
  23. </div>
  24. </template>
  25. <script>
  26. export default {
  27. name: 'product-component',
  28. props: {
  29. productId: {
  30. type: String,
  31. default: ''
  32. }
  33. },
  34. model: {
  35. prop: 'productId',
  36. event: 'productSelected'
  37. },
  38. data () {
  39. return {
  40. value: '',
  41. current: 1,
  42. size: 10,
  43. name: '',
  44. options: [],
  45. loading: false,
  46. noMore: false
  47. }
  48. },
  49. mounted () {
  50. this.init()
  51. },
  52. watch: {
  53. cusId (value) {
  54. this.value = value
  55. }
  56. },
  57. methods: {
  58. async init () {
  59. this.getList()
  60. },
  61. remoteMethod (query) {
  62. this.options = []
  63. this.current = 1
  64. this.name = query
  65. this.getList()
  66. },
  67. getList () {
  68. this.$http({
  69. url: this.$http.adornUrl(`/biz-service/product/list`),
  70. method: 'get',
  71. params: this.$http.adornParams({
  72. 'current': this.current,
  73. 'size': this.size,
  74. 'productName': this.name
  75. })
  76. }).then(({data}) => {
  77. if (data && data.code === '200') {
  78. if (this.current > data.data.pages) {
  79. return
  80. }
  81. data.data.records.forEach(item => {
  82. this.options.push({
  83. label: item.productName,
  84. value: item.productId
  85. })
  86. })
  87. } else {
  88. this.options = []
  89. }
  90. })
  91. },
  92. handleClick () {
  93. this.loadMore()
  94. },
  95. loadMore () {
  96. this.current ++
  97. this.getList()
  98. },
  99. onChange (item) {
  100. if (item === 'undefined') {
  101. this.value = null
  102. }
  103. let obj = this.options.find((t) => t.value === item)
  104. this.$emit('productSelected', obj)
  105. },
  106. cancelReadOnly (onOff) {
  107. this.$nextTick(() => {
  108. if (!onOff) {
  109. const input = this.$refs.select.$el.querySelector('.el-input__inner')
  110. const timer = setTimeout(() => {
  111. input.removeAttribute('readonly')
  112. clearTimeout(timer)
  113. }, 200)
  114. }
  115. })
  116. }
  117. }
  118. }
  119. </script>
  120. <style scoped>
  121. </style>