vue实现节点增删改功能
更新时间:2019年09月26日 15:47:52 作者:小羽向前跑
这篇文章主要为大家详细介绍了vue实现节点增删改功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了vue实现节点增删改功能的具体代码,供大家参考,具体内容如下
效果:

增删改功能 tree.vue组件代码:
<template>
<div>
<div class="all-div" v-if="model.name">
<div class="itemRow" :style="{ marginLeft:model.level*20+'px' }">
<span v-show="model.children.length" @click="expandOrCollapse">
<img v-if="model.isOpen" src="../../assets/img/login_logo.png">
<img v-else src="../../assets/img/login_logo2.png">
</span>
<div class="hover-div" @mouseover="flag=true" @mouseout="flag=false">
<span @click="jump(model.url)">{{model.name}}</span>
<span v-show="flag==true" @click="add" style="fontsize:40px;color:red;">+</span>
<span v-show="flag==true" @click="remove(model)"><img src="../../assets/img/del.png"></span>
<span v-show="flag==true" @click="edit" style="color:green;">修改</span>
<!--<span class="asce" v-show="model.children.length" @click="orderAsce">↑</span>
<span class="desc" v-show="model.children.length" @click="orderDesc">↓</span>-->
</div>
</div>
</div>
<navigation v-if="model.isOpen" v-for="row in model.children" :key="row.name" :model="row" :length="model.children.length"></navigation>
</div>
</template>
<script>
export default {
name: 'navigation',
// 使用`编辑树`组件需要传递的数据
props: {
// 编辑树对象
model: {
type: Object
},
length: {
type: Number
}
},
data () {
return {
flag:false
}
},
methods: {
// 添加节点
add(){
let val = prompt("请输入要添加的节点的名称:");
if (val) {
this.model.children.push({
name: val,
level: this.model.level + 1,
isOpen: true,
children: []
});
}
},
// 移除节点
remove(model){
var self = this;
alert('确认删除吗?');
if (self.$parent.model) {
self.$parent.model.children.forEach((item, index) => {
if (item.name == model.name) {
self.$parent.model.children.splice(index, 1);
}
})
}
},
// 编辑节点名称
edit(){
var self = this;
let rename = prompt('请输入修改后的节点名称:');
// 使用正则进行重命名的差错校验
if (!rename.length) {
alert('请输入正确的节点名称');
return;
}
self.model.name = rename;
},
/**
* 展开/收起功能
*/
expandOrCollapse(){
this.model.isOpen = !this.model.isOpen;
},
jump(url){
var self = this;
self.$router.push({path:url})
}
/*// 升序排列
orderAsce(){
function compare(property) {
return function (a, b) {
var value1 = a[property];
var value2 = b[property];
return value1 - value2;
}
}
this.model.children.sort(compare('name'));
},
// 降序排列
orderDesc(){
this.orderAsce();
this.model.children.reverse();
}*/
},
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.all-div{
margin-left: 6%;
}
.itemRow {
text-align: left;
padding-top: 2%;
padding-bottom: 2%;
}
.itemRow span,.itemRow img{
cursor: pointer;
}
.itemRow span{
font-size: 1.1vw;
}
.hover-div{
display:inline-block;
}
</style>
父组件中引用代码:
<template>
<div id="all">
<tree :model="root" :length="length"></tree>
</div>
</template>
<style scoped>
#all{
width:100%;
height: 100%;
}
</style>
<script>
import tree from './tree.vue'
export default{
data(){
return{
root:{
name:"根节点",
level:0,
isOpen:true,
children:[
{
name:"节点1",
level:1,
url:"/homePage/middle/navLeftFirst",
isOpen:false,
children:[
{
name:"节点1-1",
level:2,
isOpen:true,
children:[]
},
{
name:"节点1-2",
level:2,
isOpen:true,
children:[]
}
]
},
{
name:"节点2",
level:1,
url:"/homePage/middle/navLeftSecond",
isOpen:false,
children:[
{
name:"节点2-1",
level:2,
isOpen:true,
children:[]
},
{
name:"节点2-2",
level:2,
isOpen:true,
children:[]
}
]
}
]
},
length:2
}
},
components:{
tree
}
}
</script>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
Vue+Element-ui表单resetFields无法重置问题
本文主要介绍了Vue+Element-ui表单resetFields无法重置问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2022-04-04
vue+element-ui中form输入框无法输入问题的解决方法
很多初次接触element-ui的同学,在用到element form组件时可能会遇到input框无法输入文字的问题,下面这篇文章主要给大家介绍了关于vue+element-ui中form输入框无法输入问题的解决方法,需要的朋友可以参考下2023-04-04
vue3+vue-router+vite实现动态路由的全过程
动态路由是根据不同情况实时变化的路由,在权限管理系统中,动态路由常用于根据用户角色分配不同的菜单和功能,这篇文章主要介绍了vue3+vue-router+vite实现动态路由的相关资料,需要的朋友可以参考下2024-10-10
Vue使用Print.js打印div方式(选中区域的html)
这篇文章主要介绍了Vue使用Print.js打印div方式(选中区域的html),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2024-03-03


最新评论