tree.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <template>
  2. <div>
  3. <el-tree
  4. :data="data"
  5. lazy
  6. node-key="id"
  7. :load="loadNode"
  8. :props="props"
  9. :expand-on-click-node="false"
  10. >
  11. <span slot-scope="{ node, data }">
  12. <span>{{ data.name }}</span>
  13. <span>
  14. <el-button type="text" size="mini" @click="append(node,data)">新增</el-button>
  15. <el-button type="text" size="mini" @click="replace(node, data)">修改</el-button>
  16. <el-button type="text" size="mini" @click="remove(node, data)">删除</el-button>
  17. </span>
  18. </span>
  19. </el-tree>
  20. <el-dialog :title="dialog.title" :visible="dialog.visible" @close="handleCancel">
  21. <el-form :model="dialog" label-width="120px" ref="form">
  22. <el-form-item label="名字" :label-col="{ span: 4 }" :wrapper-col="{ span: 20 }" prop="name">
  23. <el-input v-model="dialog.name" />
  24. </el-form-item>
  25. </el-form>
  26. <template slot="footer">
  27. <el-button @click="handleCancel">取消</el-button>
  28. <el-button type="primary" @click="dataFormSubmitHandle">保存</el-button>
  29. </template>
  30. </el-dialog>
  31. </div>
  32. </template>
  33. <script>
  34. import {
  35. getTreeDemoData,
  36. deleteTreeDemoData,
  37. putTreeDemoData,
  38. postTreeDemoData
  39. } from "@/api/treeDemo";
  40. export default {
  41. name: "tree_demo",
  42. data() {
  43. return {
  44. props: {
  45. isLeaf: (data, node) => this.isChi(data, node)
  46. },
  47. data: [],
  48. dialog: {
  49. title: "",
  50. visible: false,
  51. name: "",
  52. id: -1,
  53. parentId: -1
  54. }
  55. };
  56. },
  57. methods: {
  58. // 处理后端传来的参数 是否有孩子节点和前端的是否为叶子节点判断相反
  59. isChi: function(data, node) {
  60. return !data.childer;
  61. },
  62. // 新增按钮
  63. append: function(node, data) {
  64. this.dialog.title = "新增";
  65. this.dialog.visible = true;
  66. this.id = -1;
  67. this.dialog.parentId = data.parent_id;
  68. },
  69. // 修改按钮
  70. replace: function(node, data) {
  71. this.dialog.title = "编辑";
  72. this.dialog.name = data.name;
  73. this.dialog.id = data.id;
  74. this.dialog.visible = true;
  75. console.log(data);
  76. },
  77. // 删除按钮
  78. remove: function(node, data) {
  79. deleteTreeDemoData({
  80. id: data.id
  81. }).then(res => {
  82. this.$message({
  83. message: "删除成功",
  84. type: "warning"
  85. });
  86. });
  87. },
  88. // 加载子树数据的方法
  89. loadNode: function(node, resolve) {
  90. getTreeDemoData({
  91. parent: node.data.id
  92. }).then(res => {
  93. return resolve(res.data);
  94. });
  95. },
  96. // 弹窗的取消按钮
  97. handleCancel: function() {
  98. this.dialog.name = "";
  99. this.dialog.visible = false;
  100. },
  101. // 弹窗的确定按钮
  102. dataFormSubmitHandle: function() {
  103. if (this.dialog.id == -1) {
  104. postTreeDemoData({
  105. id: this.dialog.parentId,
  106. name: this.dialog.name
  107. }).then(res => {
  108. this.$message({
  109. message: "新增成功",
  110. type: "success"
  111. });
  112. });
  113. this.dialog.name = "";
  114. this.dialog.visible = false;
  115. } else {
  116. putTreeDemoData({
  117. id: this.dialog.id,
  118. name: this.dialog.name
  119. }).then(res => {
  120. this.$message({
  121. message: "更新成功",
  122. type: "success"
  123. });
  124. });
  125. this.dialog.name = "";
  126. this.dialog.visible = false;
  127. }
  128. }
  129. }
  130. };
  131. </script>