format-rule-dict.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <template>
  2. <CusDialog
  3. :show="show"
  4. :title="titleCpt"
  5. @onClose="$emit('update:show', false)"
  6. width="80%"
  7. height="80%"
  8. @onSubmit="onSubmit"
  9. :loading="state.loading"
  10. >
  11. <div class="__cus-dialog-form">
  12. <div style="margin-bottom: 10px; display: flex">
  13. <CusButton type="main" title="新增" @click="state.data.push({key: '', value: ''})"/>
  14. </div>
  15. <CusForm ref="ref_form" label-width="100px">
  16. <CusTable
  17. :data="state.data"
  18. :table-head="state.tableHead"
  19. :no-page="true"
  20. >
  21. <template #key-column-value="{scope}">
  22. <CusFormColumn
  23. class="__cus-table-form-column"
  24. required
  25. :span="24"
  26. v-model:param="scope.row.key"
  27. />
  28. </template>
  29. <template #value-column-value="{scope}">
  30. <CusFormColumn
  31. class="__cus-table-form-column"
  32. required
  33. :span="24"
  34. v-model:param="scope.row.value"
  35. />
  36. </template>
  37. <template #do-column-value="{scope}">
  38. <CusButton type="table-del" @click="state.data.splice(scope.$index, 1)"/>
  39. </template>
  40. </CusTable>
  41. </CusForm>
  42. </div>
  43. </CusDialog>
  44. </template>
  45. <script setup lang="ts">
  46. import {computed, getCurrentInstance, nextTick, reactive, ref, watch} from "vue";
  47. import {useDictionaryStore} from "@/stores";
  48. import {ElMessage, ElMessageBox} from "element-plus";
  49. const emit = defineEmits(['update:show', 'dict'])
  50. const {proxy} = getCurrentInstance()
  51. const DictionaryStore = useDictionaryStore()
  52. const props = defineProps({
  53. show: {default: false},
  54. transfer: {}
  55. })
  56. const state: any = reactive({
  57. tableHead: [
  58. {value: "key", label: "字典键值"},
  59. {value: "value", label: "字典名称"},
  60. {value: "do", label: "操作", width: 120, fixed: 'right'},
  61. ],
  62. data: []
  63. })
  64. const ref_form = ref()
  65. const titleCpt = computed(() => {
  66. let t = '转换规则-字典值'
  67. return t
  68. })
  69. const onSubmit = () => {
  70. ref_form.value.submit().then(() => {
  71. const map = new Map()
  72. state.data.forEach(v => {
  73. map.set(v.key, v.value)
  74. })
  75. if (map.size < state.data.length) {
  76. ElMessage.error('字典键值不可重复!')
  77. return
  78. }
  79. ElMessageBox.confirm("是否提交?", "提示", {
  80. confirmButtonText: "确定",
  81. cancelButtonText: "取消",
  82. type: "warning",
  83. } as any).then(() => {
  84. emit('update:show', false)
  85. emit('dict', state.data)
  86. console.log(state.data)
  87. }).catch(() => {})
  88. }).catch((e) => {
  89. ElMessage({
  90. message: e[0].message,
  91. grouping: true,
  92. type: 'warning',
  93. })
  94. })
  95. }
  96. watch(() => props.show, (n) => {
  97. if (n) {
  98. try {
  99. state.data = typeof props.transfer.formatRule === 'object' ? [...props.transfer.formatRule] : (typeof props.transfer.formatRule === 'string' ? JSON.parse(props.transfer.formatRule) : [])
  100. } catch (e) {
  101. state.data = []
  102. }
  103. nextTick(() => {
  104. ref_form.value.reset()
  105. })
  106. }
  107. })
  108. </script>
  109. <style lang="scss" scoped>
  110. .__cus-dialog-form {
  111. height: 100%;
  112. display: flex;
  113. flex-direction: column;
  114. }
  115. </style>