el-tree实现自定义节点内容

直接上代码: 

<el-tree
					:data="treeData"
					show-checkbox
					node-key="id"
					:default-expand-all="true"
					:check-on-click-node="false"
					@check="setCarToList"
					:props="defaultProps"
					class="filter-tree"
					:filter-node-method="this.select==='1'?filterNode:filterNode1"
					ref="tree"
				>
				<span slot-scope="{ node, data }">
					<span>
						<template v-if="data.flag === 'car'">
							<template v-if="data.carInfo.zhuangtai === '在线'">
								<img src="@/assets/images/shouye/zaixian.png" alt="车辆图标" style="width:15px;height:15px;">
							</template>
							<template v-if="data.carInfo.zhuangtai === '离线'">
								<img src="@/assets/images/shouye/lixian.png" alt="车辆图标" style="width:15px;height:15px;">
							</template>
						</template>
						<template v-if="data.flag === 'dept'">
							<img src="@/assets/images/shouye/dept.png" alt="单位图标" style="width:15px;height:15px;">
						</template>
					</span>
					<span style="font-size:14px">{{ node.label }}</span>
					<template v-if="data.flag === 'dept'">
						<span style="font-size:14px">&nbsp;[{{ data.online }}/{{ data.total }}]</span>
					</template>
					<template v-if="data.flag === 'car'">
						<span style="font-size:14px">&nbsp;({{ data.sheng }}&nbsp;{{ data.carInfo.zhuangtai }})</span>
					</template>
				</span>
				</el-tree>

实现效果如下:

数据定义:

treeData: [
				{
				id: 1,
				label: "一级单位1",
				flag:"dept",
				total:10,//总数
				online:1,//在线数
				children: [
					{
					id: 4,
					label: "二级单位1-1" ,
					flag:"dept",
					total:10,//总数
					online:1,//在线数
					children: [
						{
						id: 9,
						label: "三级单位1-1-1" ,
						flag:"dept",
						total:10,//总数
						online:1,//在线数
						children: [
							{
							id: 12,
							label: "甘A34567",
							flag:"car",
							sheng:"甘肃省",
							shi:"",
							qu:"",
							carInfo: {
								startOff: true,
								cheliangid:64,
								cheliangmingcheng: "甘A34567",
								shishisudu: "10km/h",
								fuwuqishijian: "2021-08-05 09:10",
								shebeishijian: "2021-08-05 09:10",
								shebeibianma: "bfdddd",
								liuliangkahao: "1245682248",
								suoshukehu: "三级单位1-1-1",
								dianhuozhuangtai: "主电开",
								zonglicheng: "2000",
								dangrilicheng: "20",
								chezhuName: "张继科",
								chezhuPhone: "12345698451",
								anzhuangshijian: "2020-10-26",
								dangritangshu: "1",
								jingdu: "119.479321",
								weidu: "39.938574",
								shebeiIMIS: "wewwwwwww",
								beizhu: "wu",
								youliang1: "110",
								youliang2: "256",
								zongyouliang: "366",
								jiluyisudu: "10km/h",
								jiashiyuanName: "温顿瑟夫",
								jiashiyuanPhone: "123456789645",
								huichuanshijian: "",
								guoqishijian: "",
								wendu: "10",
								shidu: "23",
								baojing: "超速",
								zhuangtai: "在线",
								weizhi: "甘肃省兰州市城关区雁南街道雁滩路甘肃人防大厦西45米",
							},
							},
							{
							id: 14,
							label: "甘B45343",
							flag:"car",
							sheng:"甘肃省",
							shi:"",
							qu:"",
							carInfo: {
								startOff: true,
								cheliangid:65,
								cheliangmingcheng: "甘B45343",
								shishisudu: "10km/h",
								fuwuqishijian: "2021-08-05 09:10",
								shebeishijian: "2021-08-05 09:10",
								shebeibianma: "bfdddd",
								liuliangkahao: "1245682248",
								suoshukehu: "三级单位1-1-1",
								dianhuozhuangtai: "主电开",
								zonglicheng: "2000",
								dangrilicheng: "20",
								chezhuName: "张继科",
								chezhuPhone: "12345698451",
								anzhuangshijian: "2020-10-26",
								dangritangshu: "1",
								jingdu: "115.795795",
								weidu: "40.000893",
								shebeiIMIS: "wewwwwwww",
								beizhu: "wu",
								youliang1: "110",
								youliang2: "256",
								zongyouliang: "366",
								jiluyisudu: "10km/h",
								jiashiyuanName: "温顿瑟夫",
								jiashiyuanPhone: "123456789645",
								huichuanshijian: "",
								guoqishijian: "",
								wendu: "10",
								shidu: "23",
								baojing: "疲劳驾驶",
								zhuangtai: "在线",
								weizhi: "甘肃省兰州市城关区雁南街道雁滩路甘肃人防大厦西45米",
							},
							},
						],
						},
						{
						id: 10,
						label: "甘C34567",
						flag:"car",
						sheng:"甘肃省",
						shi:"",
						qu:"",
						carInfo: {
							startOff: true,
							cheliangid:66,
							cheliangmingcheng: "甘C34567",
							shishisudu: "10km/h",
							fuwuqishijian: "2021-08-05 09:10",
							shebeishijian: "2021-08-05 09:10",
							shebeibianma: "bfdddd",
							liuliangkahao: "1245682248",
							suoshukehu: "二级单位1-1",
							dianhuozhuangtai: "主电开",
							zonglicheng: "2000",
							dangrilicheng: "20",
							chezhuName: "张继科",
							chezhuPhone: "12345698451",
							anzhuangshijian: "2020-10-26",
							dangritangshu: "1",
							jingdu: "115.862836",
							weidu: "39.726753",
							shebeiIMIS: "wewwwwwww",
							beizhu: "wu",
							youliang1: "110",
							youliang2: "256",
							zongyouliang: "366",
							jiluyisudu: "10km/h",
							jiashiyuanName: "温顿瑟夫",
							jiashiyuanPhone: "123456789645",
							huichuanshijian: "",
							guoqishijian: "",
							wendu: "10",
							shidu: "23",
							baojing: "其他",
							zhuangtai: "在线",
							weizhi: "甘肃省兰州市城关区雁南街道雁滩路甘肃人防大厦西45米",
						},
						},
					],
					},
				],
				},
				{
				id: 2,
				label: "一级单位2",
				children: [
					{
					id: 5,
					label: "二级单位2-1",
					flag:"dept",
					total:10,//总数
					online:1,//在线数
					},
					{
					id: 6,
					label: "甘D45343",
					flag:"car",
						sheng:"甘肃省",
						shi:"",
						qu:"",
					carInfo: {
						startOff: true,
						cheliangid:67,
						cheliangmingcheng: "甘D45343",
						shishisudu: "10km/h",
						fuwuqishijian: "2021-08-05 09:10",
						shebeishijian: "2021-08-05 09:10",
						shebeibianma: "bfdddd",
						liuliangkahao: "1245682248",
						suoshukehu: "二级单位2-1",
						dianhuozhuangtai: "主电开",
						zonglicheng: "2000",
						dangrilicheng: "20",
						chezhuName: "张继科",
						chezhuPhone: "12345698451",
						anzhuangshijian: "2020-10-26",
						dangritangshu: "1",
						jingdu: "117.150433",
						weidu: "40.215925",
						shebeiIMIS: "wewwwwwww",
						beizhu: "wu",
						youliang1: "110",
						youliang2: "256",
						zongyouliang: "366",
						jiluyisudu: "10km/h",
						jiashiyuanName: "温顿瑟夫",
						jiashiyuanPhone: "123456789645",
						huichuanshijian: "",
						guoqishijian: "",
						wendu: "10",
						shidu: "23",
						baojing: "超速",
						zhuangtai: "离线",
						weizhi: "甘肃省兰州市城关区雁南街道雁滩路甘肃人防大厦西45米",
					},
					},
				],
				},
				{
				id: 11,
				label: "一级单位3",
				flag:"dept",
				total:10,//总数
				online:1,//在线数
				children: [
					{
					id: 7,
					label: "二级单位3-1",
					flag:"dept",
					total:10,//总数
					online:1,//在线数
					},
					{
					id: 8,
					label: "二级单位3-2",
					flag:"dept",
					total:10,//总数
					online:1,//在线数
					},
				],
				},
			],

 

### 关于 `el-tree` 中自定义节点样式的解决方案 在 `el-tree` 组件中,可以通过多种方式来自定义节点样式。一种常见的方式是利用 scoped slot 来定制化渲染每个节点内容,并通过 CSS 类来调整样式。 #### 使用 Scoped Slot 定制节点内容 借助 scoped slot 可以完全控制树节点的显示逻辑以及外观设计: ```html <template> <el-tree :data="data" :props="defaultProps"> <!-- 自定义节点模板 --> <template #default="{ node, data }"> <span class="custom-tree-node"> <span>{{ node.label }}</span> <span> <el-button type="text" size="mini">操作</el-button> </span> </span> </template> </el-tree> </template> <script> export default { data() { return { data: [{ label: '一级 1', children: [{ label: '二级 1-1' }] }], defaultProps: { children: 'children', label: 'label' } }; } }; </script> <style scoped> .custom-tree-node { flex: 1; display: flex; align-items: center; justify-content: space-between; font-size: 14px; padding-right: 8px; } </style> ``` 上述代码展示了如何创建一个具有自定义按钮的操作区域,并且可以进一步修改 `.custom-tree-node` 的样式以适应具体需求[^3]。 #### 添加特定类名或内联样式 如果只需要简单地改变某些节点的颜色或其他基本属性,则可以直接给这些元素添加额外的类名或是使用 inline-style 属性来进行快速设置。例如,在上面的例子基础上增加条件判断为不同类型的节点赋予不同的背景颜色: ```javascript <span :class="[{'highlight': isImportant(data)}, 'node-label']">{{ node.label }}</span> ... methods: { isImportant(nodeData){ // 根据业务逻辑返回 true 或 false return nodeData.priority === "high"; } } ``` 配合相应的CSS规则: ```css .highlight{ background-color:#ffeb9c !important; /* 高亮 */ } .node-label{ color:green; } ``` 这种方法适用于那些不需要复杂交互逻辑的情况下的样式微调。 #### 动态绑定 style 对象 对于更复杂的场景下动态计算样式的需求,还可以考虑使用 Vue 提供的数据绑定特性——即通过对象形式传递给 `v-bind:style` 指令完成更加灵活多变的效果呈现: ```vue <!-- HTML部分 --> <span v-bind:style="getNodeStyle(data)">...</span> // JS部分 computed:{ getNodeStyle(){ return function(item){ let styles={}; if (item.type==='folder'){ Object.assign(styles,{fontWeight:'bold'}); } return styles; } } } ``` 这种方式允许基于数据模型实时更新视图层面上的表现特征,非常适合处理依赖于运行时状态变化而产生的视觉反馈问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

甜甜凉白开

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值