Vue Element-ui實現(xiàn)樹形控件節(jié)點添加圖標(biāo)詳解
更新時間:2021年11月23日 15:57:05 作者:一只小木頭.
這篇文章主要為大家介紹了Element-ui實現(xiàn)樹形控件節(jié)點添加圖標(biāo),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
1.效果圖

2.樹形表格綁定數(shù)據(jù)加標(biāo)簽
想要在樹形控件的樹節(jié)點加上圖片或者element-ui的圖標(biāo),可以在樹形表格綁定數(shù)據(jù)中加上標(biāo)簽icon
children: [
{
icon:'el-icon-top-right',
label: ['beam名稱',''],
children: [
{
label:['name','RS49'],
},
{
icon:'src/assets/images/Organization.png',
label:['group('+'3'+')','']
children:[
{
label:['10600361','10950','11200','0']
}
]
}
]
}
],
在樹形控件自定義函數(shù)中
直接讓class等于element-ui的icon標(biāo)簽
img標(biāo)簽需要加上自己圖片的地址
renderContent(h,{node,data,store}){
// div代表樹形控件的一行,div中包含三個span標(biāo)簽
// 判斷節(jié)點的label數(shù)組數(shù)量,通過三目運算來選擇class
// 設(shè)置class來控制樹形控件進(jìn)行對齊
return h('div',[
// 在樹形控件自定義函數(shù)中增加icon和圖片的標(biāo)簽
// img標(biāo)簽需要加上自己圖片的地址
h('span',{class:'top-right'}),
h('img',{src:data.icon}),
h('span', {class:node.label.length === 2 ? 'nodeStyle':'groupStyle'},node.label[0]),
h('span', {class:'groupStyle'},node.label[1]),
h('span',{class:node.label.length === 2 ? 'nodeStyle':'groupStyle'},node.label.length === 2 ? '':node.label[2])
]);
},
3.所有代碼
<template>
<div class="mytree">
<el-tree
:data="tree_data"
:props="defaultProps"
@node-click="handleNodeClick"
indent="0"
:render-content="renderContent"
></el-tree>
</div>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue'
export default defineComponent({
components: {},
data() {
return {
tree_data: [
{
// type:1,
label: 'notice-id1',
children: [
{
label: ['衛(wèi)星名稱代號','ZOHREH-2'],
},
{
label: ['組織機(jī)構(gòu)','IRN'],
},
{
label: ['頻率范圍','10950-1450'],
},
{
icon:'el-icon-top-right',
label: ['beam名稱',''],
children: [
{
label:['name','RS49'],
},
{
label:['freq_min','10950'],
},
{
label:['freq_max','14500'],
},
{
icon:'src/assets/images/Organization.png',
label:['group('+'3'+')','']
children:[
{
label:['10600361','10950','11200','0']
},
{
label:['10600361','10950','11200','0']
},
{
label:['10600361','10950','11200','0']
}
]
}
]
},
],
},
],
defaultProps: {
children: 'children',
label: 'label',
},
}
},
method:{
// 自定義樹形控件函數(shù) node代表每個節(jié)點
renderContent(h,{node,data,store}){
// div代表樹形控件的一行,div中包含三個span標(biāo)簽
// 判斷節(jié)點的label數(shù)組數(shù)量,通過三目運算來選擇class
// 設(shè)置class來控制樹形控件進(jìn)行對齊
return h('div',[
// 在樹形控件自定義函數(shù)中增加icon和圖片的標(biāo)簽
h('span',{class:[data.icon,data.icon==='el-icon-top-right'? 'top-right':'bottom-left']}),
h('img',{src:data.icon === 'src/assets/images/Organization.png' ? data.icon:''}),
h('span', {class:node.label.length === 2 ? 'nodeStyle':'groupStyle'},node.label[0]),
h('span', {class:'groupStyle'},node.label[1]),
h('span',{class:node.label.length === 2 ? 'nodeStyle':'groupStyle'},node.label.length === 2 ? '':node.label[2])
]);
},
}
})
</script>
<style lang="scss" scoped>
.nodeStyle{
width:110px;
display:inline-block;
text-align:left;
}
.groupStyle{
width:150px;
display:inline-block;
text-align:left;
}
</style>
其他實現(xiàn)
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Vue源碼解析之?dāng)?shù)據(jù)響應(yīng)系統(tǒng)的使用
這篇文章主要介紹了Vue源碼解析之?dāng)?shù)據(jù)響應(yīng)系統(tǒng)的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
vue3+vite引入插件unplugin-auto-import的方法
這篇文章主要介紹了vue3+vite引入插件unplugin-auto-import的相關(guān)知識,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值?,需要的朋友可以參考下2022-10-10

