最近写了一个具有增删改查功能的多级树组件,感觉很实用,啦啦啦啦,

废话不多说,看代码:

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="../../static/img/down.png">
<img v-else src="../../static/img/right.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"><img src="../../static/img/add.png"></span>
<span v-show="flag==true" @click="remove(model)"><img src="../../static/img/delete.png"></span>
<span v-show="flag==true" @click="edit"><img src="../../static/img/edit.png"></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>

父组件代码如下:

leftNavigation.vue

 <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>

最新文章

  1. Oracle 数据库中对记录进行分页处理——学习笔记
  2. Java局部变量
  3. 关于tp.5.0角色管理导致的创建角色登陆报错问题解决!
  4. Effective Java 43 Return empty arrays or collections, not nulls
  5. SSH框架总结(框架分析+环境搭建+实例源码下载)
  6. Appium-android环境的快速搭建
  7. URAL1352. Mersenne Primes
  8. PostgreSQL解决&quot;Abc_de_fghijkl_mn&quot; 首字母小写去掉下划线并且下划线后面的第一个字母大写或首字母大写去掉下划线并且下划线后面的首字母大写的js
  9. Jquery效果之固定不动的块
  10. Unity的Profiler性能分析
  11. 世界最大射电望远镜(Arecibo)用于探測地外文明
  12. gcd - b- 201611302317
  13. SGU 310. Hippopotamus( 状压dp )
  14. Android Sqlite数据库执行插入查询更新删除的操作对比
  15. js广告图片轮播
  16. 【转】dmidecode命令详解
  17. angular或者js如何确定选中ul中的哪几个li
  18. http常见状态码(转载)
  19. 4-20模块 序列化模块 hashlib模块
  20. input框的输入限制

热门文章

  1. (1) yum源配置-epel
  2. 九度OJ 1193:矩阵转置 (矩阵计算)
  3. Android5.0以后版本把应用移动到SD或者TF卡的方法
  4. SNMP 监控方式的配置
  5. PAT 组合数的和(15)
  6. 使用微软官方U盘制作软件来安装纯净版windows
  7. Swift编程语言学习6—— 闭包
  8. Nvidia NVENC 硬编码预研总结
  9. QT5的QDesktopSerivices不同
  10. 分析DNS解析时间