dataDictionary-detail.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <style scoped>
  2. .user-panel {
  3. margin: 10px auto;
  4. }
  5. </style>
  6. <template>
  7. <el-dialog
  8. :visible.sync="showDialog"
  9. :title="modalTitle"
  10. :modal-append-to-body="false"
  11. style="text-align:left;"
  12. @close="closeDialog"
  13. :close-on-click-modal="false"
  14. >
  15. <div class="user-panel" v-loading="loading">
  16. <el-form ref="form" :model="formModel" :rules="ruleValidate" :label-width="'100px'">
  17. <el-form-item label="字典类型" prop="dataType">
  18. <el-select v-model="formModel.dataType">
  19. <el-option label="字典目录" value="1"></el-option>
  20. <el-option label="数据" value="2"></el-option>
  21. </el-select>
  22. </el-form-item>
  23. <el-form-item label="所属目录" prop="parentId">
  24. <el-select
  25. v-model="formModel.parentId"
  26. filterable
  27. remote
  28. clearable
  29. placeholder="请输入关键词"
  30. :remote-method="queryMenu"
  31. style="width:300px"
  32. >
  33. <el-option
  34. v-for="dataDictionary in dataDictionaryList"
  35. :key="dataDictionary.id"
  36. :label="dataDictionary.name + '(' + dataDictionary.sortNo + ')'"
  37. :value="dataDictionary.id"
  38. ></el-option>
  39. </el-select>
  40. </el-form-item>
  41. <el-form-item label="名称" prop="name">
  42. <el-input v-model="formModel.name" placeholder="请输入名称" style="width:300px"></el-input>
  43. </el-form-item>
  44. <el-form-item label="数值" prop="value">
  45. <el-input v-model="formModel.value" placeholder="请输入数值" style="width:300px"></el-input>
  46. </el-form-item>
  47. <el-form-item label="显示序号" prop="sortNo">
  48. <el-input v-model="formModel.sortNo" placeholder="请输入显示序号" style="width:300px"></el-input>
  49. </el-form-item>
  50. </el-form>
  51. </div>
  52. <span slot="footer" class="dialog-footer">
  53. <el-button @click="closeDialog">取 消</el-button>
  54. <el-button type="primary" @click="handleSubmit" :loading="submitting">确 定</el-button>
  55. </span>
  56. </el-dialog>
  57. </template>
  58. <script>
  59. import Constant from "@/constant";
  60. import dataDictionaryApi from "@/api/sys/dataDictionary";
  61. export default {
  62. props: ["dictId", "modalTitle","catalog"],
  63. data() {
  64. var validateValue = (rule, value, callback) => {
  65. if(this.formModel.dataType == '2' && value==='') {
  66. callback(new Error('类型为数据时,值不能为空!'));
  67. } else {
  68. callback();
  69. }
  70. };
  71. return {
  72. formModel: {
  73. id: ""
  74. },
  75. ruleValidate: {
  76. name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
  77. value: [{ validator: validateValue, trigger: "blur" }],
  78. sortNo: [
  79. { required: true, message: "显示序号不能为空", trigger: "blur" }
  80. ],
  81. dataType: [
  82. {
  83. required: true,
  84. message: "字典类型不能为空",
  85. trigger: "blur"
  86. }
  87. ]
  88. },
  89. dataDictionaryList: [],
  90. showDialog: true,
  91. loading: false,
  92. submitting: false
  93. };
  94. },
  95. computed: {
  96. dataDictionaryListFilter() {
  97. var list = [];
  98. var map = {};
  99. this.dataDictionaryList.forEach((item)=>{
  100. map[item.id] = item;
  101. });
  102. for(var key in map){
  103. list.push(map[key]);
  104. }
  105. return list;
  106. }
  107. },
  108. methods: {
  109. closeDialog() {
  110. this.$emit("close", false);
  111. },
  112. handleSubmit() {
  113. var self = this;
  114. this.$refs["form"].validate(valid => {
  115. if (valid) {
  116. (function() {
  117. var id = self.formModel.id;
  118. if (id == null || id.length == 0) {
  119. return dataDictionaryApi.add(self.formModel);
  120. } else {
  121. return dataDictionaryApi.update(self.formModel);
  122. }
  123. })().then(function(response) {
  124. var jsonData = response.data;
  125. if (jsonData.result) {
  126. self.$message({
  127. message: "保存成功!",
  128. type: "success"
  129. });
  130. self.$emit("close", true);
  131. } else {
  132. self.$message({
  133. message: jsonData.message + "",
  134. type: "warning"
  135. });
  136. self.$emit("close", false);
  137. }
  138. });
  139. }
  140. });
  141. },
  142. queryMenu(keywords,limit) {
  143. var formData = new FormData();
  144. formData.append("keywords", keywords);
  145. formData.append("dataType", "1");
  146. formData.append("excludeId", this.formModel.id);
  147. formData.append("limit", limit | 10);
  148. return dataDictionaryApi.query(formData).then(response => {
  149. var jsonData = response.data;
  150. if (jsonData.result) {
  151. this.dataDictionaryList = jsonData.data;
  152. } else {
  153. this.$message.error(jsonData.message + "");
  154. }
  155. });
  156. }
  157. },
  158. async mounted() {
  159. var self = this;
  160. self.loading = true;
  161. await this.queryMenu("",1000);
  162. (function() {
  163. if (self.dictId.length == 0) {
  164. return dataDictionaryApi.create();
  165. } else {
  166. return dataDictionaryApi.edit(self.dictId);
  167. }
  168. })()
  169. .then(response => {
  170. var jsonData = response.data;
  171. self.loading = false;
  172. if (jsonData.result) {
  173. self.formModel = jsonData.data;
  174. // 增加初始值
  175. if(jsonData.data.parentId!=null){
  176. self.formModel.parentId = jsonData.data.parentId;
  177. // self.dataDictionaryList.push({
  178. // id: jsonData.data.parentId,
  179. // name: jsonData.data.parentName
  180. // });
  181. }
  182. else if(this.catalog!=null && this.catalog.id!=null){
  183. console.log(this.catalog);
  184. self.formModel.parentId = this.catalog.id;
  185. self.formModel.dataType = "2";
  186. // self.dataDictionaryList.push({
  187. // id: this.catalog.id,
  188. // name: this.catalog.name
  189. // });
  190. }
  191. } else {
  192. self.$message.error(jsonData.message + "");
  193. }
  194. })
  195. .catch(error => {
  196. self.$message.error(error + "");
  197. });
  198. }
  199. };
  200. </script>