当前位置:   article > 正文

element-ui实现树的拖拽及树节点的增删改查_elementui tree 拖拽

elementui tree 拖拽

代码如下

界面效果

界面代码:

  1. <el-button class="comp-tr-top" type="primary" size="small" @click="handleAddTop">添加顶级节点</el-button>
  2. <div class="treenode-container">
  3. <el-tree
  4. :data="deptOptions"
  5. :props="defaultProps"
  6. :expand-on-click-node="false"
  7. ref="tree"
  8. class="flow-tree"
  9. default-expand-all
  10. @node-drag-start="handleDragStart"
  11. @node-drag-enter="handleDragEnter"
  12. @node-drag-leave="handleDragLeave"
  13. @node-drag-over="handleDragOver"
  14. @node-drag-end="handleDragEnd"
  15. @node-drop="handleDrop"
  16. draggable
  17. @node-click="handleTreeNodeClick"
  18. >
  19. <template slot-scope="{ node, data }">
  20. <div :id="node.id" class="custom-tree-node">
  21. <template v-if="data.isHead">
  22. <el-input
  23. v-model="data.name"
  24. autofocus
  25. size="mini"
  26. :ref="'slotTreeInputHead'"
  27. @blur.stop="handleHeadAdd(node, data)"
  28. @keyup.enter.native="$event.target.blur()"
  29. ></el-input>
  30. </template>
  31. <template v-if="data.isAdd && !data.isHead">
  32. <el-input
  33. v-model="data.name"
  34. autofocus
  35. size="mini"
  36. :ref="'slotTreeInput'+data.parentId"
  37. @blur.stop="handleAdd(node, data)"
  38. @keyup.enter.native="$event.target.blur()"
  39. ></el-input>
  40. </template>
  41. <template v-if="node.isEdit">
  42. <el-input
  43. v-model="data.name"
  44. autofocus
  45. size="mini"
  46. :ref="'slotTreeInput'+data.id"
  47. @blur.stop="handleInput(node, data)"
  48. @keyup.enter.native="$event.target.blur()"
  49. ></el-input>
  50. </template>
  51. <template v-else>
  52. <span
  53. class="el-tree-icon"
  54. :class="data.children ? node.expanded ? 'el-folder-opened-icon' : 'el-folder-icon' : 'el-document-icon'"
  55. />
  56. <span>{{ data.name }}</span>
  57. <span v-if="node.isCurrent && !data.isHead && !data.isAdd">
  58. <el-button type="text" size="mini" @click="() => appendTree(node, data)">新增</el-button>
  59. <el-button type="text" size="mini" @click="() => removeTree(node, data)">删除</el-button>
  60. <el-button type="text" size="mini" @click="() => updateTree(node, data)">改名</el-button>
  61. </span>
  62. </template>
  63. </div>
  64. </template>
  65. </el-tree>
  66. </div>

data中定义: 

  1. deptOptions: [],
  2. defaultProps: {
  3. children: "children",
  4. label: "name"
  5. },
  6. //是否存在顶级节点的新增
  7. isHeadAdd: false,
  8. initParam: {
  9. // 新增参数
  10. name: "",
  11. isAdd: true,
  12. children: []
  13. },
  14. initParamHead: {
  15. // 新增参数
  16. name: "",
  17. isAdd: true,
  18. isHead: true,
  19. children: [],
  20. parentId : 0, // 父id
  21. parentIds:0
  22. },
  23. //是否新增
  24. isAdd: false,

接口数据:

  methods方法

  1. updateTree(_node, _data) {
  2. if (this.isAdd) {
  3. this.$message.error("请完成上次操作后再次点击!");
  4. return;
  5. }
  6. // 设置编辑状态
  7. if (!_node.isEdit) {
  8. this.$set(_node, "isEdit", true);
  9. }
  10. // 输入框聚焦
  11. this.$nextTick(() => {
  12. if (this.$refs["slotTreeInput" + _data.id]) {
  13. this.$refs["slotTreeInput" + _data.id].$refs.input.focus();
  14. }
  15. });
  16. },
  17. handleInput(_node, _data) {
  18. // 修改节点
  19. // 退出编辑状态
  20. if (_node.isEdit) {
  21. if (_data.name && _data.name.length > 0) {
  22. updateFolderTree(_data).then(res => {
  23. if (res.msg === "ok") {
  24. this.isAdd = false;
  25. this.$message({
  26. message: "修改成功",
  27. type: "success"
  28. });
  29. this.$set(_node, "isEdit", false);
  30. this.getTreeselect();
  31. } else {
  32. this.$message({
  33. message: "修改失败",
  34. type: "error"
  35. });
  36. }
  37. });
  38. } else {
  39. this.$message.error("名称不得为空!");
  40. this.isAdd = false;
  41. return;
  42. }
  43. }
  44. },
  45. appendTree(_node, _data) {
  46. if (this.isAdd) {
  47. this.$message.error("请新增完毕后再次点击!");
  48. return;
  49. }
  50. this.isAdd = true;
  51. // 新增节点
  52. // 参数修改
  53. let obj = JSON.parse(JSON.stringify(this.initParam)); // copy参数
  54. obj.parentId = _data.id; // 父id
  55. obj.parentIds = _data.parentIds + "," + _data.id; // 父id
  56. // 判断字段是否存在
  57. if (!_data.children) {
  58. this.$set(_data, "children", []);
  59. }
  60. // 新增数据
  61. _data.children.push(obj);
  62. this.$nextTick(() => {
  63. if (this.$refs["slotTreeInput" + _data.id]) {
  64. this.$refs["slotTreeInput" + _data.id].$refs.input.focus();
  65. }
  66. });
  67. // 展开节点
  68. if (!_node.expanded) {
  69. _node.expanded = true;
  70. }
  71. },
  72. handleHeadAdd(_node, _data) {
  73. // 修改节点
  74. // 退出编辑状态
  75. if (_data.isHead) {
  76. if (_data.name && _data.name.length > 0) {
  77. saveFolderTree(_data).then(res => {
  78. if (res.msg === "ok") {
  79. this.$message({
  80. message: "新增成功",
  81. type: "success"
  82. });
  83. this.$set(_data, "isHead", false);
  84. this.isAdd = false;
  85. this.getTreeselect();
  86. } else {
  87. this.$message({
  88. message: "新增失败",
  89. type: "error"
  90. });
  91. }
  92. });
  93. } else {
  94. this.isAdd = false;
  95. this.$message.error("名称不得为空!");
  96. this.getTreeselect();
  97. }
  98. }
  99. },
  100. handleAdd(_node, _data) {
  101. // 修改节点
  102. // 退出编辑状态
  103. if (_data.isAdd) {
  104. if (_data.name && _data.name.length > 0) {
  105. saveFolderTree(_data).then(res => {
  106. if (res.msg === "ok") {
  107. this.$message({
  108. message: "新增成功",
  109. type: "success"
  110. });
  111. this.isAdd = false;
  112. this.getTreeselect();
  113. } else {
  114. this.$message({
  115. message: "新增失败",
  116. type: "error"
  117. });
  118. }
  119. });
  120. } else {
  121. this.$message.error("名称不得为空!");
  122. this.isAdd = false;
  123. this.getTreeselect();
  124. // return;
  125. }
  126. this.$set(_data, "isAdd", false);
  127. }
  128. },
  129. handleAddTop() {
  130. if (!this.isAdd) {
  131. // 添加顶部节点
  132. this.isAdd = true;
  133. let obj = JSON.parse(JSON.stringify(this.initParamHead)); // copy参数
  134. if(!(this.deptOptions && this.deptOptions.length>0)){
  135. this.deptOptions = [];
  136. }
  137. this.deptOptions.push(obj);
  138. this.$nextTick(() => {
  139. if (this.$refs["slotTreeInputHead"]) {
  140. this.$refs["slotTreeInputHead"].$refs.input.focus();
  141. }
  142. });
  143. } else {
  144. this.$message.error("请新增完毕后再次点击!");
  145. return false;
  146. }
  147. },
  148. removeTree(_node, _data) {
  149. if (this.isAdd) {
  150. this.$message.error("请完成上次操作后再次点击!");
  151. return;
  152. }
  153. // 删除节点
  154. // 判断是否存在子节点
  155. if (_data.children && _data.children.length !== 0) {
  156. this.$message.error("此节点有子级,不可删除!");
  157. return false;
  158. } else {
  159. // 删除操作
  160. let DeletOprate = () => {
  161. this.$nextTick(() => {
  162. if (this.$refs.tree) {
  163. this.$refs.tree.remove(_data);
  164. this.deleteData(_data);
  165. // this.$message.success("删除成功!")
  166. }
  167. });
  168. };
  169. // 二次确认
  170. let ConfirmFun = () => {
  171. this.$confirm("是否删除此节点?", "提示", {
  172. confirmButtonText: "确认",
  173. cancelButtonText: "取消",
  174. type: "warning"
  175. })
  176. .then(() => {
  177. DeletOprate();
  178. })
  179. .catch(() => {});
  180. };
  181. // 判断是否新增: 新增节点直接删除,已存在的节点要二次确认
  182. _data[this.id] < this.NODE_ID_START ? DeletOprate() : ConfirmFun();
  183. }
  184. },
  185. handleDragStart(node, ev) {
  186. },
  187. handleDragEnter(draggingNode, dropNode, ev) {
  188. },
  189. handleDragLeave(draggingNode, dropNode, ev) {
  190. },
  191. handleDragOver(draggingNode, dropNode, ev) {
  192. },
  193. handleDragEnd(draggingNode, dropNode, dropType, ev) {
  194. },
  195. handleDrop(draggingNode, dropNode, dropType, ev) {
  196. var data = dropType != "inner" ? dropNode.parent.data : dropNode.data;
  197. let updateData = draggingNode.data;
  198. updateData.parentId = dropType != "inner" ? data.parentId : data.id;
  199. updateData.parentIds =
  200. dropType != "inner" ? data.parentIds : data.parentIds + "," + data.id;
  201. if (!updateData.id) {
  202. return;
  203. }
  204. //接口方法
  205. update(updateData).then(res => {
  206. if (res.msg === "ok") {
  207. this.$message({
  208. message: "拖拽成功",
  209. type: "success"
  210. });
  211. this.getTreeselect();
  212. } else {
  213. this.$message({
  214. message: "拖拽失败",
  215. type: "error"
  216. });
  217. }
  218. });
  219. },

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/119207
推荐阅读
相关标签
  

闽ICP备14008679号