dict-edit.vue 13 KB

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