code-select.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <template>
  2. <el-select v-model="selected" :placeholder="placeholder || '请选择'" @change="handleChange" :disabled="disabled">
  3. <template v-for="(item, _index) in datas">
  4. <el-option-group :key="'option-group-' + _index" :label="item.name" v-if="item.children && item.children.length > 0">
  5. <el-option
  6. v-for="(child, _index2) in item.children"
  7. :key="'option-item-' + _index + '-' + _index2"
  8. :label="child.name"
  9. :value="child.code"
  10. :disabled="child.status == '1'"
  11. ></el-option>
  12. </el-option-group>
  13. <el-option :key="'option-item-' + _index" :label="item.name" :value="item.code" :disabled="item.status == '1'" v-else></el-option>
  14. </template>
  15. </el-select>
  16. </template>
  17. <script>
  18. import { createNamespacedHelpers } from 'vuex';
  19. const { mapActions } = createNamespacedHelpers('naf/dict');
  20. export default {
  21. name: 'code-select',
  22. props: {
  23. value: { required: true },
  24. codeType: { type: String, required: true },
  25. placeholder: String,
  26. disabled: Boolean,
  27. mode: { type: String, default: 'code' }, // 选值模式:code、name、pair
  28. },
  29. data() {
  30. return {
  31. selected: this.value,
  32. datas: [],
  33. };
  34. },
  35. async mounted() {
  36. const res = await this.load(this.codeType);
  37. if (!res.errcode) {
  38. this.datas = res.data || res;
  39. } else {
  40. // eslint-disable-next-line no-console
  41. console.error(`数据字典[${this.codeType}]加载失败:`, res);
  42. }
  43. },
  44. methods: {
  45. ...mapActions(['load']),
  46. handleChange() {
  47. if (this.selected) {
  48. const items = this.datas || [];
  49. const item = items.find(p => p.code === this.selected);
  50. if (item && this.mode === 'name') {
  51. this.$emit('input', item.name);
  52. return;
  53. }
  54. if (this.mode === 'pair') {
  55. this.$emit('input', item);
  56. return;
  57. }
  58. }
  59. this.$emit('input', this.selected);
  60. // this.$emit('change', this.selected);
  61. },
  62. },
  63. };
  64. </script>