基于vue的树形控件,回调组件

free-tree-list.vue

<template>
  <li>
     <span @click="toggle(model.id)">
         <i v-if="isFolder" class="icon" :class="[open ? 'folder-open': 'folder']"></i>
         <i v-if="!isFolder" class="icon file-text"></i>
         {{ model.menuName }}
     </span>
    <ul style="padding-left: 16px" :class="{slideDown:!open,slideUp:open}" v-show="open" v-if="isFolder">
      <free-tree-list @selectCaputure="onselect" v-for="item in model.children" :model="item" :key="item.id"></free-tree-list>
    </ul>
  </li>
</template>
<script>
  export default {
    name: 'free-tree-list',
    props: ['model'],
    data() {
      return {
        open: false
      }
    },
    computed: {
      isFolder: function() {
        return this.model.children && this.model.children.length
      }
    },
    methods: {
      toggle: function(id) {
        this.$emit('selectCaputure',id)
        if (this.isFolder) {
          this.open = !this.open
        }
      },
      onselect(id){
        this.$emit('selectCaputure',id);
      }
    }
  }
</script>
<style>
  ul {
    list-style: none;
  }
  i.icon {
    display: inline-block;
    width: 15px;
    height: 15px;
    background-repeat: no-repeat;
    vertical-align: middle;
    background-size: cover;
  }
  .icon.folder {
    background-image: url(/src/assets/文件夹1.png);
  }
  .icon.folder-open {
    background-image: url(/src/assets/文件夹2.png);
  }
  .icon.file-text {
    background-image: url(/src/assets/文件.png);
  }
  .tree-menu li {
    line-height: 1.5;
  }
  .tree-menu li span{
    cursor: pointer;
    display: inline-block;
    line-height: 30px;
  }
  .slideDown{
    /* overflow-y: hidden;
     max-height: 500px; !* approximate max height *!
     transition-property: all;
     transition-duration: 2s;
     transition-timing-function: cubic-bezier(0, 1, 0.5, 1);*/
    max-height: 0px;
    overflow-y: hidden;
    -webkit-transition: max-height 0.8s ease-in-out;
    -moz-transition: max-height 0.8s ease-in-out;
    -o-transition: max-height 0.8s ease-in-out;
    transition: max-height 0.8s ease-in-out;
  }
  .slideUp{
    max-height: 300px ;
  }
</style>

free-tree.vue

<template>
  <div class="tree-menu">
    <ul v-for="menuItem in allData" :key="menuItem.id">
      <free-tree-list @selectCaputure="onselect" :model="menuItem"></free-tree-list>
    </ul>
  </div>
</template>
<script>
  import freeTreeList from './free-tree-list.vue'
  export default {
    props:{
      allData:{
        type:Array,
        default:[]
      }
    },
    components: {
      freeTreeList
    },
    data() {
      return {

      }
    },
    methods:{
      onselect(id){
        this.$emit('selectCaputure',id);
      }
    }
  }
</script>

App.vue(调用)

<template>
  <div id="app">
    <free-tree @selectCaputure="onselect" :allData="myData" class="trees"></free-tree>
  </div>
</template>

<script>
  var myData = [
    {
      'id': '1',
      'menuName': '一级-01',
      'menuCode': '10',
      'children': [
        {
          'menuName': '二级-01',
          'menuCode': '11',
          'id': '11'
        },
        {
          'menuName': '二级-02',
          'menuCode': '12',
          'id': '12',
          'children': [
            {
              'menuName': '三级-02',
              'menuCode': '121',
              'id': '121'
            },
            {
              'menuName': '三级-03',
              'menuCode': '122',
              'id': '122'
            }
          ]
        },
        {
          'menuName': '二级-03',
          'menuCode': '13',
          'id': '13'
        }
      ]
    },
    {
      'id': '2',
      'menuName': '一级-02',
      'menuCode': ''
    },
    {
      'id': '3',
      'menuName': '一级-03',
      'menuCode': '30',
      'children': [
        {
          'menuName': '二级-01',
          'menuCode': '31',
          'id': '31'
        },
        {
          'menuName': '二级-02',
          'menuCode': '32',
          'id': '32',
          'children': []
        }
      ]
    },
    {
      'id': '4',
      'menuName': '一级-04',
      'menuCode': '',
      'children': []
    }
  ];
  import freeTree from './free-tree'
  export default {
    name: 'app',
    methods:{
      onselect(id){
        console.log(id);
      }
    },
    data () {
      return {
        myData:myData
      }
    },
    components:{
      freeTree
    }
  }
</script>
<style lang="scss">
.trees{
  margin: 20px;
}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三木森森の

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值