当前位置:   article > 正文

Element Ui 树形组件自定义样式与功能_element ui树形组件

element ui树形组件

一、功能描述:可实现树节点内容修改、增加节点、删除节点等,根据层级不同显示不同的图标等,已封装成组件。

  1. <template>
  2. <div class="treelistitem">
  3. <el-tree
  4. :data="data"
  5. node-key="id"
  6. :render-content="renderContent"
  7. @node-click="handleNodeClick"
  8. ></el-tree>
  9. </div>
  10. </template>
  11. <script>
  12. export default {
  13. data() {
  14. return {
  15. currentId: "",
  16. currentName: "",
  17. addName: "",
  18. open: false,
  19. };
  20. },
  21. props: {
  22. data: {
  23. type: Array,
  24. default: () => [],
  25. },
  26. deletefn: {
  27. type: Function,
  28. },
  29. edietFn: {
  30. type: Function,
  31. },
  32. addFn: {
  33. type: Function,
  34. },
  35. getId: {
  36. type: Function,
  37. },
  38. opreateId: {
  39. type: Number,
  40. default: 0,
  41. },
  42. },
  43. methods: {
  44. prevent() {
  45. this.currentId = "";
  46. },
  47. ok(data, node) {
  48. if (data.text == this.currentName) {
  49. this.currentId = "";
  50. return false;
  51. }
  52. let edietData = {
  53. data,
  54. node,
  55. name: this.currentName,
  56. id: this.currentId,
  57. };
  58. this.$emit("edietFn", edietData);
  59. data.text = this.currentName;
  60. this.currentId = "";
  61. },
  62. handleNodeClick(val) {
  63. this.$emit("getId", val.id);
  64. },
  65. handleInput(val) {
  66. this.currentName = val;
  67. },
  68. handClick(value, data, node) {
  69. if (value == "1") {
  70. this.currentId = data.id;
  71. this.currentName = data.text;
  72. } else if (value == "2") {
  73. let addData = { data, node };
  74. this.$emit("addFn", addData);
  75. } else {
  76. this.$confirm("确定删除?", "提示", {
  77. confirmButtonText: "确定",
  78. cancelButtonText: "取消",
  79. type: "warning",
  80. })
  81. .then(() => {
  82. this.$emit("deletefn", data);
  83. const parent = node.parent;
  84. const children = parent.data.children || parent.data;
  85. const index = children.findIndex((d) => d.id === data.id);
  86. children.splice(index, 1);
  87. })
  88. .catch(() => {});
  89. }
  90. },
  91. renderContent(h, { node, data, store }) {
  92. return (
  93. <div class="custom-tree-node" style={{ width: "100%" }}>
  94. {!data.son ? (
  95. node.expanded ? (
  96. <i class="el-icon-folder-opened" ></i>
  97. ) : (
  98. <i class="el-icon-folder" ></i>
  99. )
  100. ) : (
  101. <i class="el-icon-document" ></i>
  102. )}
  103. {this.currentId === data.id ? (
  104. <el-input
  105. value={this.currentName}
  106. on-input={this.handleInput}
  107. style={{ width: "40%" }}
  108. ></el-input>
  109. ) : (
  110. <el-tooltip effect="dark" content={data.text} placement="top">
  111. <span class="custom-tree-node-text">{data.text}</span>
  112. </el-tooltip>
  113. )}
  114. {this.currentId === data.id ? (
  115. <span
  116. style={{ paddingLeft: "10px" }}
  117. class={this.opreateId != 3 ? "showTreeList" : "hideTreeList"}
  118. >
  119. <el-button
  120. type="info"
  121. icon="el-icon-circle-close"
  122. class="btnreturn"
  123. circle
  124. size="mini"
  125. on-click={this.prevent}
  126. ></el-button>
  127. <el-button
  128. type="primary"
  129. size="mini"
  130. icon="el-icon-check"
  131. class="btnsearch"
  132. circle
  133. on-click={() => {
  134. this.ok(data, node);
  135. }}
  136. ></el-button>
  137. </span>
  138. ) : (
  139. <el-dropdown
  140. style={{ float: "right", paddingRight: "10px" }}
  141. class={this.opreateId != 3 ? "showTreeList" : "hideTreeList"}
  142. on-command={(value) => {
  143. this.handClick(value, data, node);
  144. }}
  145. >
  146. <span class="el-dropdown-link">
  147. <i class="el-icon-arrow-down el-icon--right"></i>
  148. </span>
  149. <el-dropdown-menu slot="dropdown">
  150. <el-dropdown-item
  151. command="1"
  152. class={
  153. data.attributes.fatherId == 0
  154. ? "hideCommand"
  155. : "showCommand"
  156. }
  157. >
  158. <i class="el-icon-edit"></i>
  159. </el-dropdown-item>
  160. <el-dropdown-item command="2">
  161. <i class="el-icon-plus"></i>
  162. </el-dropdown-item>
  163. <el-dropdown-item
  164. command="3"
  165. class={
  166. data.attributes.fatherId == 0
  167. ? "hideCommand"
  168. : "showCommand"
  169. }
  170. >
  171. <i class="el-icon-delete"></i>
  172. </el-dropdown-item>
  173. </el-dropdown-menu>
  174. </el-dropdown>
  175. )}
  176. </div>
  177. );
  178. },
  179. },
  180. };
  181. </script>
  182. <style lang="scss" >
  183. .el-tree > .el-tree-node {
  184. min-width: 100%;
  185. display: inline-block;
  186. }
  187. .showCommand {
  188. display: inline-block;
  189. }
  190. .hideCommand {
  191. display: none;
  192. }
  193. .treelistitem.showTreeList {
  194. display: inline-block;
  195. }
  196. .treelistitem .hideTreeList {
  197. display: none;
  198. }
  199. .treelistitem .el-tree {
  200. width: 100% !important;
  201. }
  202. .treelistitem .el-dropdown {
  203. float: right;
  204. padding-right: 10px;
  205. }
  206. .treelistitem .custom-tree-node {
  207. width: 100%;
  208. height: 50px;
  209. line-height: 50px;
  210. font-size: 14px;
  211. }
  212. .treelistitem .custom-tree-node .custom-tree-node-text {
  213. width: 60%;
  214. height: 30px;
  215. padding-left: 10px;
  216. padding-bottom: 0.5px;
  217. display:inline-block;
  218. white-space: nowrap;
  219. text-overflow: ellipsis;
  220. overflow: hidden;
  221. }
  222. .treelistitem .el-tree-node__content {
  223. height: 50px !important;
  224. }
  225. .treelistitem .el-input {
  226. height: 20px;
  227. margin-top: -20px;
  228. margin-left: 10px;
  229. }
  230. .treelistitem .el-input__inner {
  231. height: 30px;
  232. }
  233. </style>

二、调用组件示例:

  1. <my-tree
  2. :data="treeList"
  3. :opreateId="roleId"
  4. @deletefn="delTree"
  5. @edietFn="updateTree"
  6. @addFn="addTree"
  7. @getId="getTreeId">
  8. </my-tree>

三、效果图如下:

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