DictSelection.vue 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. filterArr: {
  42. type: Array,
  43. default: function () {
  44. return [];
  45. }
  46. }
  47. },
  48. data() {
  49. return {};
  50. },
  51. computed: {
  52. ...mapGetters(['dict', 'getDict']),
  53. dictList() {
  54. return this.filterArrFn(this.dict[dictEnum[this.dictName]]);
  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. filterArrFn(data) {
  76. let arr = [];
  77. if (data) {
  78. arr = data.filter((item) => !this.filterArr.includes(item.dictValue));
  79. }
  80. return arr;
  81. }
  82. }
  83. };
  84. </script>