dict-data-edit.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <!-- 字典项编辑弹窗 -->
  2. <template>
  3. <ele-modal
  4. width="460px"
  5. :visible="visible"
  6. :close-on-click-modal="true"
  7. :title="isUpdate ? '修改字典项' : '添加字典项'"
  8. @update:visible="updateVisible"
  9. >
  10. <el-form :model="form" ref="form" :rules="rules" label-width="96px">
  11. <el-form-item label="字典项名称:" prop="dictDataName">
  12. <el-input
  13. clearable
  14. :maxlength="20"
  15. v-model="form.dictDataName"
  16. placeholder="请输入字典项名称"
  17. />
  18. </el-form-item>
  19. <el-form-item label="字典项值:" prop="dictDataCode">
  20. <el-input
  21. clearable
  22. :maxlength="20"
  23. v-model="form.dictDataCode"
  24. placeholder="请输入字典项值"
  25. />
  26. </el-form-item>
  27. <el-form-item label="排序号:" prop="sortNumber">
  28. <el-input-number
  29. :min="0"
  30. :max="9999"
  31. v-model="form.sortNumber"
  32. placeholder="请输入排序号"
  33. controls-position="right"
  34. class="ele-fluid ele-text-left"
  35. />
  36. </el-form-item>
  37. <el-form-item label="备注:">
  38. <el-input
  39. :rows="4"
  40. type="textarea"
  41. :maxlength="200"
  42. v-model="form.comments"
  43. placeholder="请输入备注"
  44. />
  45. </el-form-item>
  46. </el-form>
  47. <template v-slot:footer>
  48. <el-button @click="updateVisible(false)">取消 </el-button>
  49. <el-button type="primary" :loading="loading" @click="save">
  50. 保存
  51. </el-button>
  52. </template>
  53. </ele-modal>
  54. </template>
  55. <script>
  56. import {
  57. addDictionaryData,
  58. updateDictionaryData
  59. } from '@/api/system/dictionary-data';
  60. export default {
  61. // props: {
  62. // // 弹窗是否打开
  63. // visible: Boolean,
  64. // // 修改回显的数据
  65. // data: Object,
  66. // // 字典id
  67. // dictId: Object
  68. // },
  69. props: ['visible', 'data', 'dictId'],
  70. data() {
  71. const defaultForm = {
  72. dictDataId: null,
  73. dictDataName: '',
  74. dictDataCode: '',
  75. sortNumber: '',
  76. comments: ''
  77. };
  78. return {
  79. defaultForm,
  80. // 表单数据
  81. form: { ...defaultForm },
  82. // 表单验证规则
  83. rules: {
  84. dictDataName: [
  85. {
  86. required: true,
  87. message: '请输入字典项名称',
  88. trigger: 'blur'
  89. }
  90. ],
  91. dictDataCode: [
  92. {
  93. required: true,
  94. message: '请输入字典项值',
  95. trigger: 'blur'
  96. }
  97. ],
  98. sortNumber: [
  99. {
  100. required: true,
  101. message: '请输入排序号',
  102. trigger: 'blur'
  103. }
  104. ]
  105. },
  106. // 提交状态
  107. loading: false,
  108. // 是否是修改
  109. isUpdate: false
  110. };
  111. },
  112. methods: {
  113. /* 保存编辑 */
  114. save() {
  115. this.$refs.form.validate((valid) => {
  116. if (!valid) {
  117. return false;
  118. }
  119. this.loading = true;
  120. const saveOrUpdate = this.isUpdate
  121. ? updateDictionaryData
  122. : addDictionaryData;
  123. saveOrUpdate({
  124. ...this.form,
  125. dictId: this.dictId
  126. })
  127. .then((msg) => {
  128. this.loading = false;
  129. console.log(msg);
  130. this.$message.success(msg);
  131. this.updateVisible(false);
  132. this.$emit('done');
  133. })
  134. .catch((e) => {
  135. this.loading = false;
  136. // this.$message.error(e.message);
  137. });
  138. });
  139. },
  140. /* 更新visible */
  141. updateVisible(value) {
  142. this.$emit('update:visible', value);
  143. }
  144. },
  145. watch: {
  146. visible(visible) {
  147. if (visible) {
  148. if (this.data) {
  149. this.$util.assignObject(this.form, this.data);
  150. this.isUpdate = true;
  151. } else {
  152. this.isUpdate = false;
  153. }
  154. } else {
  155. this.$refs.form.clearValidate();
  156. this.form = { ...this.defaultForm };
  157. }
  158. }
  159. }
  160. };
  161. </script>