小編給大家分享一下vue如何實(shí)現(xiàn)的樹形菜,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
專注于為中小企業(yè)提供做網(wǎng)站、成都網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)鳳縣免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了成百上千企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
具體代碼如下所示:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>vue</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="animate.css" rel="external nofollow" > <script src="vue.js"></script> <style> *{ color:#585858; } #app{ min-height: 650px; } #app li{ list-style-type:none; } #app a{ text-decoration:none; } #app button{ width:100%; } #app ul{ padding:10px; } #app span{ cursor:pointer; } #tree{ border: 1px solid #ccc; min-height: 650px; width: 50%; margin:0; padding-top: 10px; background-color:#f2f2f2; position: absolute; top:0; left:0; } #tree li { display: block; padding: 0; margin: 0; border: 0; border-bottom: 1px solid #e5e5e5; min-height: 32px; line-height:32px; } </style> </head> <body> <div id='app' @click='hideTree($event)'> <button @click.stop="show = !show">點(diǎn)我</button> <transition enter-active-class="animated fadeInLeft" leave-active-class="animated fadeOutLeft"> <item v-bind:tree='treeData' id='tree' v-if="show"></item> </transition> </div> <template id='tree-template'> <ul> <li v-for='(v,i) in tree'> <span v-if="isFolder(v)" @click="toggle(i)">{{ tree[i].open ? '-' : '+' }}</span> <a data-id="v.id">{{v.city}}</a> <item v-bind:tree='v.child' v-show="tree[i].open"></item> </li> </ul> </template> <script> var data = [{"id":26,"pid":1,"city":"四川省"},{"id":30,"pid":1,"city":"云南省"},{"id":322,"pid":26,"city":"成都"},{"id":323,"pid":26,"city":"綿陽"},{"id":324,"pid":26,"city":"阿壩"},{"id":325,"pid":26,"city":"巴中"},{"id":326,"pid":26,"city":"達(dá)州"},{"id":327,"pid":26,"city":"德陽"},{"id":328,"pid":26,"city":"甘孜"},{"id":329,"pid":26,"city":"廣安"},{"id":330,"pid":26,"city":"廣元"},{"id":331,"pid":26,"city":"樂山"},{"id":332,"pid":26,"city":"涼山"},{"id":333,"pid":26,"city":"眉山"},{"id":334,"pid":26,"city":"南充"},{"id":335,"pid":26,"city":"內(nèi)江"},{"id":336,"pid":26,"city":"攀枝花"},{"id":337,"pid":26,"city":"遂寧"},{"id":338,"pid":26,"city":"雅安"},{"id":339,"pid":26,"city":"宜賓"},{"id":340,"pid":26,"city":"資陽"},{"id":341,"pid":26,"city":"自貢"},{"id":342,"pid":26,"city":"瀘州"},{"id":367,"pid":30,"city":"昆明"},{"id":378,"pid":30,"city":"曲靖"},{"id":3100,"pid":367,"city":"盤龍區(qū)"},{"id":3101,"pid":367,"city":"五華區(qū)"},{"id":3102,"pid":367,"city":"官渡區(qū)"},{"id":3103,"pid":367,"city":"西山區(qū)"},{"id":3104,"pid":367,"city":"東川區(qū)"},{"id":3105,"pid":367,"city":"安寧市"},{"id":3106,"pid":367,"city":"呈貢縣"},{"id":3107,"pid":367,"city":"晉寧縣"},{"id":3108,"pid":367,"city":"富民縣"},{"id":3109,"pid":367,"city":"宜良縣"},{"id":3110,"pid":367,"city":"嵩明縣"},{"id":3111,"pid":367,"city":"石林縣"},{"id":3112,"pid":367,"city":"祿勸"},{"id":3113,"pid":367,"city":"尋甸"},{"id":3189,"pid":378,"city":"麒麟?yún)^(qū)"},{"id":3190,"pid":378,"city":"宣威市"},{"id":3191,"pid":378,"city":"馬龍縣"},{"id":3192,"pid":378,"city":"陸良縣"},{"id":3193,"pid":378,"city":"師宗縣"},{"id":3194,"pid":378,"city":"羅平縣"},{"id":3195,"pid":378,"city":"富源縣"},{"id":3196,"pid":378,"city":"會澤縣"},{"id":3197,"pid":378,"city":"沾益縣"}]; var treeData = createTree({ idname:'id', pidname:'pid', rootid:1, data:data }); function createTree(arg){ var idname = arg.idname, pidname = arg.pidname, rootid = arg.rootid, data = arg.data, treeData = []; var _createTree = function(id){ var ret = []; var index = 0; for(var i = 0; i < data.length; i++){ if(data[i][pidname] == id){ ret[index] = data[i]; ret[index].child = _createTree(data[i][idname]); index++; } } return ret; } var index = 0; for(var i = 0; i < data.length; i++){ if(data[i][pidname] == rootid){ treeData[index] = data[i]; treeData[index].child = _createTree(data[i][idname]); index++; } } return treeData; } Vue.component('item', { template: '#tree-template', props: ['tree'], data: function () { return {} }, methods: { toggle: function (i) { this.tree[i].open = !this.tree[i].open; this.$set(this.tree, i, this.tree[i]); }, isFolder: function (data) { return data.child && data.child.length }, }, }) var vm = new Vue({ el: '#app', data: { treeData: treeData, show:false, }, methods: { hideTree:function(e){ if(e.target.id == 'app'){ console.log(137); this.show = false; } } }, created: function () { function _addOpen(data) { for (var i = 0; i < data.length; i++) { data[i]['open'] = false; if (data[i].child.length > 0) { _addOpen(data[i].child); } } } _addOpen(this.treeData); } }); </script> </body> </html>
Vue具體輕量級框架、簡單易學(xué)、雙向數(shù)據(jù)綁定、組件化、數(shù)據(jù)和結(jié)構(gòu)的分離、虛擬DOM、運(yùn)行速度快等優(yōu)勢,Vue中頁面使用的是局部刷新,不用每次跳轉(zhuǎn)頁面都要請求所有數(shù)據(jù)和dom,可以大大提升訪問速度和用戶體驗(yàn)。
以上是“vue如何實(shí)現(xiàn)的樹形菜”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)站題目:vue如何實(shí)現(xiàn)的樹形菜
URL標(biāo)題:http://m.newbst.com/article42/jheiec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、App開發(fā)、網(wǎng)站設(shè)計(jì)、微信小程序、建站公司、用戶體驗(yàn)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)