dict-edit.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. <!-- 字典编辑弹窗 -->
  2. <template>
  3. <ele-modal
  4. width="800px"
  5. :maxable="true"
  6. :visible="visible"
  7. :close-on-click-modal="true"
  8. :title="isUpdate ? '修改字典' : '添加字典'"
  9. @update:visible="updateVisible"
  10. >
  11. <div class="divider">
  12. <div class="title">
  13. <div class="ele-bg-primary"></div>
  14. <span>基本信息</span>
  15. </div>
  16. <div class="ele-bg-primary" style="width: 100%; height: 2px"></div>
  17. </div>
  18. <el-form
  19. ref="form"
  20. :model="form"
  21. :rules="rules"
  22. label-width="85px"
  23. style="margin-bottom: 10px"
  24. >
  25. <el-row>
  26. <el-col :span="12">
  27. <el-form-item label="字典名称:" prop="name">
  28. <el-input
  29. clearable
  30. :disabled="isUpdate"
  31. :maxlength="20"
  32. v-model="form.name"
  33. placeholder="请输入字典名称"
  34. />
  35. </el-form-item>
  36. </el-col>
  37. <el-col :span="12">
  38. <el-form-item label="字典值:" prop="code">
  39. <el-input
  40. clearable
  41. :maxlength="20"
  42. v-model="form.code"
  43. placeholder="请输入字典值"
  44. />
  45. </el-form-item>
  46. </el-col>
  47. <el-col :span="12">
  48. <el-form-item label="应用类型:" prop="appType">
  49. <el-radio-group v-model="form.appType">
  50. <el-radio :label="1">业务字典</el-radio>
  51. <el-radio :label="2">数据字典</el-radio>
  52. </el-radio-group>
  53. </el-form-item>
  54. </el-col>
  55. <el-col :span="12">
  56. <el-form-item label="是否启用:" prop="enable">
  57. <el-radio-group v-model="form.enable">
  58. <el-radio :label="1">是</el-radio>
  59. <el-radio :label="0">否</el-radio>
  60. </el-radio-group>
  61. </el-form-item>
  62. </el-col>
  63. <!-- <el-col :span="12">
  64. <el-form-item label="排序号:" prop="sortNumber">
  65. <el-input-number
  66. :min="0"
  67. :max="9999"
  68. v-model="form.sortNumber"
  69. controls-position="right"
  70. placeholder="请输入排序号"
  71. class="ele-fluid ele-text-left"
  72. />
  73. </el-form-item>
  74. </el-col> -->
  75. <el-col :span="24">
  76. <el-form-item label="备注:">
  77. <el-input
  78. :rows="4"
  79. type="textarea"
  80. :maxlength="200"
  81. v-model="form.remark"
  82. placeholder="请输入备注"
  83. />
  84. </el-form-item>
  85. </el-col>
  86. </el-row>
  87. </el-form>
  88. <div class="divider">
  89. <div class="title">
  90. <div class="ele-bg-primary"></div>
  91. <span>字典项配置</span>
  92. </div>
  93. <div class="ele-bg-primary" style="width: 100%; height: 2px"></div>
  94. </div>
  95. <div style="margin: 5px 0">
  96. <el-button
  97. size="small"
  98. type="primary"
  99. icon="el-icon-plus"
  100. class="ele-btn-icon"
  101. @click="pushArr"
  102. >
  103. 添加
  104. </el-button>
  105. <!-- <el-button
  106. size="small"
  107. type="danger"
  108. icon="el-icon-delete"
  109. class="ele-btn-icon"
  110. @click="removeArr"
  111. >
  112. 删除
  113. </el-button> -->
  114. </div>
  115. <el-table
  116. :data="tableData"
  117. border
  118. style="width: 100%"
  119. @selection-change="handleSelectionChange"
  120. >
  121. <el-table-column prop="code" label="字典项编码">
  122. <template slot-scope="scope">
  123. <el-input
  124. @blur="checkedValue(scope.row.code)"
  125. v-model="scope.row.code"
  126. placeholder=""
  127. ></el-input>
  128. </template>
  129. </el-table-column>
  130. <el-table-column prop="name" label="字典项名称">
  131. <template slot-scope="scope">
  132. <el-input v-model="scope.row.name" placeholder=""></el-input>
  133. </template>
  134. </el-table-column>
  135. <el-table-column prop="sort" label="排序">
  136. <template slot-scope="scope">
  137. <el-input
  138. v-model="scope.row.sort"
  139. @input="
  140. scope.row.sort = String(scope.row.sort).replace(/[^\d]/g, '')
  141. "
  142. ></el-input>
  143. </template>
  144. </el-table-column>
  145. <el-table-column prop="enable" label="启用">
  146. <template slot-scope="scope">
  147. <el-radio-group v-model="scope.row.enable">
  148. <el-radio :label="1">是</el-radio>
  149. <el-radio :label="0">否</el-radio>
  150. </el-radio-group>
  151. </template>
  152. </el-table-column>
  153. <el-table-column align="center" label="操作">
  154. <template slot-scope="scope">
  155. <el-popconfirm
  156. class="ele-action"
  157. title="确定要删除此字典项吗?"
  158. @confirm="removeArr(scope)"
  159. >
  160. <template v-slot:reference>
  161. <el-link type="danger" :underline="false" icon="el-icon-delete">
  162. 删除
  163. </el-link>
  164. </template>
  165. </el-popconfirm>
  166. </template>
  167. </el-table-column>
  168. </el-table>
  169. <template v-slot:footer>
  170. <el-button @click="updateVisible(false)">取消</el-button>
  171. <el-button type="primary" :loading="loading" @click="save">
  172. 保存
  173. </el-button>
  174. </template>
  175. </ele-modal>
  176. </template>
  177. <script>
  178. import { addDictionary, updateDictionary } from '@/api/system/dictionary';
  179. import {
  180. pageDictionaryData,
  181. removeDictionaryData,
  182. removeDictionaryDataBatch
  183. } from '@/api/system/dictionary-data';
  184. export default {
  185. props: {
  186. // 弹窗是否打开
  187. visible: Boolean,
  188. // 修改回显的数据
  189. id: String | Number
  190. },
  191. data() {
  192. const defaultForm = {
  193. name: '',
  194. code: '',
  195. appType: 1,
  196. enable: 1,
  197. remark: '',
  198. dictStaticSubmitPOList: []
  199. };
  200. return {
  201. defaultForm,
  202. // 表单数据
  203. form: { ...defaultForm },
  204. // form: {
  205. // name: '',
  206. // code: '',
  207. // appType: 1,
  208. // enable: 1,
  209. // remark: ''
  210. // },
  211. // 表单验证规则
  212. rules: {
  213. name: [
  214. {
  215. required: true,
  216. message: '请输入字典名称',
  217. trigger: 'blur'
  218. }
  219. ],
  220. code: [
  221. {
  222. required: true,
  223. message: '请输入字典值',
  224. trigger: 'blur'
  225. }
  226. ],
  227. appType: [
  228. {
  229. required: true,
  230. message: '请选择应用类型',
  231. trigger: 'blur'
  232. }
  233. ],
  234. enable: [
  235. {
  236. required: true,
  237. message: '请选择是否启用',
  238. trigger: 'blur'
  239. }
  240. ],
  241. sort: [
  242. {
  243. required: true,
  244. message: '请输入排序号',
  245. trigger: 'blur'
  246. }
  247. ]
  248. },
  249. // 表格列配置
  250. columns: [
  251. {
  252. columnKey: 'selection',
  253. type: 'selection',
  254. width: 45,
  255. align: 'center'
  256. },
  257. {
  258. columnKey: 'index',
  259. type: 'index',
  260. width: 45,
  261. align: 'center',
  262. showOverflowTooltip: true
  263. },
  264. {
  265. prop: 'code',
  266. label: '字典编码',
  267. showOverflowTooltip: true
  268. },
  269. {
  270. prop: 'name',
  271. label: '字典名称',
  272. showOverflowTooltip: true
  273. },
  274. // {
  275. // prop: 'appType',
  276. // label: '字典类型',
  277. // showOverflowTooltip: true,
  278. // minWidth: 110
  279. // },
  280. {
  281. prop: 'appType',
  282. label: '应用类型',
  283. showOverflowTooltip: true
  284. },
  285. {
  286. prop: 'remark',
  287. label: '描述',
  288. showOverflowTooltip: true
  289. },
  290. {
  291. prop: 'createTime',
  292. label: '创建时间',
  293. showOverflowTooltip: true,
  294. minWidth: 110,
  295. formatter: (_row, _column, cellValue) => {
  296. return this.$util.toDateString(cellValue);
  297. }
  298. },
  299. {
  300. columnKey: 'action',
  301. label: '操作',
  302. width: 130,
  303. align: 'center',
  304. resizable: false,
  305. slot: 'action',
  306. showOverflowTooltip: true
  307. }
  308. ],
  309. tableData: [],
  310. // 表格选中数据
  311. selection: [],
  312. datasource: {
  313. list: []
  314. },
  315. // 提交状态
  316. loading: false,
  317. // 是否是修改
  318. isUpdate: false,
  319. delectId: 1
  320. };
  321. },
  322. created() {},
  323. methods: {
  324. pushArr() {
  325. this.form.dictStaticSubmitPOList.push({
  326. code: '',
  327. enable: 1,
  328. name: '',
  329. sort: '',
  330. delectId: this.delectId
  331. });
  332. this.delectId = this.delectId + 1;
  333. this.filterArr();
  334. },
  335. //
  336. checkedValue(val) {
  337. console.log(val);
  338. },
  339. removeArr(row) {
  340. console.log(row);
  341. if (row.row.id) {
  342. this.form.dictStaticSubmitPOList.forEach((item) => {
  343. if (item.id === row.row.id) {
  344. item.deleted = 1;
  345. }
  346. });
  347. this.filterArr();
  348. } else {
  349. let index = this.form.dictStaticSubmitPOList.findIndex(
  350. (item) => item.delectId === row.row.delectId
  351. );
  352. console.log(index);
  353. this.form.dictStaticSubmitPOList.splice(index, 1); // 在数组的指定位置移除对应的元素。 返回移除的
  354. this.filterArr();
  355. }
  356. // let falg = this.form.dictStaticSubmitPOList[row.$index].deleted === 0;
  357. // if (falg) {
  358. // this.form.dictStaticSubmitPOList[row.$index].deleted = 1;
  359. // this.filterArr();
  360. // } else {
  361. // this.form.dictStaticSubmitPOList.splice(row.$index, 1);
  362. // this.filterArr();
  363. // }
  364. },
  365. handleSelectionChange() {},
  366. /* 保存编辑 */
  367. save() {
  368. this.$refs.form.validate((valid) => {
  369. if (!valid) {
  370. return false;
  371. }
  372. let falg = this.form.dictStaticSubmitPOList.some(
  373. (item, index) => item.code.length === 0
  374. );
  375. if (falg) {
  376. this.$message({
  377. message: '字典项编码不能为空',
  378. type: 'warning'
  379. });
  380. return;
  381. }
  382. this.loading = true;
  383. const saveOrUpdate = this.isUpdate ? updateDictionary : addDictionary;
  384. saveOrUpdate(this.form)
  385. .then((msg) => {
  386. this.loading = false;
  387. this.$message.success(msg);
  388. this.updateVisible(false);
  389. this.$emit('done');
  390. })
  391. .catch((e) => {
  392. this.loading = false;
  393. this.$message.error(e.message);
  394. });
  395. });
  396. },
  397. filterArr() {
  398. this.tableData = this.form.dictStaticSubmitPOList.filter(
  399. (item) => item.deleted !== 1
  400. );
  401. },
  402. /* 更新visible */
  403. updateVisible(value) {
  404. this.$emit('update:visible', value);
  405. },
  406. async getDetail() {
  407. const res = await pageDictionaryData(this.id);
  408. this.form = res.data.dictInfoVO;
  409. this.form.dictStaticSubmitPOList = res.data.dictStaticVOList;
  410. if (this.form.dictStaticSubmitPOList.length > 0) {
  411. this.filterArr();
  412. } else {
  413. this.tableTata = [];
  414. }
  415. }
  416. },
  417. watch: {
  418. visible(visible) {
  419. if (visible) {
  420. if (this.id) {
  421. // this.$util.assignObject(this.form, this.data);
  422. this.isUpdate = true;
  423. this.getDetail();
  424. } else {
  425. this.isUpdate = false;
  426. }
  427. } else {
  428. this.$refs.form.clearValidate();
  429. this.form = { ...this.defaultForm };
  430. this.form.dictStaticSubmitPOList = []; // clear the list of the dictionary static push button. 描述变化的字段不会在push后
  431. this.tableData = []; // clear the list of the dictionary static push button. 描述变化的字段不会在push后。
  432. }
  433. }
  434. }
  435. };
  436. </script>
  437. <style lang="scss" scoped>
  438. .divider {
  439. margin-bottom: 20px;
  440. .title {
  441. display: flex;
  442. align-items: center;
  443. margin-bottom: 10px;
  444. div {
  445. width: 8px;
  446. height: 20px;
  447. margin-right: 10px;
  448. }
  449. span {
  450. font-size: 20px;
  451. }
  452. }
  453. }
  454. </style>