溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何在vue中利用遞歸組件實(shí)現(xiàn)一個(gè)樹形控件

發(fā)布時(shí)間:2022-05-06 13:40:13 來(lái)源:億速云 閱讀:528 作者:iii 欄目:大數(shù)據(jù)

這篇文章主要講解了“如何在vue中利用遞歸組件實(shí)現(xiàn)一個(gè)樹形控件”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“如何在vue中利用遞歸組件實(shí)現(xiàn)一個(gè)樹形控件”吧!

vue版的樹形控件

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>樹形結(jié)構(gòu)2</title>
</head>
<body>
  <div id = "app">
    <tree :folder = "trees" :select = "select"></tree>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <script>
    Vue.component('tree', {
      name:"tree",
      template:`<ul>
            <li v-for = "item in folder">
              <span @click = "select(item)">{{ item.label }}</span>
              <tree v-if = "item.children" :folder = "item.children" :select = "select"></tree>
            </li>
          </ul>`,
      props:["folder","select"],
    })
    // <tree v-if = "item.children" :folder = "item.children"></tree>
    var app = new Vue({
      el:"#app",
      data:{
        msg:"hello world",
        trees: [
          {
            id:1,
            label:"1級(jí)目錄1",
            show:false,
            children:[
              {
                id:"1-1",
                label:"1.1目錄"
              },
              {
                id:"1-2",
                label:"1.2目錄"
              },
              {
                id:"1-3",
                label:"1.3目錄"
              },
            ]
          },
          {
            id:2,
            label:"1級(jí)目錄2",
            show:false
          },
          {
            id:3,
            label:"1級(jí)目錄3",
            show:false,
            children:[
              {
                id:"3-1",
                label:"3.1目錄"
              },
              {
                id:"3-2",
                label:"3.2目錄",
                show:false,
                children:[
                  {
                    id:"3-2-1",
                    label:"3.2.1目錄"
                  },
                  {
                    id:"3-2-2",
                    label:"3.2.2目錄"
                  },
                  {
                    id:"3-2-3",
                    label:"3.2.3目錄"
                  }
                ]
              }
            ]
          },
          {
            id:4,
            label:"1級(jí)目錄4",
            show:false,
            children:[
              {
                id:"4-1",
                label:"4.1目錄"
              },
              {
                id:"4-2",
                label:"4.2目錄",
                show:false,
                children:[
                  {
                    id:"4-2-1",
                    label:"4.2.1目錄"
                  }
                ]
              }
            ]
          },
          {
            id:5,
            label:"1級(jí)目錄5",
            show:false,
            children:[
              {
                id:"5-1",
                label:"5.1目錄",
                show:false,
                children:[
                  {
                    id:"5-1-1",
                    label:"5.1.1目錄"
                  },
                  {
                    id:"5-1-2",
                    label:"5.1.2目錄",
                    show:false,
                    children:[
                      {
                        id:"5-1-2-1",
                        label:"5.1.2.1目錄"
                      },
                    ]
                  }
                ]
              },
              {
                id:"5-2",
                label:"5.2目錄",
                show:false
              }
            ]
          },
        ]
      },
      methods:{
        clickHandler(){
          console.log(23333);
        },
        select(data){
          console.log(data);
        }
      },
      mounted(){
        console.log(this.trees);
      }
    })
  </script>
</body>
</html>

看下結(jié)果

如何在vue中利用遞歸組件實(shí)現(xiàn)一個(gè)樹形控件

當(dāng)然我用的全局組件,如果用vue-cli搭建的環(huán)境是一樣的,引入組件就可以了,但是一定要注意,組件內(nèi)必須要用name屬性,而且name的名稱要和組件名稱(組件標(biāo)簽名稱)一致才可以

貼一個(gè)項(xiàng)目中用的模板吧,相當(dāng)于做筆記了

<template>
  <ul class = "dataBaseTree">
    <li v-for = "(item,index) in folder" :key = "index">
      <span @click = "select(item)" :class = "{'active':currentId == item.id}">
        <i class = "folderIcon" v-if = "item.children">
          <icon :icon = "'xiala'" v-if = "item.show"></icon>
          <icon :icon = "'xiala2'" v-else></icon>
        </i>
        {{ item.label }}
      </span>
      <el-collapse-transition>
        <DatabaseTree v-if = "item.children && item.show" :folder = "item.children" :select = "select" :currentId = "currentId"></DatabaseTree>
      </el-collapse-transition>
    </li>
  </ul>
</template>

<script>
  import { mapGetters , mapActions} from 'vuex';
  export default{
    name:"DatabaseTree",
    props:["folder","select","currentId"],
    data(){
      return{
        addParams:{
          label:"",
          children:[]
        },
        noteData:{
          children:[]
        }
      }
    },
     computed:{
      ...mapGetters(["catalog"])
    },
    methods:{}
  }
</script>

<style lang="scss" scoped>
  .dataBaseTree{
    padding-left:12%;
    line-height:40px;
    ul{
      padding-left:12%;
      line-height:40px;
      li{
        span{
          display:inline-block;
          padding-left:23%;
          height:100%;
          width:120%;
          color:#ababab;
          font-size:14px;
          position: relative;
          cursor: pointer;
          &:hover{
            background: #EDF0F5;
          }
          .folderIcon{
            color:#BCBCBC;
            position: absolute;
            top:-1px;
            left:22px;
          }
        }
      }
    }
    li{
      position: relative;
      span{
        display:inline-block;
        padding-left:40px;
        font-size:14px;
        height:100%;
        width:120%;
        cursor: pointer;
        position: relative;
        right:25px;
        top:-2px;
        color:#ababab;
        &:hover{
          background: #EDF0F5;
        }
        .titleIcon{
          color:#C3C3C3;
          font-size:16px;
          position: absolute;
          top:12px;
          left:16px;
        }
        .folderIcon{
          color:#BCBCBC;
          position: absolute;
          top:-1px;
          left:22px;
        }
      }
      .active{
        background: #EDF0F5;
      }
    }
  }
</style>

vue版的就到這里了

 下面貼一個(gè)原生js版的,感興趣的小伙伴可以繼續(xù)往下看

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <script>
    var tree=[
        {
          id:1,
          label:"1級(jí)目錄1",
          children:[
            {
              id:"1-1",
              label:"1.1目錄"
            },
            {
              id:"1-2",
              label:"1.2目錄"
            },
            {
              id:"1-3",
              label:"1.3目錄"
            },
          ]
        },
        {
          id:2,
          label:"1級(jí)目錄2",
        },
        {
          id:3,
          label:"1級(jí)目錄3",
          children:[
            {
              id:"3-1",
              label:"3.1目錄"
            },
            {
              id:"3-2",
              label:"3.2目錄",
              children:[
                {
                  id:"3-2-1",
                  label:"3.2.1目錄"
                },
                {
                  id:"3-2-2",
                  label:"3.2.2目錄"
                },
                {
                  id:"3-2-3",
                  label:"3.2.3目錄"
                }
              ]
            }
          ]
        },
        {
          id:4,
          label:"1級(jí)目錄4",
          children:[
            {
              id:"4-1",
              label:"4.1目錄"
            },
            {
              id:"4-2",
              label:"4.2目錄",
              children:[
                {
                  id:"4-2-1",
                  label:"4.2.1目錄"
                }
              ]
            }
          ]
        },
        {
          id:5,
          label:"1級(jí)目錄5",
          children:[
            {
              id:"5-1",
              label:"5.1目錄",
              children:[
                {
                  id:"5-1-1",
                  label:"5.1.1目錄"
                },
                {
                  id:"5-1-2",
                  label:"5.1.2目錄",

                  children:[
                    {
                      id:"5-1-2-1",
                      label:"5.1.2.1目錄"
                    },
                  ]
                }
              ]
            },
            {
              id:"5-2",
              label:"5.2目錄"
            }
          ]
        },
      ];
    var render = function(tree) {
      if (!tree) return null
      var ul = document.createElement('ul');
      for(var i = 0; i < tree.length;i++){
        var li = document.createElement('li')
        // 創(chuàng)建span標(biāo)簽
        var span = document.createElement('span'); span.innerText = tree[i].label;
        li.appendChild(span);
        if(tree[i].children){
          var sub = render(tree[i].children);
          li.appendChild(sub);
        }
        ul.appendChild(li);
      }
      return ul
    };
    document.body.innerHTML = '';
    document.body.appendChild(render(tree)); 
  </script>
</body>
</html>

感謝各位的閱讀,以上就是“如何在vue中利用遞歸組件實(shí)現(xiàn)一個(gè)樹形控件”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)如何在vue中利用遞歸組件實(shí)現(xiàn)一個(gè)樹形控件這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

vue
AI