dept.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <template>
  2. <div class="app-container">
  3. <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true">
  4. <el-form-item label="部门名称" prop="deptName">
  5. <el-input
  6. v-model="queryParams.deptName"
  7. placeholder="请输入部门名称"
  8. clearable
  9. @keyup.enter.native="handleQuery"
  10. />
  11. </el-form-item>
  12. <el-form-item label="状态" prop="status">
  13. <el-select v-model="queryParams.status" placeholder="部门状态" clearable>
  14. <el-option :key="0" label="正常" :value="0"/>
  15. <el-option :key="1" label="停用" :value="1"/>
  16. </el-select>
  17. </el-form-item>
  18. <el-form-item>
  19. <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
  20. <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
  21. </el-form-item>
  22. </el-form>
  23. <el-row :gutter="10" class="mb8">
  24. <el-col :span="1.5">
  25. <el-button
  26. type="primary"
  27. plain
  28. icon="el-icon-plus"
  29. size="mini"
  30. @click="handleAdd"
  31. v-hasPermi="['system:dept:add']"
  32. >新增
  33. </el-button>
  34. </el-col>
  35. <el-col :span="1.5">
  36. <el-button
  37. type="info"
  38. plain
  39. icon="el-icon-sort"
  40. size="mini"
  41. @click="toggleExpandAll"
  42. >展开/折叠
  43. </el-button>
  44. </el-col>
  45. </el-row>
  46. <el-table
  47. v-if="refreshTable"
  48. :data="deptList"
  49. row-key="id"
  50. size="mini"
  51. :default-expand-all="isExpandAll"
  52. :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
  53. <el-table-column prop="deptName" label="部门名称" width="260"></el-table-column>
  54. <el-table-column prop="orderNum" label="排序" width="200"></el-table-column>
  55. <el-table-column prop="status" label="状态" width="100">
  56. <template slot-scope="scope">
  57. <el-tag type="success" size="small" v-if="scope.row.status==0">正常</el-tag>
  58. <el-tag type="info" size="small" v-else>停用</el-tag>
  59. </template>
  60. </el-table-column>
  61. <el-table-column label="创建时间" align="center" prop="createTime" width="200">
  62. <template slot-scope="scope">
  63. <span>{{ parseTime(scope.row.createTime) }}</span>
  64. </template>
  65. </el-table-column>
  66. <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
  67. <template slot-scope="scope">
  68. <el-button
  69. size="mini"
  70. type="text"
  71. icon="el-icon-edit"
  72. @click="handleUpdate(scope.row)"
  73. v-hasPermi="['system:dept:edit']"
  74. >修改
  75. </el-button>
  76. <el-button
  77. size="mini"
  78. type="text"
  79. icon="el-icon-plus"
  80. @click="handleAdd(scope.row)"
  81. v-hasPermi="['system:dept:add']"
  82. >新增
  83. </el-button>
  84. <el-button
  85. v-if="scope.row.parentId != 0"
  86. size="mini"
  87. type="text"
  88. icon="el-icon-delete"
  89. @click="handleDelete(scope.row)"
  90. v-hasPermi="['system:dept:delete']"
  91. >删除
  92. </el-button>
  93. </template>
  94. </el-table-column>
  95. </el-table>
  96. <!-- 添加或修改部门对话框 -->
  97. <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body :close-on-click-modal="false">
  98. <el-form ref="form" :model="form" :rules="rules" label-width="80px">
  99. <el-row>
  100. <el-col :span="24" v-if="form.parentId !== 0">
  101. <el-form-item label="上级部门" prop="parentId">
  102. <treeselect v-model="form.parentId" :options="deptOptions" :normalizer="normalizer" placeholder="选择上级部门"/>
  103. </el-form-item>
  104. </el-col>
  105. </el-row>
  106. <el-row>
  107. <el-col :span="12">
  108. <el-form-item label="部门名称" prop="deptName">
  109. <el-input v-model="form.deptName" placeholder="请输入部门名称"/>
  110. </el-form-item>
  111. </el-col>
  112. <el-col :span="12">
  113. <el-form-item label="显示排序" prop="orderNum">
  114. <el-input-number v-model="form.orderNum" controls-position="right" :min="0"/>
  115. </el-form-item>
  116. </el-col>
  117. </el-row>
  118. <el-row>
  119. <el-col :span="12">
  120. <el-form-item label="负责人" prop="leader">
  121. <el-input v-model="form.leader" placeholder="请输入负责人" maxlength="20"/>
  122. </el-form-item>
  123. </el-col>
  124. <el-col :span="12">
  125. <el-form-item label="联系电话" prop="phone">
  126. <el-input v-model="form.phone" placeholder="请输入联系电话" maxlength="11"/>
  127. </el-form-item>
  128. </el-col>
  129. </el-row>
  130. <el-row>
  131. <el-col :span="12">
  132. <el-form-item label="邮箱" prop="email">
  133. <el-input v-model="form.email" placeholder="请输入邮箱" maxlength="50"/>
  134. </el-form-item>
  135. </el-col>
  136. <el-col :span="12">
  137. <el-form-item label="部门状态" prop="status">
  138. <el-radio-group v-model="form.status">
  139. <el-radio label="0">正常</el-radio>
  140. <el-radio label="1">停用</el-radio>
  141. </el-radio-group>
  142. </el-form-item>
  143. </el-col>
  144. </el-row>
  145. </el-form>
  146. <div slot="footer" class="dialog-footer">
  147. <el-button type="primary" @click="submitForm">确 定</el-button>
  148. <el-button @click="cancel">取 消</el-button>
  149. </div>
  150. </el-dialog>
  151. </div>
  152. </template>
  153. <script>
  154. import {listDept, getDept, delDept, addDept, updateDept, listDeptExcludeChild} from "@/api/system/dept";
  155. import Treeselect from "@riophae/vue-treeselect";
  156. import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  157. export default {
  158. name: "Dept",
  159. components: {Treeselect},
  160. data() {
  161. return {
  162. // 表格树数据
  163. deptList: [],
  164. // 部门树选项
  165. deptOptions: [],
  166. // 弹出层标题
  167. title: "",
  168. // 是否显示弹出层
  169. open: false,
  170. // 是否展开,默认全部展开
  171. isExpandAll: true,
  172. // 重新渲染表格状态
  173. refreshTable: true,
  174. // 查询参数
  175. queryParams: {
  176. deptName: undefined,
  177. status: undefined
  178. },
  179. // 表单参数
  180. form: {},
  181. // 表单校验
  182. rules: {
  183. parentId: [
  184. {required: true, message: "上级部门不能为空", trigger: "blur"}
  185. ],
  186. deptName: [
  187. {required: true, message: "部门名称不能为空", trigger: "blur"}
  188. ],
  189. orderNum: [
  190. {required: true, message: "显示排序不能为空", trigger: "blur"}
  191. ],
  192. email: [
  193. {
  194. type: "email",
  195. message: "请输入正确的邮箱地址",
  196. trigger: ["blur", "change"]
  197. }
  198. ],
  199. phone: [
  200. {
  201. pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
  202. message: "请输入正确的手机号码",
  203. trigger: "blur"
  204. }
  205. ]
  206. }
  207. };
  208. },
  209. created() {
  210. this.getList();
  211. },
  212. methods: {
  213. /** 查询部门列表 */
  214. getList() {
  215. this.loading = true;
  216. listDept(this.queryParams).then(response => {
  217. this.deptList = this.handleTree(response.data, "id");
  218. this.loading = false;
  219. });
  220. },
  221. // 取消按钮
  222. cancel() {
  223. this.open = false;
  224. this.reset();
  225. },
  226. // 表单重置
  227. reset() {
  228. this.form = {
  229. id: undefined,
  230. parentId: undefined,
  231. deptName: undefined,
  232. orderNum: undefined,
  233. leader: undefined,
  234. phone: undefined,
  235. email: undefined,
  236. status: "0"
  237. };
  238. this.resetForm("form");
  239. },
  240. /** 搜索按钮操作 */
  241. handleQuery() {
  242. this.getList();
  243. },
  244. /** 重置按钮操作 */
  245. resetQuery() {
  246. this.resetForm("queryForm");
  247. this.handleQuery();
  248. },
  249. /** 转换部门数据结构 */
  250. normalizer(node) {
  251. if (node.children && !node.children.length) {
  252. delete node.children;
  253. }
  254. return {
  255. id: node.id,
  256. label: node.deptName,
  257. children: node.children
  258. };
  259. },
  260. /** 新增按钮操作 */
  261. handleAdd(row) {
  262. this.reset();
  263. if (row != undefined) {
  264. this.form.parentId = row.id;
  265. }
  266. this.open = true;
  267. this.title = "添加部门";
  268. listDept().then(response => {
  269. this.deptOptions = this.handleTree(response.data, "id");
  270. });
  271. },
  272. /** 展开/折叠操作 */
  273. toggleExpandAll() {
  274. this.refreshTable = false;
  275. this.isExpandAll = !this.isExpandAll;
  276. this.$nextTick(() => {
  277. this.refreshTable = true;
  278. });
  279. },
  280. /** 修改按钮操作 */
  281. handleUpdate(row) {
  282. this.reset();
  283. getDept(row.id).then(response => {
  284. this.form = response.data;
  285. this.open = true;
  286. this.title = "修改部门";
  287. listDeptExcludeChild(row.id).then(response => {
  288. this.deptOptions = this.handleTree(response.data, "id");
  289. if (this.deptOptions.length == 0) {
  290. const noResultsOptions = {id: this.form.parentId, deptName: this.form.parentName, children: []};
  291. this.deptOptions.push(noResultsOptions);
  292. }
  293. });
  294. });
  295. },
  296. /** 提交按钮 */
  297. submitForm() {
  298. this.$refs["form"].validate(valid => {
  299. if (valid) {
  300. if (this.form.id != undefined) {
  301. updateDept(this.form).then(response => {
  302. this.open = false;
  303. this.getList();
  304. });
  305. } else {
  306. addDept(this.form).then(response => {
  307. this.open = false;
  308. this.getList();
  309. });
  310. }
  311. }
  312. });
  313. },
  314. /** 删除按钮操作 */
  315. handleDelete(row) {
  316. this.$confirm('是否确认删除名称为"' + row.deptName + '"的数据项?').then(function () {
  317. return delDept(row.id);
  318. }).then(() => {
  319. this.getList();
  320. }).catch(() => {
  321. });
  322. }
  323. }
  324. };
  325. </script>