dict.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. <template>
  2. <basic-container>
  3. <avue-crud
  4. :option="optionParent"
  5. :table-loading="loading"
  6. :data="dataParent"
  7. :page="pageParent"
  8. ref="crud"
  9. v-model="formParent"
  10. :permission="permissionList"
  11. :before-open="beforeOpen"
  12. @row-del="rowDel"
  13. @row-update="rowUpdate"
  14. @row-save="rowSave"
  15. @row-click="handleRowClick"
  16. @search-change="searchChange"
  17. @search-reset="searchReset"
  18. @selection-change="selectionChange"
  19. @current-change="currentChange"
  20. @size-change="sizeChange"
  21. @refresh-change="refreshChange"
  22. @on-load="onLoadParent"
  23. >
  24. <template slot="menuLeft">
  25. <el-button
  26. type="danger"
  27. size="small"
  28. icon="el-icon-delete"
  29. v-if="permission.dict_delete"
  30. plain
  31. @click="handleDelete"
  32. >删 除
  33. </el-button>
  34. </template>
  35. <template slot-scope="scope" slot="menu">
  36. <el-button
  37. type="text"
  38. icon="el-icon-setting"
  39. size="small"
  40. @click.stop="handleRowClick(scope.row)"
  41. v-if="userInfo.role_name.includes('admin')"
  42. >字典配置
  43. </el-button>
  44. </template>
  45. <template slot-scope="{row}" slot="code">
  46. <el-tag @click="handleRowClick(row)" style="cursor:pointer">{{ row.code }}</el-tag>
  47. </template>
  48. <template slot-scope="{row}" slot="isSealed">
  49. <el-tag>{{ row.isSealed === 0 ? '否' : '是' }}</el-tag>
  50. </template>
  51. </avue-crud>
  52. <el-dialog :title="`[${dictValue}]字典配置`"
  53. append-to-body
  54. :visible.sync="box"
  55. width="1000px">
  56. <avue-crud
  57. :option="optionChild"
  58. :table-loading="loadingChild"
  59. :data="dataChild"
  60. ref="crudChild"
  61. v-model="formChild"
  62. :permission="permissionList"
  63. :before-open="beforeOpenChild"
  64. :before-close="beforeCloseChild"
  65. @row-del="rowDelChild"
  66. @row-update="rowUpdateChild"
  67. @row-save="rowSaveChild"
  68. @search-change="searchChangeChild"
  69. @search-reset="searchResetChild"
  70. @selection-change="selectionChangeChild"
  71. @current-change="currentChangeChild"
  72. @size-change="sizeChangeChild"
  73. @refresh-change="refreshChangeChild"
  74. @on-load="onLoadChild"
  75. >
  76. <template slot="menuLeft">
  77. <el-button
  78. type="danger"
  79. size="small"
  80. icon="el-icon-delete"
  81. v-if="permission.dict_delete"
  82. plain
  83. @click="handleDelete"
  84. >删 除
  85. </el-button>
  86. </template>
  87. <template slot-scope="scope" slot="menu">
  88. <el-button
  89. type="text"
  90. icon="el-icon-circle-plus-outline"
  91. size="small"
  92. @click.stop="handleAdd(scope.row,scope.index)"
  93. v-if="userInfo.role_name.includes('admin')"
  94. >新增子项
  95. </el-button>
  96. </template>
  97. <template slot-scope="{row}" slot="isSealed">
  98. <el-tag>{{ row.isSealed === 0 ? '否' : '是' }}</el-tag>
  99. </template>
  100. </avue-crud>
  101. </el-dialog>
  102. </basic-container>
  103. </template>
  104. <script>
  105. import {
  106. getParentList,
  107. getChildList,
  108. remove,
  109. update,
  110. add,
  111. getDict,
  112. getDictTree
  113. } from "@/api/system/dict";
  114. import {optionParent, optionChild} from "@/option/system/dict";
  115. import {mapGetters} from "vuex";
  116. export default {
  117. data() {
  118. return {
  119. dictValue: '暂无',
  120. parentId: -1,
  121. formParent: {},
  122. formChild: {},
  123. selectionList: [],
  124. query: {},
  125. box: false,
  126. loading: true,
  127. loadingChild: true,
  128. pageParent: {
  129. pageSize: 10,
  130. pageSizes: [10, 30, 50, 100, 200],
  131. currentPage: 1,
  132. total: 0
  133. },
  134. pageChild: {
  135. pageSize: 10,
  136. pageSizes: [10, 30, 50, 100, 200],
  137. currentPage: 1,
  138. total: 0
  139. },
  140. dataParent: [],
  141. dataChild: [],
  142. optionParent: optionParent,
  143. optionChild: optionChild,
  144. };
  145. },
  146. computed: {
  147. ...mapGetters(["userInfo", "permission"]),
  148. permissionList() {
  149. return {
  150. addBtn: this.vaildData(this.permission.dict_add, false),
  151. delBtn: this.vaildData(this.permission.dict_delete, false),
  152. editBtn: this.vaildData(this.permission.dict_edit, false),
  153. viewBtn: false,
  154. };
  155. },
  156. ids() {
  157. let ids = [];
  158. this.selectionList.forEach(ele => {
  159. ids.push(ele.id);
  160. });
  161. return ids.join(",");
  162. }
  163. },
  164. mounted() {
  165. this.initData();
  166. },
  167. methods: {
  168. initData() {
  169. getDictTree().then(res => {
  170. const column = this.findObject(this.optionChild.column, "parentId");
  171. column.dicData = res.data.data;
  172. });
  173. },
  174. handleAdd(row) {
  175. this.formChild.dictValue = "";
  176. this.formChild.dictKey = "";
  177. this.formChild.sort = 0;
  178. this.formChild.isSealed = 0;
  179. this.formChild.remark = "";
  180. this.formChild.parentId = row.id;
  181. this.$refs.crudChild.rowAdd();
  182. },
  183. rowSave(row, done, loading) {
  184. const form = {
  185. ...row,
  186. dictKey: -1,
  187. };
  188. add(form).then(() => {
  189. this.onLoadParent(this.pageParent);
  190. this.$message({
  191. type: "success",
  192. message: "操作成功!"
  193. });
  194. done();
  195. }, error => {
  196. window.console.log(error);
  197. loading();
  198. });
  199. },
  200. rowUpdate(row, index, done, loading) {
  201. update(row).then(() => {
  202. this.onLoadParent(this.pageParent);
  203. this.$message({
  204. type: "success",
  205. message: "操作成功!"
  206. });
  207. this.onLoadChild(this.pageChild);
  208. done();
  209. }, error => {
  210. window.console.log(error);
  211. loading();
  212. });
  213. },
  214. rowDel(row) {
  215. this.$confirm("确定将选择数据删除?", {
  216. confirmButtonText: "确定",
  217. cancelButtonText: "取消",
  218. type: "warning"
  219. })
  220. .then(() => {
  221. return remove(row.id);
  222. })
  223. .then(() => {
  224. this.onLoadParent(this.pageParent);
  225. this.$message({
  226. type: "success",
  227. message: "操作成功!"
  228. });
  229. });
  230. },
  231. handleRowClick(row) {
  232. this.query = {};
  233. this.parentId = row.id;
  234. this.dictValue = row.dictValue;
  235. const code = this.findObject(this.optionChild.column, "code");
  236. code.value = row.code;
  237. const parentId = this.findObject(this.optionChild.column, "parentId");
  238. parentId.value = row.id;
  239. this.box = true;
  240. this.onLoadChild(this.pageChild);
  241. },
  242. searchReset() {
  243. this.query = {};
  244. this.onLoadParent(this.pageParent);
  245. },
  246. searchChange(params, done) {
  247. this.query = params;
  248. this.pageParent.currentPage = 1;
  249. this.onLoadParent(this.pageParent, params);
  250. done();
  251. },
  252. selectionChange(list) {
  253. this.selectionList = list;
  254. },
  255. selectionClear() {
  256. this.selectionList = [];
  257. this.$refs.crud.toggleSelection();
  258. },
  259. handleDelete() {
  260. if (this.selectionList.length === 0) {
  261. this.$message.warning("请选择至少一条数据");
  262. return;
  263. }
  264. this.$confirm("确定将选择数据删除?", {
  265. confirmButtonText: "确定",
  266. cancelButtonText: "取消",
  267. type: "warning"
  268. })
  269. .then(() => {
  270. return remove(this.ids);
  271. })
  272. .then(() => {
  273. this.onLoadParent(this.pageParent);
  274. this.$message({
  275. type: "success",
  276. message: "操作成功!"
  277. });
  278. this.$refs.crud.toggleSelection();
  279. });
  280. },
  281. beforeOpen(done, type) {
  282. if (["edit", "view"].includes(type)) {
  283. getDict(this.formParent.id).then(res => {
  284. this.formParent = res.data.data;
  285. });
  286. }
  287. done();
  288. },
  289. currentChange(currentPage) {
  290. this.pageParent.currentPage = currentPage;
  291. },
  292. sizeChange(pageSize) {
  293. this.pageParent.pageSize = pageSize;
  294. },
  295. refreshChange() {
  296. this.onLoadParent(this.pageParent, this.query);
  297. },
  298. rowSaveChild(row, done, loading) {
  299. add(row).then(() => {
  300. this.onLoadChild(this.pageChild);
  301. this.$message({
  302. type: "success",
  303. message: "操作成功!"
  304. });
  305. done();
  306. }, error => {
  307. window.console.log(error);
  308. loading();
  309. });
  310. },
  311. rowUpdateChild(row, index, done, loading) {
  312. update(row).then(() => {
  313. this.onLoadChild(this.pageChild);
  314. this.$message({
  315. type: "success",
  316. message: "操作成功!"
  317. });
  318. done();
  319. }, error => {
  320. window.console.log(error);
  321. loading();
  322. });
  323. },
  324. rowDelChild(row) {
  325. this.$confirm("确定将选择数据删除?", {
  326. confirmButtonText: "确定",
  327. cancelButtonText: "取消",
  328. type: "warning"
  329. })
  330. .then(() => {
  331. return remove(row.id);
  332. })
  333. .then(() => {
  334. this.onLoadChild(this.pageChild);
  335. this.$message({
  336. type: "success",
  337. message: "操作成功!"
  338. });
  339. });
  340. },
  341. searchResetChild() {
  342. this.query = {};
  343. this.onLoadChild(this.pageChild);
  344. },
  345. searchChangeChild(params, done) {
  346. this.query = params;
  347. this.pageChild.currentPage = 1;
  348. this.onLoadChild(this.pageChild, params);
  349. done();
  350. },
  351. selectionChangeChild(list) {
  352. this.selectionList = list;
  353. },
  354. selectionClearChild() {
  355. this.selectionList = [];
  356. this.$refs.crudChild.toggleSelection();
  357. },
  358. handleDeleteChild() {
  359. if (this.selectionList.length === 0) {
  360. this.$message.warning("请选择至少一条数据");
  361. return;
  362. }
  363. this.$confirm("确定将选择数据删除?", {
  364. confirmButtonText: "确定",
  365. cancelButtonText: "取消",
  366. type: "warning"
  367. })
  368. .then(() => {
  369. return remove(this.ids);
  370. })
  371. .then(() => {
  372. this.onLoadChild(this.pageChild);
  373. this.$message({
  374. type: "success",
  375. message: "操作成功!"
  376. });
  377. this.$refs.crudChild.toggleSelection();
  378. });
  379. },
  380. beforeOpenChild(done, type) {
  381. if (["add", "edit"].includes(type)) {
  382. this.initData();
  383. }
  384. if (["edit", "view"].includes(type)) {
  385. getDict(this.formChild.id).then(res => {
  386. this.formChild = res.data.data;
  387. });
  388. }
  389. done();
  390. },
  391. beforeCloseChild(done) {
  392. this.$refs.crudChild.value.parentId = this.parentId;
  393. this.$refs.crudChild.option.column.filter(item => {
  394. if (item.prop === "parentId") {
  395. item.value = this.parentId;
  396. }
  397. });
  398. done();
  399. },
  400. currentChangeChild(currentPage) {
  401. this.pageChild.currentPage = currentPage;
  402. },
  403. sizeChangeChild(pageSize) {
  404. this.pageChild.pageSize = pageSize;
  405. },
  406. refreshChangeChild() {
  407. this.onLoadChild(this.pageChild, this.query);
  408. },
  409. onLoadParent(page, params = {}) {
  410. this.loading = true;
  411. getParentList(
  412. page.currentPage,
  413. page.pageSize,
  414. Object.assign(params, this.query)
  415. ).then(res => {
  416. const data = res.data.data;
  417. this.pageParent.total = data.total;
  418. this.dataParent = data.records;
  419. this.loading = false;
  420. this.selectionClear();
  421. });
  422. },
  423. onLoadChild(page, params = {}) {
  424. this.loadingChild = true;
  425. getChildList(
  426. page.currentPage,
  427. page.pageSize,
  428. this.parentId,
  429. Object.assign(params, this.query)
  430. ).then(res => {
  431. this.dataChild = res.data.data;
  432. this.loadingChild = false;
  433. this.selectionClear();
  434. });
  435. }
  436. }
  437. };
  438. </script>