DictSelection.vue 1.6 KB

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