Vue組件模板形式實(shí)現(xiàn)對(duì)象數(shù)組數(shù)據(jù)循環(huán)為樹(shù)形結(jié)構(gòu)(實(shí)例代碼)
數(shù)據(jù)結(jié)構(gòu)為數(shù)組中包含對(duì)象--樹(shù)形結(jié)構(gòu),用Vue組件的寫法實(shí)現(xiàn)以下的效果:
樹(shù)形列表,縮進(jìn)顯示層級(jí),第5級(jí)數(shù)據(jù)加底色,數(shù)據(jù)樣式顯色,點(diǎn)擊展開(kāi)折疊數(shù)據(jù)。本文為用Vue實(shí)現(xiàn)方式,另有一篇為用knockout.js的實(shí)現(xiàn)方法。
html代碼
<div id="table-component-div"> <table-component v-for="item in data1" v-bind:list="item"></table-component> </div>
組件模板代碼
<script type="text/x-template" id="table-component-template"> <div class="tem"> <div class="tem-p"> <div v-on:click="toggleClick"><i v-bind:style="{'visibility':list.number!=0?'visible':'hidden'}">{{list.number}}</i><span>{{list.name}}</span></div> <!--綁定數(shù)據(jù)--> <div><span>{{list.energyone}}</span></div> <div><span>{{list.energytwo}}</span></div> <div><span>{{list.energythree}}</span></div> <!--綁定class,使數(shù)值顯示出區(qū)分--> <div><span v-bind:class="{'isgreen':list.huanRatio<0,'isred':list.huanRatio>100}">{{list.huanRatio}}<em>%</em></span></div> <div><span v-bind:class="{'isgreen':list.tongRatio<0,'isred':list.tongRatio>100}">{{list.tongRatio}}<em>%</em></span></div> </div> <div class="tem-c"> <!-- 子組件 --> <table-component v-for="itemc in list.child" :list="itemc"></table-component> </div> </div> </script>
JavaScript代碼
/* 數(shù)據(jù)結(jié)構(gòu) */ var ko_vue_data=[ { name: "總能耗", number:"0", energyone: 14410, energytwo: 1230, energythree: 1230, huanRatio: -36.8, tongRatio: 148.5, child: [ { name: "租戶電耗", number:"1", energyone: 14410, energytwo: 1230, energythree: 1230, huanRatio: -36.8, tongRatio: 148.5, child: [] }, { name: "公共用電", number:"2", energyone: 14410, energytwo: 1230, energythree: 1230, huanRatio: -36.8, tongRatio: 148.5, child: [ { name: "暖通空調(diào)", number:"2.1", energyone: 14410, energytwo: 1230, energythree: 1230, huanRatio: -36.8, tongRatio: 148.5, child: [ { name: "冷站", number:"2.1.1", energyone: 14410, energytwo: 1230, energythree: 1230, huanRatio: -36.8, tongRatio: 148.5, child: [ { name: "冷水機(jī)組", number:"2.1.1.1", energyone: 14410, energytwo: 1230, energythree: 1230, huanRatio: -36.8, tongRatio: 148.5, child: [] } ] }, { name: "熱力站", number: "2.1.2", energyone: 14410, energytwo: 1230, energythree: 1230, huanRatio: -36.8, tongRatio: 148.5, child: [] } ] } ] } ] } ]; /* 注冊(cè)組件 */ Vue.component('table-component', { template:"#table-component-template",//模板 props:['list'],//傳遞數(shù)據(jù) computed:{//計(jì)算屬性 isFolder: function () {//判斷數(shù)據(jù)有沒(méi)有子集,此效果中暫沒(méi)用到,有需要的可以看下具體使用方式 return this.list.child && this.list.child.length > 0; } }, methods:{ /* 展開(kāi)折疊操作 */ toggleClick:function(event){ event.stopPropagation();//阻止冒泡 var _this = $(event.currentTarget);//點(diǎn)擊的對(duì)象 if (_this.parent().next().is(":visible")) { _this.parent().next().slideUp(); } else { _this.parent().next().slideDown(); } } } }); /* 創(chuàng)建實(shí)例 */ new Vue({ el:"#table-component-div",//掛載dom data:{ data1:ko_vue_data//數(shù)據(jù) } })
數(shù)據(jù)顯示完畢,接下來(lái)是樣式效果,縮進(jìn)顯示層級(jí)及底色顯示。
css代碼
.tem-p{ clear: both; border-bottom: 1px solid #dddddd; height: 30px; line-height: 30px; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } .tem-p>div{ float: left; width:100px; box-sizing: border-box; -ms-text-overflow: ellipsis; text-overflow: ellipsis; white-space:nowrap; overflow: hidden; text-align: center; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; height: 100%; border-right: 1px solid #dddddd; } .tem-p>div:first-of-type{ width: 298px; text-align: left; } .tem>.tem-c .tem-p>div:first-of-type{ padding-left: 30px; } .tem>.tem-c .tem-c .tem-p>div:first-of-type{ padding-left: 60px; } .tem>.tem-c .tem-c .tem-c .tem-p>div:first-of-type{ padding-left: 90px; } .tem>.tem-c .tem-c .tem-c .tem-c .tem-p>div:first-of-type{ padding-left: 120px; } .tem>.tem-c .tem-c .tem-c .tem-c .tem-p{ background-color: #f8f8f8; } .tem>.tem-c .tem-c .tem-c .tem-c .tem-c .tem-p>div:first-of-type{ padding-left: 150px; } .lastChild{ background: #f8f8f8; } .isred{ color: red; } .isgreen{ color: green; }
總結(jié)
以上所述是小編給大家介紹的Vue組件模板形式實(shí)現(xiàn)對(duì)象數(shù)組數(shù)據(jù)循環(huán)為樹(shù)形結(jié)構(gòu),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- vue遞歸實(shí)現(xiàn)樹(shù)形組件
- Vue組件庫(kù)ElementUI實(shí)現(xiàn)表格加載樹(shù)形數(shù)據(jù)教程
- Vue遞歸組件+Vuex開(kāi)發(fā)樹(shù)形組件Tree--遞歸組件的簡(jiǎn)單實(shí)現(xiàn)
- vue用遞歸組件寫樹(shù)形控件的實(shí)例代碼
- 用 Vue.js 遞歸組件實(shí)現(xiàn)可折疊的樹(shù)形菜單(demo)
- Vue.js遞歸組件構(gòu)建樹(shù)形菜單
- vuejs使用遞歸組件實(shí)現(xiàn)樹(shù)形目錄的方法
- 基于 Vue 的樹(shù)形選擇組件的示例代碼
- Vue組件tree實(shí)現(xiàn)樹(shù)形菜單
- vue實(shí)現(xiàn)自定義樹(shù)形組件的示例代碼
相關(guān)文章
Vue實(shí)現(xiàn)記住賬號(hào)密碼功能的操作過(guò)程
這篇文章主要介紹了Vue實(shí)現(xiàn)記住賬號(hào)密碼功能,用戶登錄時(shí)若勾選“記住我”功能選項(xiàng),則將登錄名和密碼(加密后)存入本地緩存,下次登錄頁(yè)面加載時(shí)自動(dòng)獲取保存好的賬號(hào)和密碼(需解密),回顯到登錄輸入框中,下面分享我實(shí)現(xiàn)的具體步驟,需要的朋友可以參考下2022-07-07vue實(shí)現(xiàn)一個(gè)移動(dòng)端屏蔽滑動(dòng)的遮罩層實(shí)例
本篇文章主要介紹了vue實(shí)現(xiàn)一個(gè)移動(dòng)端屏蔽滑動(dòng)的遮罩層實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-06-06Vue.js devtool插件安裝后無(wú)法使用的解決辦法
Vue.js devtool插件最近在開(kāi)發(fā)人員中很火,這篇文章主要為大家詳細(xì)介紹了Vue.js devtool插件安裝后無(wú)法使用,出現(xiàn)提示“vue.js not detected”的解決辦法2017-11-11Vue使用mockjs問(wèn)題(返回?cái)?shù)據(jù)、get、post 請(qǐng)求)
這篇文章主要介紹了Vue使用mockjs問(wèn)題(返回?cái)?shù)據(jù)、get、post 請(qǐng)求),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2023-05-05Vue中使用Echarts響應(yīng)式布局flexible.js+rem適配方案詳解
這篇文章主要介紹了Vue中使用Echarts響應(yīng)式布局flexible.js+rem適配方案詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-01-01vue中關(guān)于template報(bào)錯(cuò)等問(wèn)題的解決
這篇文章主要介紹了vue中關(guān)于template報(bào)錯(cuò)等問(wèn)題的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04