亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

基于 Vue 的樹形選擇組件的示例代碼

 更新時間:2017年08月18日 09:47:40   作者:ZhuFaner  
本篇文章主要介紹了基于 Vue 的樹形選擇組件的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

本文介紹了基于 Vue 的樹形選擇組件。分享給大家,具體如下:

系統(tǒng)要求:Vue 2

基本特性

  •  完美的多級聯(lián)動效果
  •  支持無限多的分級
  •  有 全選、半選、不選 三種狀態(tài)

 截圖展示

代碼如下:

 <!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <link rel="icon"  rel="external nofollow" type="image/x-icon">
 <title>Vue Tree Select Example</title>
 <script src="https://cdn.bootcss.com/vue/2.4.2/vue.js"></script>
</head>
<body>

 <!-- 遞歸引用的模板 -->
 <template id="one-select" style="display: none;">
  <ul>
   <li v-for="(node, key, index) in tree">
    <div v-if="key != 'selected'">
     <div v-on:click="nodeClick(node, index)" v-bind:class="[node.selected == null ? 'tree-select-null' : (node.selected == 'half' ? 'tree-select-half' : 'tree-select-full'), 'tree-select', 'inline-block']"></div>
     
     <div class="inline-block">{{ key }}</div>
     <div v-if="key != ''">
      <one-select v-bind:tree="node" v-bind:isroot="false"></one-select>
     </div>
    </div>
   </li>
  </ul>
 </template>

 <!-- 整體樹容器 -->
 <div id="tree">
  <one-select v-bind:isroot="true" v-bind:tree="tree"></one-select>
 </div>

<textarea id="treeDataJSON" style="display: none;">
{
 "客戶管理": {
  "我的客戶": {
   "新分配": {},
   "跟進中": {},
   "簽單客戶": {},
   "長期客戶": {}
  },
  "長期客戶權限": {
   "設為長期客戶": {},
   "還原長期客戶": {}
  }
 },
 "采購列表": {
  "添加異常客情": {},
  "添加采購單": {},
  "采購退貨單列表": {},
  "供應商管理": {},
  "供應商聯(lián)系人": {},
  "品牌列表": {
   "寶潔": {},
   "樂視": {
    "樂視網(wǎng)": {},
    "樂視手機": {
     "樂視手機 1": {},
     "樂視手機 2": {},
     "樂視手機 3": {},
     "樂視手機 4": {},
     "樂視手機 5": {
      "體驗超深層級": {
       "繼續(xù)體驗超深層級": {
        "依然體驗超深層級": {},
        "依然體驗超深層級 2": {}
       }
      }
     }
    },
    "樂視電視": {}
   },
   "可口可樂": {},
   "圣象": {}
  }
 }
}
</textarea>

<script>
// 初始數(shù)據(jù)
var treeDataJSON = document.getElementById("treeDataJSON").value;
var treeData = JSON.parse(treeDataJSON);
Vue.component('one-select', {
 name: 'one-select',
 template: '#one-select',
 props: ['tree', 'isroot'],
 created: function() {
  var realTree = Object.assign({}, this.tree);
  delete realTree.selected;
  if (Object.keys(realTree).length === 0) { // 判斷最低級,再刷新父級
   this.refreshAllParentNodes(this.$parent);
  }
 },
 methods: {
  nodeClick: function(node, index) {
   if (node.selected === 'full' || node.selected === 'half') {
    Vue.set(node, 'selected', null);
   } else {
    Vue.set(node, 'selected', 'full');
   }
   this.refreshAllParentNodes(self.$parent);
   this.refreshAllParentNodes(this);
   this.refreshAllSonNodes(this.$children[index], node.selected);
  },
  refreshAllSonNodes: function(node, status) {
   if (node instanceof Vue && node.$children.length) {
    for (index in node.$children) {
     Vue.set(node.$children[index].tree, 'selected', status);
     // 遞歸計算子級
     this.refreshAllSonNodes(node.$children[index], status);
    }
   }
  },
  refreshAllParentNodes: function(node) {
   if (node instanceof Vue) {
    var status = null;
    var nullCount = 0;
    var halfCount = 0;
    var fullCount = 0;
    for (index in node.$children) {
     if (typeof node.$children[index].tree.selected === 'undefined') {
      nullCount++;
     } else if (node.$children[index].tree.selected === null) {
      nullCount++;
     } else if (node.$children[index].tree.selected === 'half') {
      halfCount++;
     } else if (node.$children[index].tree.selected === 'full') {
      fullCount++;
     }
    }
    if (fullCount === node.$children.length) {
     status = 'full';
    } else if (nullCount === node.$children.length) {
     status = null;
    } else {
     status = 'half';
    }
    Vue.set(node.tree, 'selected', status);

    // 遞歸計算父級
    this.refreshAllParentNodes(node.$parent);
   }
  },
  log: function(o) {
   console.log(o);
  }
 }
});
vm = new Vue({
 el: '#tree',
 data: {
  tree: treeData
 },
 methods: {
  // 返回最終數(shù)據(jù)
  getResult: function() {
   return Object.assign({}, this.tree);
  }
 }
});
</script>

<style>
#tree {
 width: 500px;
 margin: 0 auto;
 margin-top: 50px;
}
li {
 list-style: none;
 line-height: 25px;
}
.inline-block {
 display: inline-block;
}
.tree-select {
 width: 13px;
 height: 13px;
 line-height: 16px;
 margin: 3px;
 display: inline-block;
 vertical-align: middle;
 border: 0 none;
 cursor: pointer;
 outline: none;
 background-color: transparent;
 background-repeat: no-repeat;
 background-attachment: scroll;
 background-image: url('selects.png');
}
.tree-select-null {
 background-position: 0 0;
}
.tree-select-full {
 background-position: -14px 0;
}
.tree-select-half {
 background-position: -14px -28px;
}
</style>

</body>
</html>

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • vue實現(xiàn)兩個組件之間數(shù)據(jù)共享和修改操作

    vue實現(xiàn)兩個組件之間數(shù)據(jù)共享和修改操作

    這篇文章主要介紹了vue實現(xiàn)兩個組件之間數(shù)據(jù)共享和修改操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • 解決Vue編譯時寫在style中的路徑問題

    解決Vue編譯時寫在style中的路徑問題

    下面小編就為大家?guī)硪黄鉀QVue編譯時寫在style中的路徑問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • element input輸入框自動獲取焦點的實現(xiàn)

    element input輸入框自動獲取焦點的實現(xiàn)

    本文主要介紹了element input輸入框自動獲取焦點的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • Vue函數(shù)式組件-你值得擁有

    Vue函數(shù)式組件-你值得擁有

    這篇文章主要介紹了Vue函數(shù)式組件及vue函數(shù)式組件的優(yōu)缺點,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-05-05
  • Vue.js鼠標懸浮更換圖片功能

    Vue.js鼠標懸浮更換圖片功能

    這篇文章主要為大家詳細介紹了Vue.js實現(xiàn)鼠標懸浮更換圖片功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Vue使用pdf-lib實現(xiàn)為文件流添加水印并預覽

    Vue使用pdf-lib實現(xiàn)為文件流添加水印并預覽

    這篇文章主要為大家詳細介紹了Vue如何使用pdf-lib實現(xiàn)為文件流添加水印并預覽的功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起了解一下
    2023-03-03
  • 詳解van-cell如何使用插槽

    詳解van-cell如何使用插槽

    這篇文章主要為大家介紹了van-cell如何使用插槽詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-12-12
  • vue.js實現(xiàn)表格合并示例代碼

    vue.js實現(xiàn)表格合并示例代碼

    最近工作中遇到一個需求,是要做一個頁面放張大表格用來顯示數(shù)據(jù)項,純粹為了view層操作方便,就用了vue做渲染。然而又被提出了一個需求,需要相鄰的相同值的行數(shù)據(jù)項進行單元格合并,這就醉了。沒辦法,只能想辦法解決,下面通過這篇文章來一起看看吧。
    2016-11-11
  • 深入理解Vue-cli4路由配置

    深入理解Vue-cli4路由配置

    Vue-router是Vue官方的路由插件,本文將結合實例代碼,介紹Vue-cli4路由配置,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • VUE實現(xiàn)表單元素雙向綁定(總結)

    VUE實現(xiàn)表單元素雙向綁定(總結)

    本篇文章主要介紹了VUE實現(xiàn)表單元素雙向綁定(總結) ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08

最新評論