dict-edit.vue 13 KB

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