dict-detail.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <template>
  2. <div>
  3. <el-row>
  4. <el-col :span="12">
  5. <!-- 表单查询 -->
  6. <el-form ref="queryForm" :model="queryParams" :inline="true" size="small">
  7. <el-form-item label="" prop="dictKey">
  8. <el-input v-model="queryParams.dictKey" prefix-icon="el-icon-search" placeholder="请输入名称" clear />
  9. </el-form-item>
  10. <el-form-item>
  11. <el-button type="primary" icon="el-icon-search" size="small" @click="handelSearch">搜索</el-button>
  12. <el-button type="info" icon="el-icon-refresh" size="small" @click="handelReset">重置</el-button>
  13. </el-form-item>
  14. </el-form>
  15. </el-col>
  16. <el-col :span="12">
  17. <el-row class="czBtns" style="float: right;">
  18. <el-col>
  19. <el-button size="small" icon="el-icon-plus" type="primary" @click="handelAdd">新增</el-button>
  20. <el-button
  21. type="danger"
  22. icon="el-icon-delete-solid"
  23. size="small"
  24. :disabled="multiple"
  25. @click="handelDelAll"
  26. >批量删除</el-button>
  27. <el-button size="small" icon="el-icon-back" type="info" @click="handelGo">返回</el-button>
  28. </el-col>
  29. </el-row>
  30. </el-col>
  31. </el-row>
  32. <el-row>
  33. <el-col :span="24">
  34. <!-- 表格数据信息 -->
  35. <el-table
  36. ref="table"
  37. :data="mulData"
  38. row-key="id"
  39. header-row-class-name="headBackground"
  40. @select="select"
  41. @select-all="selectAll"
  42. @selection-change="selectChange"
  43. >
  44. <el-table-column type="selection" align="left" />
  45. <el-table-column label="名称" prop="dictKey" show-overflow-tooltip header-align="left" />
  46. <el-table-column label="值" prop="dictValue" show-overflow-tooltip align="left" />
  47. <el-table-column label="排序号" prop="sortNum" show-overflow- align="left" width=100 />
  48. <el-table-column label="操作" align="left" class-name="small-padding fixed-width">
  49. <template slot-scope="scope">
  50. <el-button size="mini" type="text" icon="el-icon-edit" @click="handelEdit(scope.row)">修改
  51. </el-button>
  52. <el-button
  53. size="mini"
  54. type="text"
  55. icon="el-icon-delete"
  56. :disabled="!multiple"
  57. @click="handelDel(scope.row)"
  58. >删除
  59. </el-button>
  60. </template>
  61. </el-table-column>
  62. </el-table>
  63. <!-- 分页信息 -->
  64. <pagination
  65. v-show="total > 0"
  66. :total="total"
  67. :page.sync="queryParams.page"
  68. :limit.sync="queryParams.num"
  69. align="right"
  70. @pagination="getPageDictList"
  71. />
  72. </el-col>
  73. </el-row>
  74. <!-- 弹出框 -->
  75. <el-dialog :title="title" :visible.sync="dialogVisible" width="600px" append-to-body :close-on-click-modal="false">
  76. <el-form ref="form" :model="form" :rules="rules" label-width="80px">
  77. <el-row>
  78. <el-col :span="24">
  79. <el-form-item label="上级字典" prop="parentId">
  80. <selectTree :key="key" v-model="form.parentId" :options="options" placeholder="请选择上级字典" :key-name="keyName" menu-name="主字典" />
  81. </el-form-item>
  82. </el-col>
  83. </el-row>
  84. <el-row>
  85. <el-col :span="24">
  86. <el-form-item label="名称" prop="dictKey">
  87. <el-input v-model.trim="form.dictKey" maxlength="15" clearable placeholder="请输入名称" />
  88. </el-form-item>
  89. </el-col>
  90. </el-row>
  91. <el-row>
  92. <el-col :span="12">
  93. <el-form-item label="值" prop="dictValue">
  94. <el-input v-model.trim="form.dictValue" maxlength="15" clearable placeholder="请输入值" />
  95. </el-form-item>
  96. </el-col>
  97. <el-col :span="12">
  98. <el-form-item label="排序号" prop="sortNum">
  99. <el-input
  100. v-model.trim="form.sortNum"
  101. placeholder="请输入排序号"
  102. controls-position="right"
  103. :minlength="1"
  104. :maxlength="5"
  105. />
  106. </el-form-item>
  107. </el-col>
  108. </el-row>
  109. </el-form>
  110. <span slot="footer" class="dialog-footer">
  111. <el-button size="mini" type="primary" @click="handelSubmit('form')">确定</el-button>
  112. <el-button size="mini" @click="handelCancel('form')">取消</el-button>
  113. </span>
  114. </el-dialog>
  115. </div>
  116. </template>
  117. <script>
  118. import { delDict, addDict, getPageDictList, editDict, getDictListByType, getDictDetail } from '@/api/system/dict.js'
  119. import selectTree from '@/components/SelectTree'
  120. import multiple from '@/views/system/mixins/multiple'
  121. export default {
  122. components: { selectTree },
  123. mixins: [multiple],
  124. data() {
  125. const sortNumRule = (rule, value, callback) => {
  126. var reg = /^[1-9][0-9]*$/
  127. if (!value) {
  128. callback(new Error('排序号不能为空'))
  129. }
  130. if (!(reg.test(value))) {
  131. callback(new Error('只能输入正整数'))
  132. }
  133. callback()
  134. }
  135. return {
  136. // 遮罩层
  137. loading: false,
  138. // 查询参数
  139. queryParams: {
  140. page: 1,
  141. num: 10,
  142. dictValue: null,
  143. dictKey: null
  144. },
  145. // 总条数
  146. total: 0,
  147. // 表格数据
  148. mulData: [],
  149. // 弹出框
  150. dialogVisible: false,
  151. // 弹出框标题
  152. title: '',
  153. // 弹出框表单
  154. form: {
  155. parentId: 0,
  156. dictValue: null,
  157. dictKey: null,
  158. sortNum: null
  159. },
  160. // 字典树数据
  161. options: [],
  162. // 表单验证
  163. rules: {
  164. dictValue: [
  165. { required: true, message: '字典值不能为空', trigger: 'blur' }
  166. ],
  167. dictKey: [
  168. { required: true, message: '字典名称不能为空', trigger: 'blur' }
  169. ],
  170. sortNum: [
  171. { required: false, trigger: 'blur', validator: sortNumRule }
  172. ]
  173. },
  174. // 控制批量删除按钮
  175. multiple: true,
  176. // 删除字典id集合
  177. ids: [],
  178. // 自定义treeselect键名
  179. keyName: null,
  180. // 字典类型
  181. keyType: null,
  182. // 更新下拉树
  183. key: 0
  184. }
  185. },
  186. created() {
  187. const { id, dictKeyType } = this.$route.query
  188. this.form.dictTypeId = id
  189. this.queryParams.dictTypeId = id
  190. this.keyType = dictKeyType
  191. this.getPageDictList()
  192. },
  193. methods: {
  194. /** 分页获取字典列表数据 */
  195. getPageDictList() {
  196. getPageDictList(this.queryParams).then(res => {
  197. if (res.code === 200 & res.data === null) {
  198. this.mulData = []
  199. this.total = 0
  200. return
  201. }
  202. const data = res.data
  203. this.mulData = data.dictPage
  204. this.total = data.count
  205. this.defaultExpand(this.mulData)
  206. })
  207. },
  208. /** 默认收起所有行 */
  209. defaultExpand(data) {
  210. data.forEach(item => {
  211. this.$refs.table && this.$refs.table.toggleRowExpansion(item, false)
  212. if (item.children && item.children.length) {
  213. this.defaultExpand(item.children)
  214. }
  215. })
  216. },
  217. /** 全量获取字典列表数据 */
  218. getDictListByType() {
  219. getDictListByType({ keyType: this.keyType }).then(res => {
  220. const data = res.data
  221. this.options = this.$utils.toArrayTree(data)
  222. this.keyName = 'dictKey'
  223. })
  224. },
  225. /** 搜索 */
  226. handelSearch(event) {
  227. this.$resetBtn(event)
  228. this.queryParams.page = 1
  229. this.getPageDictList()
  230. },
  231. /** 重置按钮操作 */
  232. handelReset(event) {
  233. this.$refs['queryForm'].resetFields()
  234. this.handelSearch(event)
  235. },
  236. /** 返回按钮 */
  237. handelGo() {
  238. this.$store.dispatch('tagsView/delVisitedView', this.$route)
  239. this.$router.push('/system/dict')
  240. },
  241. /** 新增按钮 */
  242. handelAdd() {
  243. this.dialogVisible = true
  244. this.key++
  245. this.title = '新增字典'
  246. this.reset()
  247. this.getDictListByType()
  248. },
  249. /** 修改按钮 */
  250. handelEdit(row) {
  251. this.key++
  252. getDictDetail({ dictId: row.id }).then(res => {
  253. if (res.code === 200) {
  254. this.form = res.data
  255. this.getDictListByType()
  256. }
  257. })
  258. this.dialogVisible = true
  259. this.title = '修改字典'
  260. },
  261. /** 表格多选 */
  262. selectChange(selection) {
  263. this.ids = selection
  264. this.multiple = !selection.length
  265. },
  266. /** 删除按钮 */
  267. handelDel(row) {
  268. this.$confirm('您确定要删除该字典吗?', '系统提示', {
  269. confirmButtonText: '确定',
  270. cancelButtonText: '取消',
  271. cancelButtonClass: 'btn_custom_cancel',
  272. closeOnClickModal: false,
  273. type: 'warning'
  274. }).then(() => {
  275. if (row.children && row.children.length) {
  276. this.$message({
  277. type: 'warning',
  278. message: '请先删除下级字典'
  279. })
  280. } else {
  281. if (this.mulData.length === 1 && this.queryParams.page > 1) {
  282. this.queryParams.page -= 1
  283. }
  284. this.ids.push(row.id)
  285. this.delDictType()
  286. }
  287. }).catch(() => {
  288. this.$message({
  289. type: 'info',
  290. message: '已取消删除'
  291. })
  292. })
  293. },
  294. /** 批量删除 */
  295. handelDelAll() {
  296. this.$confirm('您确定要批量删除选中的字典吗?', '系统提示', {
  297. confirmButtonText: '确定',
  298. cancelButtonText: '取消',
  299. cancelButtonClass: 'btn_custom_cancel',
  300. closeOnClickModal: false,
  301. type: 'warning'
  302. }).then(() => {
  303. if (this.ids.some(o => o.children && o.children.length)) {
  304. this.$message({
  305. type: 'warning',
  306. message: '请先删除下级字典'
  307. })
  308. this.$refs.table.clearSelection()
  309. } else {
  310. this.ids = this.ids.map(item => item.id)
  311. if (this.ids.length === this.mulData.length && this.queryParams.page > 1) {
  312. this.queryParams.page -= 1
  313. }
  314. this.delDictType()
  315. }
  316. }).catch(() => {
  317. this.$message({
  318. type: 'info',
  319. message: '已取消删除'
  320. })
  321. this.$refs.table.clearSelection()
  322. })
  323. },
  324. /** 删除接口 */
  325. delDictType() {
  326. delDict(this.ids).then(res => {
  327. if (res.code === 200) {
  328. this.$message({
  329. type: 'success',
  330. message: '删除成功'
  331. })
  332. this.getPageDictList()
  333. }
  334. })
  335. },
  336. /** 表单提交 */
  337. handelSubmit(formName) {
  338. this.$refs[formName].validate((valid) => {
  339. if (valid) {
  340. if (this.title === '新增字典') {
  341. addDict(this.form).then(res => {
  342. if (res.code === 200) {
  343. this.$message({
  344. message: res.data,
  345. type: 'success'
  346. })
  347. this.getPageDictList()
  348. this.dialogVisible = false
  349. }
  350. })
  351. } else {
  352. if (this.form.parentId === undefined) {
  353. this.form.parentId = 0
  354. return
  355. }
  356. if (this.form.parentId === this.form.id) {
  357. this.$message({
  358. message: '不可选择与自身相同的上级字典',
  359. type: 'warning'
  360. })
  361. return
  362. }
  363. editDict(this.form).then(res => {
  364. if (res.code === 200) {
  365. this.$message({
  366. message: res.data,
  367. type: 'success'
  368. })
  369. this.getPageDictList()
  370. this.dialogVisible = false
  371. }
  372. })
  373. }
  374. }
  375. })
  376. },
  377. /** 表单取消 */
  378. handelCancel() {
  379. this.dialogVisible = false
  380. this.reset()
  381. },
  382. /** 表单重置 */
  383. reset() {
  384. if (this.$refs['form']) {
  385. this.$refs['form'].resetFields()
  386. }
  387. }
  388. }
  389. }
  390. </script>
  391. <style lang="scss" rel="stylesheet/scss" scoped>
  392. </style>