dict-detail.vue 10 KB

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