craft-product-component.vue 3.0 KB

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