index.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <template>
  2. <div class="app-container">
  3. <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
  4. <el-form-item label="树节点名" prop="treeName">
  5. <el-input
  6. v-model="queryParams.treeName"
  7. placeholder="请输入树节点名"
  8. clearable
  9. size="small"
  10. @keyup.enter.native="handleQuery"
  11. />
  12. </el-form-item>
  13. <el-form-item label="创建时间">
  14. <el-date-picker
  15. v-model="daterangeCreateTime"
  16. size="small"
  17. style="width: 240px"
  18. value-format="yyyy-MM-dd"
  19. type="daterange"
  20. range-separator="-"
  21. start-placeholder="开始日期"
  22. end-placeholder="结束日期"
  23. ></el-date-picker>
  24. </el-form-item>
  25. <el-form-item>
  26. <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
  27. <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
  28. </el-form-item>
  29. </el-form>
  30. <el-row :gutter="10" class="mb8">
  31. <el-col :span="1.5">
  32. <el-button
  33. type="primary"
  34. plain
  35. icon="el-icon-plus"
  36. size="mini"
  37. @click="handleAdd"
  38. v-hasPermi="['demo:tree:add']"
  39. >新增</el-button>
  40. </el-col>
  41. <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
  42. </el-row>
  43. <el-table
  44. v-loading="loading"
  45. :data="treeList"
  46. row-key="id"
  47. default-expand-all
  48. :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
  49. >
  50. <el-table-column label="父id" prop="parentId" />
  51. <el-table-column label="部门id" align="center" prop="deptId" />
  52. <el-table-column label="用户id" align="center" prop="userId" />
  53. <el-table-column label="树节点名" align="center" prop="treeName" />
  54. <el-table-column label="创建时间" align="center" prop="createTime" width="180">
  55. <template slot-scope="scope">
  56. <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
  57. </template>
  58. </el-table-column>
  59. <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
  60. <template slot-scope="scope">
  61. <el-button
  62. size="mini"
  63. type="text"
  64. icon="el-icon-edit"
  65. @click="handleUpdate(scope.row)"
  66. v-hasPermi="['demo:tree:edit']"
  67. >修改</el-button>
  68. <el-button
  69. size="mini"
  70. type="text"
  71. icon="el-icon-plus"
  72. @click="handleAdd(scope.row)"
  73. v-hasPermi="['demo:tree:add']"
  74. >新增</el-button>
  75. <el-button
  76. size="mini"
  77. type="text"
  78. icon="el-icon-delete"
  79. @click="handleDelete(scope.row)"
  80. v-hasPermi="['demo:tree:remove']"
  81. >删除</el-button>
  82. </template>
  83. </el-table-column>
  84. </el-table>
  85. <!-- 添加或修改测试树表对话框 -->
  86. <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
  87. <el-form ref="form" :model="form" :rules="rules" label-width="80px">
  88. <el-form-item label="父id" prop="parentId">
  89. <treeselect v-model="form.parentId" :options="treeOptions" :normalizer="normalizer" placeholder="请选择父id" />
  90. </el-form-item>
  91. <el-form-item label="部门id" prop="deptId">
  92. <el-input v-model="form.deptId" placeholder="请输入部门id" />
  93. </el-form-item>
  94. <el-form-item label="用户id" prop="userId">
  95. <el-input v-model="form.userId" placeholder="请输入用户id" />
  96. </el-form-item>
  97. <el-form-item label="树节点名" prop="treeName">
  98. <el-input v-model="form.treeName" placeholder="请输入树节点名" />
  99. </el-form-item>
  100. </el-form>
  101. <div slot="footer" class="dialog-footer">
  102. <el-button type="primary" @click="submitForm">确 定</el-button>
  103. <el-button @click="cancel">取 消</el-button>
  104. </div>
  105. </el-dialog>
  106. </div>
  107. </template>
  108. <script>
  109. import { listTree, getTree, delTree, addTree, updateTree, exportTree } from "@/api/demo/tree";
  110. import Treeselect from "@riophae/vue-treeselect";
  111. import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  112. export default {
  113. name: "Tree",
  114. components: {
  115. Treeselect
  116. },
  117. data() {
  118. return {
  119. // 遮罩层
  120. loading: true,
  121. // 显示搜索条件
  122. showSearch: true,
  123. // 测试树表表格数据
  124. treeList: [],
  125. // 测试树表树选项
  126. treeOptions: [],
  127. // 弹出层标题
  128. title: "",
  129. // 是否显示弹出层
  130. open: false,
  131. // 创建时间时间范围
  132. daterangeCreateTime: [],
  133. // 查询参数
  134. queryParams: {
  135. treeName: null,
  136. createTime: null,
  137. },
  138. // 表单参数
  139. form: {},
  140. // 表单校验
  141. rules: {
  142. treeName: [
  143. { required: true, message: "树节点名不能为空", trigger: "blur" }
  144. ],
  145. }
  146. };
  147. },
  148. created() {
  149. this.getList();
  150. },
  151. methods: {
  152. /** 查询测试树表列表 */
  153. getList() {
  154. this.loading = true;
  155. this.queryParams.params = {};
  156. if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
  157. this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
  158. this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
  159. }
  160. listTree(this.queryParams).then(response => {
  161. this.treeList = this.handleTree(response.data, "id", "parentId");
  162. this.loading = false;
  163. });
  164. },
  165. /** 转换测试树表数据结构 */
  166. normalizer(node) {
  167. if (node.children && !node.children.length) {
  168. delete node.children;
  169. }
  170. return {
  171. id: node.id,
  172. label: node.treeName,
  173. children: node.children
  174. };
  175. },
  176. /** 查询测试树表下拉树结构 */
  177. getTreeselect() {
  178. listTree().then(response => {
  179. this.treeOptions = [];
  180. const data = { id: 0, treeName: '顶级节点', children: [] };
  181. data.children = this.handleTree(response.data, "id", "parentId");
  182. this.treeOptions.push(data);
  183. });
  184. },
  185. // 取消按钮
  186. cancel() {
  187. this.open = false;
  188. this.reset();
  189. },
  190. // 表单重置
  191. reset() {
  192. this.form = {
  193. id: null,
  194. parentId: null,
  195. deptId: null,
  196. userId: null,
  197. treeName: null,
  198. version: null,
  199. createTime: null,
  200. createBy: null,
  201. updateTime: null,
  202. updateBy: null,
  203. delFlag: null
  204. };
  205. this.resetForm("form");
  206. },
  207. /** 搜索按钮操作 */
  208. handleQuery() {
  209. this.getList();
  210. },
  211. /** 重置按钮操作 */
  212. resetQuery() {
  213. this.daterangeCreateTime = [];
  214. this.resetForm("queryForm");
  215. this.handleQuery();
  216. },
  217. /** 新增按钮操作 */
  218. handleAdd(row) {
  219. this.reset();
  220. this.getTreeselect();
  221. if (row != null && row.id) {
  222. this.form.parentId = row.id;
  223. } else {
  224. this.form.parentId = 0;
  225. }
  226. this.open = true;
  227. this.title = "添加测试树表";
  228. },
  229. /** 修改按钮操作 */
  230. handleUpdate(row) {
  231. this.loading = true;
  232. this.reset();
  233. this.getTreeselect();
  234. if (row != null) {
  235. this.form.parentId = row.id;
  236. }
  237. getTree(row.id).then(response => {
  238. this.loading = false;
  239. this.form = response.data;
  240. this.open = true;
  241. this.title = "修改测试树表";
  242. });
  243. },
  244. /** 提交按钮 */
  245. submitForm() {
  246. this.$refs["form"].validate(valid => {
  247. if (valid) {
  248. this.loading = true;
  249. if (this.form.id != null) {
  250. updateTree(this.form).then(response => {
  251. this.loading = false;
  252. this.msgSuccess("修改成功");
  253. this.open = false;
  254. this.getList();
  255. });
  256. } else {
  257. addTree(this.form).then(response => {
  258. this.loading = false;
  259. this.msgSuccess("新增成功");
  260. this.open = false;
  261. this.getList();
  262. });
  263. }
  264. }
  265. });
  266. },
  267. /** 删除按钮操作 */
  268. handleDelete(row) {
  269. this.$confirm('是否确认删除测试树表编号为"' + row.id + '"的数据项?', "警告", {
  270. confirmButtonText: "确定",
  271. cancelButtonText: "取消",
  272. type: "warning"
  273. }).then(() => {
  274. this.loading = true;
  275. return delTree(row.id);
  276. }).then(() => {
  277. this.loading = false;
  278. this.getList();
  279. this.msgSuccess("删除成功");
  280. })
  281. }
  282. }
  283. };
  284. </script>