条件渲染
根据条件动态修改单元格样式、颜色、字体、分页等视觉属性
概述
条件渲染(渲染项 renderItems)允许在满足特定条件时,动态修改单元格的视觉属性——包括背景色、字体颜色、字体大小、边框、强制分页等——无需额外的单元格或复杂的版面设计。
使用场景
- 负数显示为红色
- 超出预警值的行标红背景
- 库存不足时字体加粗
- 每组数据切换时强制分页
- 条件控制列宽/行高变化
作用范围(scope)
| 值 | 说明 |
|---|---|
cell | 仅当前单元格 |
row | 整行(常用于行变色) |
column | 整列 |
可修改的属性类型
| 类型 | 可设置属性 | 说明 |
|---|---|---|
background | backgroundColor | 单元格/行背景色(#RRGGBB) |
color | fontColor | 字体颜色 |
font | fontFamily / fontSize / bold / italic / underline | 字体样式 |
border | leftBorder / rightBorder / topBorder / bottomBorder | 边框 |
height | height | 动态行高 |
width | width | 动态列宽 |
newValue | newValue(表达式) | 替换单元格显示值 |
paging | pagingType: "before" / "after" | 动态强制分页 |
条件运算符
条件中 operator 支持以下值:
equals notEquals greatThen EqualsGreatThen lessThen equalsLessThen contains notContains startsWith endsWith isEmpty isNotEmpty
示例:负数标红
当 $$value < 0 时,将字体颜色改为红色:
renderItems: [{
name: "负数红色",
enabled: true,
conditions: [{
joinType: "and",
itemType: "common",
leftValue: "$$value",
operator: "lessThen",
rightValueType: "Number",
rightValue: 0
}],
contents: [{
type: "color",
scope: "cell",
fontColor: "#FF0000"
}]
}]
示例:整行背景交替
通过 seq() 序号奇偶实现隔行变色:
conditions: [{
leftValue: "$$value", // 用表达式类型:seq() % 2 == 0
operator: ...,
rightValueType: "Expression",
rightValue: "seq() % 2 == 0"
}],
contents: [{
type: "background",
scope: "row",
backgroundColor: "#F5F5F5"
}]
示例:分组切换时强制分页
当分组字段值变化时(即每进入新分组),在该行之前强制分页:
contents: [{
type: "paging",
scope: "cell",
pagingType: "before" // 在此单元格之前分页
}]
提示:通常将分组条件渲染加在分组标题单元格(
aggregateType=group),触发条件为&A2 > 1(跳过第一组,避免第一页前产生空白页)。