index.vue 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <template>
  2. <div ref="search">
  3. <!-- 动态渲染表单项 -->
  4. <a-form :model="formState" layout="inline">
  5. <a-form-item v-for="(item, index) in SearchForm" :key="index" :label="item.label" :field="item.field">
  6. <component :is="'a-' + item.type" v-model="formState[item.field].value" :placeholder="item.placeholder"
  7. allow-clear :style="{ width: item.width ? item.width + 'px' : '' }">
  8. <template v-if="item.type == 'select'">
  9. <a-option v-for="option in item.options" :key="option.value" :value="option.value">
  10. {{ option.label }}
  11. </a-option>
  12. </template>
  13. </component>
  14. </a-form-item>
  15. <a-form-item>
  16. <a-button type="primary" @click="handleQuery">查询</a-button>
  17. <a-button @click="handleReset" style="margin-left: 10px;">重置</a-button>
  18. </a-form-item>
  19. </a-form>
  20. </div>
  21. </template>
  22. <script setup>
  23. import { ref, defineProps, toRefs, onMounted, watch, watchEffect } from 'vue';
  24. import { Getdictionary } from "@/mixins/index.js";
  25. // 接收 props
  26. const props = defineProps({
  27. SearchForm: {
  28. type: Array,
  29. default: () => [
  30. {
  31. type: 'input',
  32. label: '字典名称',
  33. field: 'source',
  34. placeholder: '请输入字典名称',
  35. value: '', // 双向绑定的值
  36. },
  37. {
  38. type: 'input',
  39. label: '资费ID',
  40. field: 'trafficId',
  41. placeholder: '请输入资费ID',
  42. value: '', // 双向绑定的值
  43. },
  44. {
  45. type: 'select',
  46. label: '卡类型',
  47. field: 'simType',
  48. placeholder: '选择卡类型',
  49. options: [], // 默认空,后面会通过字典加载
  50. dict:'CardType',
  51. value: '', // 双向绑定的值
  52. width: '200'
  53. },
  54. ],
  55. },
  56. });
  57. const { SearchForm } = toRefs(props);
  58. const formState = ref({});
  59. SearchForm.value.forEach(item => {
  60. formState.value[item.field] = {
  61. value: item.value,
  62. };
  63. });
  64. // 字典加载
  65. const loadedDicts = ref({});
  66. const loadDictOptions = async (index, dict) => {
  67. if (loadedDicts.value[dict]) {
  68. SearchForm.value[index].options = loadedDicts.value[dict];
  69. return;
  70. }
  71. const dictionary = await Getdictionary(dict);
  72. loadedDicts.value[dict] = dictionary || [];
  73. SearchForm.value[index].options = loadedDicts.value[dict];
  74. };
  75. watch(
  76. () => SearchForm.value,
  77. async () => {
  78. for (let index = 0; index < SearchForm.value.length; index++) {
  79. const item = SearchForm.value[index];
  80. if (item.dict && !loadedDicts.value[item.dict]) {
  81. await loadDictOptions(index, item.dict);
  82. }
  83. }
  84. }, { immediate: true });
  85. // 查询操作
  86. const handleQuery = () => {
  87. // 将表单数据通过事件传递给父组件
  88. emit('query', formState.value);
  89. };
  90. </script>