DictSelection.vue 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <template>
  2. <el-select
  3. v-model="selectVal"
  4. filterable
  5. clearable
  6. style="width: 100%"
  7. v-bind="$attrs"
  8. v-on="$listeners"
  9. >
  10. <el-option
  11. v-for="item in dictList"
  12. :key="item[valueName]"
  13. :value="item[valueName]"
  14. :label="item[labelName]"
  15. ></el-option>
  16. </el-select>
  17. </template>
  18. <script>
  19. import dictEnum from '@/enum/dict';
  20. import { mapActions, mapGetters } from 'vuex';
  21. export default {
  22. model: {
  23. prop: 'value',
  24. event: 'updateVal'
  25. },
  26. props: {
  27. value: {
  28. type: [String, Number],
  29. default: ''
  30. },
  31. dictName: {
  32. type: String,
  33. required: true
  34. },
  35. labelName: {
  36. type: String,
  37. default: 'dictValue'
  38. },
  39. valueName: {
  40. type: String,
  41. default: 'dictCode'
  42. }
  43. },
  44. data () {
  45. return {};
  46. },
  47. computed: {
  48. ...mapGetters(['dict', 'getDict']),
  49. dictList () {
  50. return this.dict[dictEnum[this.dictName]] || [];
  51. },
  52. selectVal: {
  53. set (val) {
  54. this.$emit('updateVal', val);
  55. // change获取选中项所有数据
  56. this.$emit('itemChange', this.getDict(this.dictName, val));
  57. },
  58. get () {
  59. return this.value;
  60. }
  61. }
  62. },
  63. created () {
  64. if (this.dictName) {
  65. this.requestDict(this.dictName);
  66. }
  67. },
  68. methods: {
  69. ...mapActions('dict', ['requestDict'])
  70. }
  71. };
  72. </script>