dict-select.vue 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <template>
  2. <div>
  3. <el-select
  4. v-model="value"
  5. ref="select"
  6. placeholder="请选择"
  7. filterable
  8. remote
  9. :remote-method="remoteMethod"
  10. @change = "onChange">
  11. <el-option
  12. v-for="item in options"
  13. :key="item.value"
  14. :label="item.label"
  15. :value="item.value">
  16. </el-option>
  17. </el-select>
  18. </div>
  19. </template>
  20. <script>
  21. export default {
  22. name: 'dict-select',
  23. props: ['dictId', 'dictType'],
  24. model: {
  25. prop: 'dictId',
  26. event: 'select'
  27. },
  28. watch: {
  29. dictId (newVal) {
  30. this.init(newVal, this.dictType)
  31. }
  32. },
  33. data () {
  34. return {
  35. value: '',
  36. name: '',
  37. options: [],
  38. loading: false
  39. }
  40. },
  41. mounted () {
  42. this.init(this.dictId, this.dictType)
  43. },
  44. methods: {
  45. async init (dictId, dictType) {
  46. this.options = []
  47. this.current = 1
  48. this.getList(dictId, dictType)
  49. },
  50. remoteMethod (query) {
  51. this.options = []
  52. this.name = query
  53. this.getList(this.dictId, this.dictType)
  54. },
  55. getList (dictId, dictType) {
  56. this.$http({
  57. url: this.$http.adornUrl(`/user-service/dict/queryByType`),
  58. method: 'get',
  59. params: this.$http.adornParams({
  60. 'type': dictType
  61. })
  62. }).then(({data}) => {
  63. if (data && data.code === '200') {
  64. data.data.forEach(item => {
  65. this.options.push({
  66. label: item.value,
  67. value: item.id
  68. })
  69. })
  70. if (dictId) {
  71. this.value = dictId
  72. }
  73. } else {
  74. this.options = []
  75. }
  76. })
  77. },
  78. onChange (item) {
  79. if (item === 'undefined') {
  80. this.value = null
  81. }
  82. this.$emit('select', item)
  83. }
  84. }
  85. }
  86. </script>
  87. <style scoped>
  88. </style>