VUE树形图(递归实现)

1.效果

 也可戳链接体验...


瞎封装组件系列:

VUE简单提示框

VUE选择商品多规格(库存判断)

VUE多店铺购物车

2.代码实现

<template>
  <div class="tree_cont">
    <div>
      {
   
   {treeData.tit}}
      <span v-if="isOpen" @click="toggle">[{
   
   {open? '-':'+'}}]</span>
    </div>
    <ul v-if="isOpen" v-show="open">
      <item v-for='(data,index) in treeData.list' :treeData='data' :key='index'></item>
    </ul>
  </div>
</template>

<script>
  export default{
    name:'item',
    props:{
      treeData:{
        type:Object,
        required:true
      }
    },
    data(){
      return{
        open:false,
      }
    },
    methods:{
      toggle(){
        this.open = !this.open
      }
    },
    computed:{
      isOpen(){
        return this.treeData.list && this.treeData.list.length;
      }
    }
  }
</script>

<style scoped>
  ul{
    padding-left: .5rem;
  }
	
	.tree_cont{
		margin: .15rem .25rem;
		background: white;
		padding: .1rem .25rem;
	}
</style>

3.使用

需要传入一个规定json。

<template>
	<div class="ui-pane">
		<ui-header headertit="树形图"></ui-header>
		<div class="ui-content">
			<arealine linetit="树形图组件"></arealine>
			<tree :treeData="treeData"></tree>
		</div>
	</div>
</template>

<script>
	import tree from '../../../../components/ghTree/YGHH-Tree.vue'
	export default {
		name: 'home',
		data() {
			return {
				treeData: {
					tit: '标题',
					list: [{
						tit: 'AAA',
						list: [{
							tit: 'AAA-1'
						}, {
							tit: 'AAA-2',
							list: [{
								tit: 'AAA-2-1'
							}]
						}, {
							tit: 'AAA-3'
						}, {
							tit: 'AAA-4',
							list: [{
								tit: 'AAA-4-1',
							}, {
								tit: 'AAA-4-2',
							}, {
								tit: 'AAA-4-3',
							}]
						}]
					}, {
						tit: 'BBB',
						list: [{
							tit: 'BBB-1'
						}]
					}, {
						tit: 'CCC',
						list: [{
							tit: 'CCC-1'
						}, {
							tit: 'CCC-2'
						}]
					}]
				}
			}
		},
		methods: {},
		components: {
			tree
		}
	}
</script>

<style scoped="scoped">
	.area-line {
		background: #ccc;
		margin: 0 .25rem;
		color: #666;
		padding: .2rem .25rem;
	}

	.line_tit {
		color: #333;
		font-size: .3rem;
		font-weight: bold;
		text-overflow: ellipsis;
		overflow: hidden;
	}

	.area-line .line_rihgt {
		height: 2rem;
		width: 2rem;
	}

	.area_left {
		height: 2rem;
		padding: .1rem 0;
		width: calc(100% - 2.4rem);
	}

	.line_cont {
		color: #666;
		font-size: .24rem;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		margin: .1rem 0 .2rem 0;
		-webkit-box-orient: vertical;
	}

	.line_num .flex-star {
		margin-right: .15rem;
		font-size: .24rem;
	}

	.line_num .flex-star svg {
		width: .32rem;
		height: .32rem;
		margin-right: .05rem;
	}
</style>

写的不好,多多指教。

猜你喜欢

转载自blog.csdn.net/m0_43599959/article/details/113860884