当前位置:   article > 正文

Element Ui Tree组件实现增、删、改、查、拖拽节点 的树形结构_element ui 树形表格 增删改查

element ui 树形表格 增删改查

介绍:首先组件 | Element官网某些功能都具备了,这里我就把这些功能结合在一起更完美的使用,其次编辑节点官网是没有实例,所以这里搞了一套较完整的功能,其次编辑和添加,这里直接使用了弹窗(顾及到多个参数设置),接下来效果图展示!

效果图如下:

1,其中点击展开/折叠看个人需求;

2,组件中 :default-checked-keys="List_id" (如: 获取所有权限,匹配改个节点(角色)所包含的权限,修改时默认勾选,List_id 该角色拥有权限的id的集合!)讲述的可能稍有复杂,代码有注释可以看看!

(自己引入相关插件)

    src: url('../css/element-icons.woff') format("woff");这个是图标不展示问题

 这里就直接展示代码了!

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. <link rel="stylesheet" href="../css/index.css" />
  7. <link rel="stylesheet" href="../css/element-ui.css" />
  8. <script src="../js/vue.js"></script>
  9. <script src="../js/element-ui.js"></script>
  10. <style type="text/css">
  11. @font-face {
  12. font-family: element-icons;
  13. src: url('../css/element-icons.woff') format("woff");
  14. font-weight: 400;
  15. font-display: "auto";
  16. font-style: normal
  17. }
  18. .fade-enter-active,
  19. .fade-leave-active {
  20. transition: opacity 5s
  21. }
  22. .fade-enter,
  23. .fade-leave-active {
  24. opacity: 0
  25. }
  26. .custom-tree-node {
  27. flex: 1;
  28. /* display: flex; */
  29. align-items: center;
  30. justify-content: space-between;
  31. font-size: 14px;
  32. padding-right: 8px;
  33. }
  34. .show-hide:hover :nth-child(2) {
  35. display: inline-block !important;
  36. }
  37. .el-tree-node__content {
  38. height: 30px !important;
  39. }
  40. .el-input__inner {
  41. height: 28px !important;
  42. }
  43. em {
  44. color: red;
  45. font-style: inherit !important;
  46. }
  47. .el-input__inner {
  48. height: 20px;
  49. }
  50. .dialog_input>input {
  51. height: 34px !important;
  52. }
  53. input.el-input__inner {
  54. height: 34px !important;
  55. }
  56. .btn_is {
  57. padding: 7px !important;
  58. font-size: 15px !important;
  59. background: rgb(39, 99, 220);
  60. color: white;
  61. }
  62. .el-tree {
  63. overflow-y: auto;
  64. max-height: calc(100vh - 180px);
  65. border-bottom: 1px solid #c8c8c8;
  66. padding: 5px 0px;
  67. }
  68. .dialog_input {
  69. width: 400px !important;
  70. }
  71. input.el-input__inner {
  72. width: 80%;
  73. }
  74. </style>
  75. </head>
  76. <body>
  77. <div id="app">
  78. <template>
  79. <el-button class="btn_is" size="medium" @click="toggleRowExpansion()">全部展开/折叠</el-button>
  80. <el-input placeholder="输入关键字" v-model="filterText" highlight-current></el-input>
  81. <!-- default-expand-all:默认打开全部
  82. :expand-on-click-node="true" 打开点击当前行节点-->
  83. <el-tree v-if="refreshTable" :default-expand-all="isExpandAll" :data="data" class="filter-tree"
  84. :highlight-current="true" :filter-node-method="filterNode" show-checkbox node-key="id" ref="tree"
  85. highlight-current :props="defaultProps" @node-drag-start="handleDragStart"
  86. @node-drag-leave="handleDragLeave" @node-drag-over="handleDragOver" @node-drag-end="handleDragEnd"
  87. @node-drop="handleDrop" :allow-drop="allowDrop" :allow-drag="allowDrag" draggable
  88. :expand-on-click-node="false" :default-checked-keys="List_id">
  89. <span class="custom-tree-node show-hide keywords_em" slot-scope="{ node, data }">
  90. <span class="ssss" v-if="filterText != ''" v-html="ruleTitle(node.label)"></span>
  91. <span class="ddd" v-if="filterText == ''" v-text="data.label"></span>
  92. <span style="display:none">
  93. <el-button v-if="data.id !== 1" v-if="data.id!=1" type="text" size="mini"
  94. @click="() => edit(node,data)">
  95. <i class="el-icon-edit"></i>
  96. </el-button>
  97. <el-button type="text" size="mini" @click="() => append(node,data)">
  98. <i class="el-icon-plus"></i>
  99. </el-button>
  100. <el-button type="text" size="mini" @click="() => remove(node, data)">
  101. <i class="el-icon-delete"></i>
  102. </el-button>
  103. </span>
  104. </span>
  105. </el-tree>
  106. <!-- 编辑 -->
  107. <el-dialog style="width: 100%;height: 100%;" title="角色权限修改" :visible.sync="upd_dialog">
  108. <div>
  109. <el-form :model="upd_data">
  110. <el-form-item label="名称:" width="80px">
  111. <el-input class="dialog_input" v-model="upd_data.label"></el-input>
  112. </el-form-item>
  113. <el-form-item label="id:" width="80px">
  114. <el-input class="dialog_input" v-model="upd_data.id"></el-input>
  115. </el-form-item>
  116. </el-form>
  117. </div>
  118. <div slot="footer" class="dialog-footer">
  119. <button class="submitbtn" @click="upd_submit">提 交</button>
  120. <button class="censelbtn" @click="cancel">取 消</button>
  121. </div>
  122. </el-dialog>
  123. <!-- 添加 -->
  124. <el-dialog style="width: 100%;height: 100%;" title="角色权限添加" :visible.sync="zdydialog">
  125. <div>
  126. <el-form :model="add_data">
  127. <el-form-item label="名称:" width="80px">
  128. <el-input class="dialog_input" v-model="add_data.label"></el-input>
  129. </el-form-item>
  130. <el-form-item label="序号:" width="80px">
  131. <el-input class="dialog_input" v-model="add_data.id"></el-input>
  132. </el-form-item>
  133. </el-form>
  134. </div>
  135. <div slot="footer" class="dialog-footer">
  136. <button class="submitbtn" @click="add_submit">提 交</button>
  137. <button class="censelbtn" @click="cancel">取 消</button>
  138. </div>
  139. </el-dialog>
  140. </template>
  141. </div>
  142. </body>
  143. <script>
  144. new Vue({
  145. el: "#app",
  146. data() {
  147. return {
  148. data: [{
  149. id: 1,
  150. label: '一级',
  151. children: [{
  152. id: 4,
  153. label: '二级',
  154. children: [{
  155. id: 9,
  156. label: '三级'
  157. }, {
  158. id: 10,
  159. label: '三级'
  160. }]
  161. }]
  162. }, {
  163. id: 2,
  164. label: '一级',
  165. children: [{
  166. id: 5,
  167. label: '二级'
  168. }, {
  169. id: 6,
  170. label: '二级'
  171. }]
  172. }, {
  173. id: 3,
  174. label: '一级',
  175. children: [{
  176. id: 7,
  177. label: '二级'
  178. }, {
  179. id: 8,
  180. label: '二级'
  181. }]
  182. }],
  183. newApiGroupName: '',
  184. defaultProps: {
  185. children: 'children',
  186. label: 'label'
  187. },
  188. updateApiGroup: {},
  189. id: 1000,
  190. resetNodeForm: {},
  191. filterText: '',
  192. newname: '',
  193. isExpandAll: true,
  194. refreshTable: true,
  195. List_id: [5, 7], //匹配到的id默认勾选,
  196. zdydialog: false,
  197. upd_dialog: false,
  198. add_data: {},
  199. upd_data: {},
  200. };
  201. },
  202. watch: {
  203. filterText(val) {
  204. console.log(val)
  205. this.$refs.tree.filter(val);
  206. }
  207. },
  208. created() {},
  209. methods: {
  210. edit(node, row) {
  211. console.log(row)
  212. this.upd_dialog = true
  213. this.upd_data = row
  214. },
  215. upd_submit() {
  216. //调取相关接口
  217. this.upd_dialog = false
  218. },
  219. append(node, row) {
  220. this.add_data = {
  221. id: '',
  222. label: '',
  223. }
  224. if (!row.children) {
  225. this.$set(row, 'children', [])
  226. }
  227. row.children.push(this.add_data)
  228. console.log(this.add_data)
  229. this.zdydialog = true
  230. },
  231. add_submit() {
  232. //调取相关接口
  233. this.zdydialog = false
  234. },
  235. cancel() {
  236. this.upd_dialog = false;
  237. this.zdydialog = false;
  238. },
  239. // 全部展开/折叠
  240. toggleRowExpansion() {
  241. this.refreshTable = false;
  242. this.isExpandAll = !this.isExpandAll;
  243. this.$nextTick(() => {
  244. this.refreshTable = true;
  245. });
  246. },
  247. // 高亮搜索的关键字
  248. ruleTitle(items) {
  249. const title = items
  250. const rep = new RegExp(this.filterText, 'g')
  251. const resDtring = `<span style="color: #145afe">${this.filterText}</span>`
  252. return title.replace(rep, resDtring)
  253. },
  254. //筛选结点
  255. filterNode(value, data, node) {
  256. if (!value) return true;
  257. return this.findSearKey(node, value);
  258. },
  259. // 拖拽
  260. handleDragStart(node, ev) {
  261. console.log('drag start', node.data.label)
  262. },
  263. handleDragEnter(draggingNode, dropNode, ev) {
  264. console.log('tree drag enter: ', dropNode.data.label)
  265. },
  266. handleDragLeave(draggingNode, dropNode, ev) {
  267. console.log('tree drag leave: ', dropNode.data.label)
  268. },
  269. handleDragOver(draggingNode, dropNode, ev) {
  270. console.log('tree drag over: ', dropNode.data.label)
  271. },
  272. handleDragEnd(draggingNode, dropNode, dropType, ev) {
  273. console.log(
  274. 'tree drag end: ',
  275. dropNode && dropNode.data.label,
  276. dropType
  277. )
  278. // 调后端更新
  279. // this.updateApiGroup(this.data)
  280. },
  281. handleDrop(draggingNode, dropNode, dropType, ev) {
  282. console.log('tree drop: ', dropNode.data.label, dropType)
  283. },
  284. allowDrop(draggingNode, dropNode, type) {
  285. if (dropNode.data.id === 1) {
  286. return false
  287. } else {
  288. return true
  289. }
  290. },
  291. allowDrag(draggingNode) {
  292. // 顶层默认分组不允许拖拽
  293. if (draggingNode.data.id === 1) {
  294. return false
  295. } else {
  296. return true
  297. }
  298. // return draggingNode.data.apiGroupName.indexOf('三级 3-2-2') === -1
  299. },
  300. remove(node, data) {
  301. const parent = node.parent
  302. const children = parent.data.children || parent.data
  303. const index = children.findIndex(d => d.id === data.id)
  304. children.splice(index, 1)
  305. // this.updateApiGroup(this.data)
  306. },
  307. }
  308. })
  309. </script>
  310. </html>

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

闽ICP备14008679号