最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Vue實現(xiàn)一個樹組件的示例

 更新時間:2020年11月06日 08:47:26   作者:秋荷雨翔  
這篇文章主要介紹了使用Vue實現(xiàn)一個樹組件的示例,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下

HTML代碼:

<!DOCTYPE html>
<html>
<head>
  <title>Vue Demo</title>

  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <style type="text/css">
    /* span樣式 */
    .treeviewitem-span {
      font-size: 14px;
    }

    /* 箭頭樣式 */
    .treeviewitem-arrow-icon {
      margin-top: 3px;
      margin-left: 3px;
      margin-right: 3px;
      float: left;
      width: 0;
      height: 0;
      border-top-width: 6px;
      border-right-width: 0px;
      border-bottom-width: 6px;
      border-left-width: 6px;
      border-style: solid;
      border-color: transparent transparent transparent #666666;
      transform: rotate(0deg);
    }

    /* 90度旋轉(zhuǎn)箭頭樣式 */
    .treeviewitem-arrow-icon90 {
      margin-top: 3px;
      margin-left: 3px;
      margin-right: 3px;
      float: left;
      width: 0;
      height: 0;
      border-top-width: 6px;
      border-right-width: 0px;
      border-bottom-width: 6px;
      border-left-width: 6px;
      border-style: solid;
      border-color: transparent transparent transparent #666666;
      transform: rotate(90deg);
    }

    /* 模板隱藏 */
    template {
      display: none;
    }
  </style>

  <script type="text/javascript" src="vue.js"></script>
  <script type="text/javascript">

  </script>
</head>
<body>
  <!-- treeviewitem模板 -->
  <template id="treeviewitem">
    <div>
      <div style="background-color:transparent; cursor:default; height:25px;"><div v-on:click="expand" style="width:16px; height:16px; float:left; background-color:transparent;"><span v-bind:class="[self.expand ? 'treeviewitem-arrow-icon90':'treeviewitem-arrow-icon']" v-if="isLeaf"></span></div><input type="checkbox" v-on:click="checkboxClick" v-model="self.selected" /><span v-on:click="itemClick" class="treeviewitem-span">{{ self.name }}</span></div>
      <div v-if="self.expand" v-for="item in items" style="margin-left:20px;">
        <treeviewitem v-bind:items="item.items" v-bind:self="item"></treeviewitem>
      </div>
    </div>
  </template>

  <!-- treeview模板 -->
  <template id="treeview">
    <div>
      <div v-for="item in items">
        <treeviewitem v-bind:items="item.items" v-bind:self="item"></treeviewitem>
      </div>
    </div>
  </template>

  <div id="app">
    <!-- 使用treeview組件 -->
    <treeview v-bind:items="items"></treeview>
    <br />
    <button onclick="showSelectedResult()">勾選結果</button>
  </div>

  <script type="text/javascript">
    //定義treeviewitem組件
    Vue.component('treeviewitem', {
      props: ['items', 'self'],
      template: '#treeviewitem',
      methods: {
        itemClick: function (d) {
          alert("您單擊了節(jié)點:" + this.self.name);
        },
        checkboxClick: function (e) {
          var checkChild;
          checkChild = function (items, checked) {
            for (var i = 0; i < items.length; i++) {
              var item = items[i];
              item.selected = checked;
              if (item.items) {
                checkChild(item.items, checked)
              }
            }
          };

          if (e.target.checked) {
            checkChild(this.items, true);
          } else {
            checkChild(this.items, false);
          }
        },
        expand: function (e) {
          if (this.self.expand) {
            this.self.expand = false;
          } else {
            this.self.expand = true;
          }
        }
      },
      computed: {
        isLeaf: function () {
          if (this.items && this.items.length > 0) {
            return true;
          }
          return false;
        }
      }
    });

    //定義treeview組件
    Vue.component('treeview', {
      props: ['items'],
      template: '#treeview'
    });

    //定義vm
    var vm = new Vue({
      el: '#app',
      methods: {
        /**
         * @description 獲取勾選結果
         */
        getSelected: function (items) {
          if (!items) items = this.items;

          var result = [];
          for (var i = 0; i < items.length; i++) {
            var item = items[i];
            if (item.selected) {
              result.push(item.name);
            }
            if (item.items) {
              var childSelected = this.getSelected(item.items);
              for (var k = 0; k < childSelected.length; k++) {
                result.push(childSelected[k]);
              }
            }
          }
          return result;
        }
      },
      data: {
        items: [
          {
            name: '條目1',
            expand: true,
            selected: false,
            items: [
              {
                name: '條目11',
                expand: true,
                selected: false,
                items: [
                  {
                    name: '條目111',
                    expand: true,
                    selected: true,
                  }
                ]
              },
              {
                name: '條目12',
                expand: false,
                selected: false,
                items: [
                  {
                    name: '條目121',
                    expand: true,
                    selected: false,
                  },
                  {
                    name: '條目122',
                    expand: true,
                    selected: false,
                    items: [
                      {
                        name: '條目1221',
                        expand: true,
                        selected: false
                      },
                      {
                        name: '條目1222',
                        expand: true,
                        selected: false
                      }
                    ]
                  }
                ]
              },
              {
                name: '條目13',
                expand: true,
                selected: false
              }
            ]
          },
          {
            name: '條目2',
            expand: true,
            selected: false
          },
          {
            name: '條目3',
            expand: true,
            selected: false,
            items: [
              {
                name: "條目31",
                expand: true,
                selected: false
              }
            ]
          }
        ]
      }
    })

    //顯示勾選結果
    function showSelectedResult() {
      var selected = vm.getSelected();
      alert("您勾選了:" + selected.join(', '));
    }
  </script>
</body>
</html>

效果圖:

以上就是使用Vue實現(xiàn)一個樹組件的示例的詳細內(nèi)容,更多關于vue 實現(xiàn)樹組件的資料請關注腳本之家其它相關文章!

相關文章

  • vue如何把字符串中的所有@內(nèi)容,替換成帶標簽的

    vue如何把字符串中的所有@內(nèi)容,替換成帶標簽的

    這篇文章主要介紹了vue如何把字符串中的所有@內(nèi)容,替換成帶標簽的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • Vue組件之單向數(shù)據(jù)流的解決方法

    Vue組件之單向數(shù)據(jù)流的解決方法

    這篇文章主要介紹了Vue組件之單向數(shù)據(jù)流的解決方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • webpack如何打包一個按需引入的vue組件庫

    webpack如何打包一個按需引入的vue組件庫

    在vue項目開發(fā)中,我們會將經(jīng)常用到的邏輯或模塊抽象成組件,對于那些多個項目都有用到的組件,可以考慮封裝成組件庫,這篇文章主要給大家介紹了關于webpack如何打包一個按需引入的vue組件庫的相關資料,需要的朋友可以參考下
    2022-02-02
  • vue element 關閉當前tab 跳轉(zhuǎn)到上一路由操作

    vue element 關閉當前tab 跳轉(zhuǎn)到上一路由操作

    這篇文章主要介紹了vue element 關閉當前tab 跳轉(zhuǎn)到上一路由操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • vue.js開發(fā)環(huán)境安裝教程

    vue.js開發(fā)環(huán)境安裝教程

    這篇文章主要為大家詳細介紹了vue.js開發(fā)環(huán)境的安裝教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • vue腳手架vue-cli的學習使用教程

    vue腳手架vue-cli的學習使用教程

    本篇文章主要介紹了vue腳手架vue-cli的學習使用教程,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • Vue組件間傳遞數(shù)據(jù)的多種方法

    Vue組件間傳遞數(shù)據(jù)的多種方法

    在實際開發(fā)中,Vue組件之間的數(shù)據(jù)傳遞是最常見的需求,由于組件的作用域相互獨立,如何在父子、兄弟和跨級組件間傳遞數(shù)據(jù)就顯得尤為重要,本文將詳細介紹多種Vue組件間傳遞數(shù)據(jù)的方,需要的朋友可以參考下
    2025-03-03
  • 基于Vue-Cli 打包自動生成/抽離相關配置文件的實現(xiàn)方法

    基于Vue-Cli 打包自動生成/抽離相關配置文件的實現(xiàn)方法

    基于Vue-cli 項目產(chǎn)品部署,涉及到的交互的地址等配置信息,每次都要重新打包才能生效,極大的降低了效率。這篇文章主要介紹了基于Vue-Cli 打包自動生成/抽離相關配置文件 ,需要的朋友可以參考下
    2018-12-12
  • Vue聲明式導航與編程式導航及導航守衛(wèi)和axios攔截器全面詳細講解

    Vue聲明式導航與編程式導航及導航守衛(wèi)和axios攔截器全面詳細講解

    這篇文章主要介紹了Vue聲明式導航與編程式導航及導航守衛(wèi)和axios攔截器,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2023-01-01
  • vue-cli擴展多模塊打包的示例代碼

    vue-cli擴展多模塊打包的示例代碼

    本篇文章主要介紹了vue-cli擴展多模塊打包的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04

最新評論

博湖县| 康马县| 噶尔县| 深圳市| 米易县| 赤壁市| 惠来县| 南江县| 枣阳市| 蚌埠市| 宝应县| 舟山市| 洪泽县| 贵南县| 陇南市| 云龙县| 孝感市| 承德县| 镶黄旗| 宜君县| 南通市| 新平| 凤庆县| 连云港市| 呼玛县| 黎平县| 星子县| 任丘市| 兴国县| 云南省| 建宁县| 甘孜县| 承德市| 余干县| 梧州市| 奎屯市| 即墨市| 广西| 萝北县| 潮安县| 墨竹工卡县|