当前位置:   article > 正文

element-ui组件库中的Tree树形控件,鼠标右键节点,显示新增,删除,编辑等操作(拖拽,切换等等)_element tree 右键功能

element tree 右键功能

其中包括的方法:

1)节点被点击时候的回调

2)当某一节点被鼠标右键点击时会触发的事件

3)节点开始拖拽时触发的事件

4)拖拽进入其他节点时触发的事件

5)拖拽离开某个节点时触发的事件

6)在拖拽节点时触发的事件(类似浏览器的 mouseover 事件)

7)拖拽结束时(可能未成功)触发的事件

8)判断节点能否被拖拽

9)拖拽时判定目标节点能否被放置。type 参数有三种情况:'prev'、'inner' 和 'next',分别表示放置在目标节点前、插入至目标节点和放置在目标节点后

下面代码里面都有详细说明 需要哪一项功能请自行断查

  1. <template>
  2. <div class="lalala">
  3.     <el-input
  4.           placeholder="输入关键字进行过滤"
  5.           v-model="filterText"
  6.         class="search">
  7.     </el-input>
  8.     <el-tree
  9.           :data="treeData"
  10.           node-key="id"
  11.           default-expand-all
  12.           @node-click="handleLeftclick"
  13.           @node-drag-start="handleDragStart"
  14.           @node-drag-enter="handleDragEnter"
  15.           @node-drag-leave="handleDragLeave"
  16.           @node-drag-over="handleDragOver"
  17.           @node-drag-end="handleDragEnd"
  18.           @node-drop="handleDrop"
  19.           @node-contextmenu="rightClick"
  20.           :filter-node-method="filterNode"
  21.           draggable
  22.           :allow-drop="allowDrop"
  23.           :allow-drag="allowDrag"
  24.         ref="tree">
  25.             <span class="slot-t-node" slot-scope="{ node, data }">
  26.                 <span v-show="!data.isEdit">
  27.                 <span :class="[data.id>= 99 ? 'slot-t-node--label' : '']">{{node.label}}</span>
  28.                 </span>
  29.             <span v-show="data.isEdit">
  30.                 <el-input class="slot-t-input" size="mini" autofocus
  31.                       v-model="data.label"
  32.                       :ref="'slotTreeInput'+data.id"
  33.                       @blur.stop="NodeBlur(node,data)"
  34.                       @keydown.native.enter="NodeBlur(node,data)"></el-input>
  35.             </span>
  36.         </span>
  37.     </el-tree>
  38.     <el-card class="box-card" ref="card" v-show="menuVisible">
  39.         <div @click="addSameLevelNode()" v-show="firstLevel">
  40.             <i class="el-icon-circle-plus-outline"></i>&nbsp;&nbsp;同级增加
  41.         </div>
  42.         <div class="add" @click="addChildNode()">
  43.             <i class="el-icon-circle-plus-outline"></i>&nbsp;&nbsp;子级增加
  44.         </div>
  45.         <div class="delete" @click="deleteNode()">
  46.             <i class="el-icon-remove-outline"></i>&nbsp;&nbsp;删除节点
  47.         </div>
  48.         <div class="edit" @click="editNode()">
  49.             <i class="el-icon-edit"></i>&nbsp;&nbsp;修改节点
  50.         </div>
  51.     </el-card>
  52. </div>
  53. </template>
  54. <script>
  55. import '../mock/mockfile.js'
  56.   import axios from 'axios'
  57.   export default {
  58. name:'processManagement',
  59. data () {
  60. return {
  61. eleId:'',
  62. isShow:false,
  63. currentData:'',
  64. currentNode:'',
  65. menuVisible:false,
  66. firstLevel:false,
  67. filterText:'',
  68. maxexpandId:4,
  69. treeData: [{
  70. id:1,
  71. label:'一级 1',
  72. isEdit:false,
  73. children: [{
  74. id:4,
  75. label:'二级 1-1',
  76. isEdit:false,
  77. children: [{
  78. id:9,
  79. label:'三级 1-1-1',
  80. isEdit:false
  81.             }, {
  82. id:10,
  83. label:'三级 1-1-2',
  84. isEdit:false
  85.             }]
  86. }]
  87. }, {
  88. id:2,
  89. label:'一级 2',
  90. isEdit:false,
  91. children: [{
  92. id:5,
  93. label:'二级 2-1',
  94. isEdit:false
  95.           }, {
  96. id:6,
  97. label:'二级 2-2',
  98. isEdit:false
  99.           }]
  100. }, {
  101. id:3,
  102. label:'一级 3',
  103. isEdit:false,
  104. children: [{
  105. id:7,
  106. label:'二级 3-1',
  107. isEdit:false
  108.           }, {
  109. id:8,
  110. label:'二级 3-2',
  111. isEdit:false,
  112. children: [{
  113. id:11,
  114. label:'三级 3-2-1',
  115. isEdit:false
  116.             }, {
  117. id:12,
  118. label:'三级 3-2-2',
  119. isEdit:false
  120.             }, {
  121. id:13,
  122. label:'三级 3-2-3',
  123. isEdit:false
  124.             }]
  125. }]
  126. }],
  127. defaultProps: {
  128. children:'children',
  129. label:'label'
  130.         }
  131. }
  132. },
  133. methods: {
  134. test () {
  135. axios.get('http://test.cn')
  136. .then(response => {
  137. this.isShow = response.data.operations[0].pubResource.isVisiable
  138.             console.log(response.data.operations[0].pubResource)
  139. this.eleId = response.data.operations[0].pubResource.elementId
  140.           })
  141. },
  142. NodeBlur (Node, data) {
  143. debugger
  144.         console.log(Node, data)
  145. if (data.label.length ===0) {
  146. this.$message.error('菜单名不可为空!')
  147. return false
  148.         }else {
  149. if (data.isEdit) {
  150. this.$set(data,'isEdit',false)
  151. console.log(data.isEdit)
  152. }
  153. this.$nextTick(() => {
  154. this.$refs['slotTreeInput' + data.id].$refs.input.focus()
  155. })
  156. }
  157. },
  158. // 查询
  159.       filterNode (value, data) {
  160. if (!value)return true
  161.         return data.label.indexOf(value) !== -1
  162.       },
  163. handleDragStart (node, ev) {
  164. console.log('drag start', node)
  165. },
  166. handleDragEnter (draggingNode, dropNode, ev) {
  167. console.log('tree drag enter: ', dropNode.label)
  168. },
  169. handleDragLeave (draggingNode, dropNode, ev) {
  170. console.log('tree drag leave: ', dropNode.label)
  171. },
  172. handleDragOver (draggingNode, dropNode, ev) {
  173. console.log('tree drag over: ', dropNode.label)
  174. },
  175. handleDragEnd (draggingNode, dropNode, dropType, ev) {
  176. console.log('tree drag end: ', dropNode && dropNode.label, dropType)
  177. },
  178. handleDrop (draggingNode, dropNode, dropType, ev) {
  179. console.log('tree drop: ', dropNode.label, dropType)
  180. },
  181. allowDrop (draggingNode, dropNode, type) {
  182. if (dropNode.data.label ==='二级 3-1') {
  183. return type !=='inner'
  184.         }else {
  185. return true
  186.         }
  187. },
  188. allowDrag (draggingNode) {
  189. return draggingNode.data.label.indexOf('三级 3-2-2') === -1
  190.       },
  191. // 鼠标右击事件
  192.       rightClick (MouseEvent, object, Node, element) {
  193. debugger
  194.         this.currentData = object
  195. this.currentNode = Node
  196. if (Node.level ===1) {
  197. this.firstLevel =true
  198.         }else {
  199. this.firstLevel =false
  200.         }
  201. this.menuVisible =true
  202.         // let menu = document.querySelector('#card')
  203. // /* 菜单定位基于鼠标点击位置 */
  204. // menu.style.left = event.clientX + 'px'
  205. // menu.style.top = event.clientY + 'px'
  206.         document.addEventListener('click',this.foo)
  207. this.$refs.card.$el.style.left =event.clientX +40 +'px'
  208.         this.$refs.card.$el.style.top =event.clientY +10 +'px'
  209.       },
  210. // 鼠标左击事件
  211.       handleLeftclick (data, node) {
  212. this.foo()
  213. },
  214. //  取消鼠标监听事件 菜单栏
  215.       foo () {
  216. this.menuVisible =false
  217.         //  要及时关掉监听,不关掉的是一个坑,不信你试试,虽然前台显示的时候没有啥毛病,加一个alert你就知道了
  218.         document.removeEventListener('click',this.foo)
  219. },
  220. // 增加同级节点事件
  221.       addSameLevelNode () {
  222. let id =Math.ceil(Math.random() *100)
  223. var data = {id:id,label:'新增节点'}
  224. this.$refs.tree.append(data,this.currentNode.parent)
  225. },
  226. // 增加子级节点事件
  227.       addChildNode () {
  228. console.log(this.currentData)
  229. console.log(this.currentNode)
  230. if (this.currentNode.level >=3) {
  231. this.$message.error('最多只支持三级!')
  232. return false
  233.         }
  234. let id =Math.ceil(Math.random() *100)
  235. var data = {id:id,label:'新增节点'}
  236. this.$refs.tree.append(data,this.currentNode)
  237. },
  238. // 删除节点
  239.       deleteNode () {
  240. this.$refs.tree.remove(this.currentNode)
  241. },
  242. // 编辑节点
  243.       editNode () {
  244. debugger
  245.         if (!this.currentData.isEdit) {
  246. this.$set(this.currentData,'isEdit',true)
  247. }
  248. }
  249. },
  250. watch: {
  251. filterText (val) {
  252. this.$refs.tree.filter(val)
  253. }
  254. },
  255. mounted () {
  256. this.test()
  257. }
  258. }
  259. </script>
  260. <style scoped lang="scss">
  261. /*.lalala {*/
  262. /*position: relative;*/
  263. /*}*/
  264.   .text {
  265. font-size:14px;
  266. }
  267. .el-tree{
  268. width:20%;
  269. margin-top:10px;
  270. }
  271. .search{
  272. width:20%;
  273. }
  274. .item {
  275. padding:18px 0;
  276. }
  277. .add{
  278. cursor:pointer;
  279. margin-top:10px;
  280. }
  281. .delete{
  282. margin:10px 0;
  283. cursor:pointer;
  284. }
  285. .edit{
  286. margin-bottom:10px;
  287. cursor:pointer;
  288. }
  289. .search {
  290. cursor:pointer;
  291. }
  292. .box-card {
  293. width:120px;
  294. position:absolute;
  295. z-index:1000;
  296. }
  297. </style>

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

闽ICP备14008679号