dict-data-edit.vue 4.1 KB

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