91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何在vue中利用遞歸組件實現一個樹形控件

發布時間:2022-05-06 13:40:13 來源:億速云 閱讀:535 作者:iii 欄目:大數據

這篇文章主要講解了“如何在vue中利用遞歸組件實現一個樹形控件”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“如何在vue中利用遞歸組件實現一個樹形控件”吧!

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>樹形結構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級目錄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級目錄2",
            show:false
          },
          {
            id:3,
            label:"1級目錄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級目錄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級目錄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>

看下結果

如何在vue中利用遞歸組件實現一個樹形控件

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

貼一個項目中用的模板吧,相當于做筆記了

<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版的就到這里了

 下面貼一個原生js版的,感興趣的小伙伴可以繼續往下看

<!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級目錄1",
          children:[
            {
              id:"1-1",
              label:"1.1目錄"
            },
            {
              id:"1-2",
              label:"1.2目錄"
            },
            {
              id:"1-3",
              label:"1.3目錄"
            },
          ]
        },
        {
          id:2,
          label:"1級目錄2",
        },
        {
          id:3,
          label:"1級目錄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級目錄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級目錄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')
        // 創建span標簽
        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中利用遞歸組件實現一個樹形控件”的內容了,經過本文的學習后,相信大家對如何在vue中利用遞歸組件實現一個樹形控件這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

vue
AI

临安市| 高清| 商丘市| 抚宁县| 乐亭县| 府谷县| 莱芜市| 荆州市| 阜城县| 会理县| 新乐市| 天柱县| 江都市| 龙里县| 广灵县| 福清市| 资阳市| 马边| 颍上县| 灌阳县| 报价| 舒城县| 金湖县| 黎川县| 宁国市| 翁牛特旗| 个旧市| 甘谷县| 西和县| 盐源县| 革吉县| 嘉黎县| 永丰县| 哈巴河县| 响水县| 阜新市| 青海省| 建始县| 南投县| 广水市| 新安县|