dict-detail.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <template>
  2. <div class="sy-content">
  3. <el-row>
  4. <el-col :span="24">
  5. <!-- 表单查询 -->
  6. <el-form ref="queryForm" :model="queryParams" :inline="true" size="small">
  7. <el-form-item label="字典名称" prop="dictValue">
  8. <el-input v-model="queryParams.dictValue" placeholder="请输入字典名称" clear />
  9. </el-form-item>
  10. <el-form-item label="字典值" prop="dictKey">
  11. <el-input v-model="queryParams.dictKey" placeholder="请输入字典值" clear />
  12. </el-form-item>
  13. <el-form-item>
  14. <el-button type="primary" icon="el-icon-search" size="mini" @click="handelSearch">搜索</el-button>
  15. <el-button icon="el-icon-refresh" size="mini" @click="handelReset">重置</el-button>
  16. </el-form-item>
  17. </el-form>
  18. <el-row class="czBtns">
  19. <el-col>
  20. <el-button size="mini" icon="el-icon-plus" type="primary" plain @click="handelAdd">新增</el-button>
  21. <el-button
  22. type="danger"
  23. plain
  24. icon="el-icon-delete-solid"
  25. size="mini"
  26. :disabled="multiple"
  27. @click="handelDelAll"
  28. >批量删除</el-button>
  29. <el-button
  30. size="mini"
  31. icon="el-icon-back"
  32. type="warning"
  33. plain
  34. @click="(() => { this.$router.push('/system/dict') })"
  35. >返回</el-button>
  36. </el-col>
  37. </el-row>
  38. <!-- 表格数据信息 -->
  39. <vxe-table
  40. resizable
  41. :tree-config="{ transform: true, rowField: 'id', parentField: 'parentId' }"
  42. :data="dictList"
  43. align="center"
  44. border
  45. header-row-class-name="headBackground"
  46. stripe
  47. :loading="loading"
  48. @checkbox-change="select"
  49. @checkbox-all="select"
  50. >
  51. <vxe-column type="checkbox" width="60px" />
  52. <vxe-column field="dictKey" title="字典名称" tree-node show-overflow />
  53. <vxe-column field="dictValue" title="字典值" show-overflow />
  54. <vxe-column field="sortNum" title="排序号" show-overflow />
  55. <vxe-column title="操作">
  56. <template slot-scope="scope">
  57. <el-button size="mini" type="text" icon="el-icon-edit" @click="handelEdit(scope.row)">修改
  58. </el-button>
  59. <el-button
  60. size="mini"
  61. type="text"
  62. icon="el-icon-delete"
  63. :disabled="!multiple"
  64. @click="handelDel(scope.row.id)"
  65. >删除
  66. </el-button>
  67. </template>
  68. </vxe-column>
  69. </vxe-table>
  70. <!-- 分页信息 -->
  71. <pagination
  72. v-show="total > 0"
  73. :total="total"
  74. :page.sync="queryParams.page"
  75. :limit.sync="queryParams.num"
  76. align="right"
  77. @pagination="getPageDictList"
  78. />
  79. </el-col>
  80. </el-row>
  81. <!-- 弹出框 -->
  82. <el-dialog :title="title" :visible.sync="dialogVisible" width="600px" append-to-body :close-on-click-modal="false">
  83. <el-form ref="form" :model="form" :rules="rules" label-width="80px">
  84. <el-row>
  85. <el-col :span="24">
  86. <el-form-item label="上级字典" prop="parentId">
  87. <selectTree v-model="form.parentId" :options="options" placeholder="请选择上级字典" :key-name="keyName" />
  88. </el-form-item>
  89. </el-col>
  90. </el-row>
  91. <el-row>
  92. <el-col :span="24">
  93. <el-form-item label="字典名称" prop="dictKey">
  94. <el-input v-model.trim="form.dictKey" clearable placeholder="请输入字典名称" />
  95. </el-form-item>
  96. </el-col>
  97. </el-row>
  98. <el-row>
  99. <el-col :span="12">
  100. <el-form-item label="字典值" prop="dictValue">
  101. <el-input v-model.trim="form.dictValue" clearable placeholder="请输入字典值" />
  102. </el-form-item>
  103. </el-col>
  104. <el-col :span="12">
  105. <el-form-item label="排序号" prop="sortNum">
  106. <el-input
  107. v-model.trim="form.sortNum"
  108. placeholder="请输入排序号"
  109. controls-position="right"
  110. :minlength="1"
  111. :maxlength="5"
  112. />
  113. </el-form-item>
  114. </el-col>
  115. </el-row>
  116. </el-form>
  117. <span slot="footer" class="dialog-footer">
  118. <el-button type="primary" @click="handelSubmit('form')">确 定</el-button>
  119. <el-button @click="handelCancel('form')">取 消</el-button>
  120. </span>
  121. </el-dialog>
  122. </div>
  123. </template>
  124. <script>
  125. import { delDict, addDict, getPageDictList, editDict, getDictListByType } from '@/api/system/dict.js'
  126. import selectTree from '@/components/SelectTree'
  127. export default {
  128. components: { selectTree },
  129. data() {
  130. const sortNumRule = (rule, value, callback) => {
  131. var reg = /^[1-9][0-9]*$/
  132. if (!value) {
  133. callback(new Error('排序号不能为空'))
  134. }
  135. if (!(reg.test(value))) {
  136. callback(new Error('只能输入正整数'))
  137. }
  138. callback()
  139. }
  140. return {
  141. // 遮罩层
  142. loading: false,
  143. // 查询参数
  144. queryParams: {
  145. page: 1,
  146. num: 10
  147. },
  148. // 总条数
  149. total: 0,
  150. // 表格数据
  151. dictList: [],
  152. // 弹出框
  153. dialogVisible: false,
  154. // 弹出框标题
  155. title: '',
  156. // 弹出框表单
  157. form: {
  158. parentId: 0
  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: true, 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. },
  184. created() {
  185. const { id, dictKeyType } = this.$route.query
  186. this.form.dictTypeId = id
  187. this.queryParams.dictTypeId = id
  188. this.keyType = dictKeyType
  189. this.getPageDictList()
  190. },
  191. methods: {
  192. /** 分页获取字典列表数据 */
  193. getPageDictList() {
  194. getPageDictList(this.queryParams).then(res => {
  195. if (res.code === 200 & res.data === null) {
  196. this.dictList = []
  197. this.total = 0
  198. return
  199. }
  200. const data = res.data
  201. this.dictList = this.$utils.toTreeArray(data.dictPage, { clear: true })
  202. this.total = data.count
  203. this.getDictListByType()
  204. })
  205. },
  206. /** 全量获取字典列表数据 */
  207. getDictListByType() {
  208. getDictListByType({ keyType: this.keyType }).then(res => {
  209. const data = res.data
  210. this.options = this.$utils.toArrayTree(data)
  211. this.keyName = 'dictKey'
  212. })
  213. },
  214. /** 搜索 */
  215. handelSearch(event) {
  216. this.$resetBtn(event)
  217. this.queryParams.page = 1
  218. this.getPageDictList()
  219. },
  220. /** 重置按钮操作 */
  221. handelReset(event) {
  222. this.$refs['queryForm'].resetFields()
  223. this.handelSearch(event)
  224. },
  225. /** 新增按钮 */
  226. handelAdd() {
  227. this.dialogVisible = true
  228. this.title = '添加字典'
  229. this.reset()
  230. },
  231. /** 修改按钮 */
  232. handelEdit(row) {
  233. this.form = JSON.parse(JSON.stringify(row))
  234. this.dialogVisible = true
  235. this.title = '修改字典'
  236. },
  237. /** 表格多选 */
  238. select({ $table }) {
  239. const records = $table.getCheckboxRecords()
  240. this.ids = records.map(item => item.id)
  241. this.multiple = !records.length
  242. },
  243. /** 删除按钮 */
  244. handelDel(id) {
  245. this.$confirm('您确定要删除该字典吗?', '提示', {
  246. confirmButtonText: '确定',
  247. cancelButtonText: '取消',
  248. cancelButtonClass: 'btn_custom_cancel',
  249. type: 'warning'
  250. }).then(() => {
  251. if (this.dictList.length === 1) {
  252. this.queryParams.page -= 1
  253. }
  254. this.ids.push(id)
  255. this.delDictType()
  256. }).catch(() => {
  257. this.$message({
  258. type: 'info',
  259. message: '已取消删除'
  260. })
  261. })
  262. },
  263. /** 批量删除 */
  264. handelDelAll() {
  265. this.$confirm('您确定要批量删除选中的字典吗?', '提示', {
  266. confirmButtonText: '确定',
  267. cancelButtonText: '取消',
  268. cancelButtonClass: 'btn_custom_cancel',
  269. type: 'warning'
  270. }).then(() => {
  271. if (this.ids.length === this.dictList.length) {
  272. this.queryParams.page -= 1
  273. }
  274. this.delDictType()
  275. }).catch(() => {
  276. this.$message({
  277. type: 'info',
  278. message: '已取消删除'
  279. })
  280. })
  281. },
  282. /** 删除接口 */
  283. delDictType() {
  284. delDict(this.ids).then(res => {
  285. if (res.code === 200) {
  286. this.$message({
  287. type: 'success',
  288. message: '删除成功'
  289. })
  290. this.getPageDictList()
  291. }
  292. })
  293. },
  294. /** 表单提交 */
  295. handelSubmit(formName) {
  296. this.$refs[formName].validate((valid) => {
  297. if (valid) {
  298. if (this.title === '添加字典') {
  299. addDict(this.form).then(res => {
  300. if (res.code === 200) {
  301. this.$message({
  302. message: res.data,
  303. type: 'success'
  304. })
  305. this.getPageDictList()
  306. this.dialogVisible = false
  307. }
  308. })
  309. } else {
  310. if (this.form.parentId === undefined) {
  311. this.form.parentId = 0
  312. return
  313. }
  314. editDict(this.form).then(res => {
  315. if (res.code === 200) {
  316. this.$message({
  317. message: res.data,
  318. type: 'success'
  319. })
  320. this.getPageDictList()
  321. this.dialogVisible = false
  322. }
  323. })
  324. }
  325. }
  326. })
  327. },
  328. /** 表单取消 */
  329. handelCancel() {
  330. this.dialogVisible = false
  331. this.reset()
  332. },
  333. /** 表单重置 */
  334. reset() {
  335. if (this.$refs['form']) {
  336. this.$refs['form'].resetFields()
  337. }
  338. }
  339. }
  340. }
  341. </script>
  342. <style lang="scss" rel="stylesheet/scss" scoped>
  343. </style>