组更改时如何应用条件样式?
How to apply conditional style when group changes?
有很多教程介绍如何对报告的 行 进行斑马条纹处理。像这样:
+-------+-------+
| Value | Color |
+-------+-------+
| A | white |
| A | black |
| B | white |
| B | black |
| B | white |
| C | black |
| D | white |
| D | black |
+-------+-------+
但我想做的是分组剥离。像这样:
+-------+-------+
| Value | Color |
+-------+-------+
| A | white |
| A | white |
| B | black |
| B | black |
| B | black |
| C | white |
| D | black |
| D | black |
+-------+-------+
我使用列 "Value" 作为组的表达式,我的数据按 "Value" 排序。 "black"是组为黑色时要打印的黑色矩形。 "white" 是没有黑色矩形。我想要一个可以放入黑色矩形 "Print When Expression" 的变量。
到目前为止我尝试过的:
- 创建一个变量$V{print}
- 初始值表达式:false
- 变量表达式:!$V{print}
- 增量类型:组
- 增量组:值
我希望每次组更改时 $V{print} 的值都会更改为相反的值。我得到的是正常的条纹列表(黑,白,黑,白...)
您当前解决的问题是:
calculationType="Nothing"
Nothing: This is the default calculation type that a variable performs. It means that the variable's value is recalculated with every iteration in the data source and that the value returned is obtained by simply evaluating the variable's expression.
这种类型的计算会使您的 incrementType
无效,因此 incrementType
无效,因为我们没有计算。这就是为什么目前你得到黑色、白色、黑色、白色。
这将达到您想要的结果
变量定义(让我们做一些计算示例,每次组更改、求和或计数时递增1)
<variable name="GroupCnt" class="java.lang.Integer" incrementType="Group" incrementGroup="myGroup" calculation="Sum">
<variableExpression><![CDATA[1]]></variableExpression>
</variable>
conditionExpression(我们可以对变量使用取模运算符GroupCnt
)
<conditionExpression><![CDATA[$V{GroupCnt}%2==0]]></conditionExpression>
完整的 jrxml 示例(我添加了一个矩形作为 OP 评论)
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="group" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="c1d9b4b7-6162-4b17-b871-3cf3b867d1ef">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<style name="myStyle">
<conditionalStyle>
<conditionExpression><![CDATA[new Boolean($V{GroupCnt}.intValue()%2==0)]]></conditionExpression>
<style mode="Opaque" forecolor="#FFFFFF" backcolor="#000000"/>
</conditionalStyle>
</style>
<field name="Value" class="java.lang.String"/>
<variable name="GroupCnt" class="java.lang.Integer" incrementType="Group" incrementGroup="myGroup" calculation="Sum">
<variableExpression><![CDATA[1]]></variableExpression>
</variable>
<group name="myGroup">
<groupExpression><![CDATA[$F{Value}]]></groupExpression>
</group>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement style="myStyle" x="0" y="0" width="150" height="20" uuid="7ca1ac35-6249-4ba6-ac87-031f8d410d2e"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{Value}]]></textFieldExpression>
</textField>
<rectangle>
<reportElement style="myStyle" x="150" y="0" width="150" height="20" uuid="d322e0df-0d39-4370-90e6-58305d449852"/>
</rectangle>
</band>
</detail>
</jasperReport>
new Boolean($V{GroupCnt}.intValue()%2==0)
、new Boolean
和 intValue()
仅用于与旧的 jasper 报告版本兼容,最新版本不需要它
结果
有很多教程介绍如何对报告的 行 进行斑马条纹处理。像这样:
+-------+-------+
| Value | Color |
+-------+-------+
| A | white |
| A | black |
| B | white |
| B | black |
| B | white |
| C | black |
| D | white |
| D | black |
+-------+-------+
但我想做的是分组剥离。像这样:
+-------+-------+
| Value | Color |
+-------+-------+
| A | white |
| A | white |
| B | black |
| B | black |
| B | black |
| C | white |
| D | black |
| D | black |
+-------+-------+
我使用列 "Value" 作为组的表达式,我的数据按 "Value" 排序。 "black"是组为黑色时要打印的黑色矩形。 "white" 是没有黑色矩形。我想要一个可以放入黑色矩形 "Print When Expression" 的变量。
到目前为止我尝试过的:
- 创建一个变量$V{print}
- 初始值表达式:false
- 变量表达式:!$V{print}
- 增量类型:组
- 增量组:值
我希望每次组更改时 $V{print} 的值都会更改为相反的值。我得到的是正常的条纹列表(黑,白,黑,白...)
您当前解决的问题是:
calculationType="Nothing"
Nothing: This is the default calculation type that a variable performs. It means that the variable's value is recalculated with every iteration in the data source and that the value returned is obtained by simply evaluating the variable's expression.
这种类型的计算会使您的 incrementType
无效,因此 incrementType
无效,因为我们没有计算。这就是为什么目前你得到黑色、白色、黑色、白色。
这将达到您想要的结果
变量定义(让我们做一些计算示例,每次组更改、求和或计数时递增1)
<variable name="GroupCnt" class="java.lang.Integer" incrementType="Group" incrementGroup="myGroup" calculation="Sum">
<variableExpression><![CDATA[1]]></variableExpression>
</variable>
conditionExpression(我们可以对变量使用取模运算符GroupCnt
)
<conditionExpression><![CDATA[$V{GroupCnt}%2==0]]></conditionExpression>
完整的 jrxml 示例(我添加了一个矩形作为 OP 评论)
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="group" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="c1d9b4b7-6162-4b17-b871-3cf3b867d1ef">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<style name="myStyle">
<conditionalStyle>
<conditionExpression><![CDATA[new Boolean($V{GroupCnt}.intValue()%2==0)]]></conditionExpression>
<style mode="Opaque" forecolor="#FFFFFF" backcolor="#000000"/>
</conditionalStyle>
</style>
<field name="Value" class="java.lang.String"/>
<variable name="GroupCnt" class="java.lang.Integer" incrementType="Group" incrementGroup="myGroup" calculation="Sum">
<variableExpression><![CDATA[1]]></variableExpression>
</variable>
<group name="myGroup">
<groupExpression><![CDATA[$F{Value}]]></groupExpression>
</group>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement style="myStyle" x="0" y="0" width="150" height="20" uuid="7ca1ac35-6249-4ba6-ac87-031f8d410d2e"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{Value}]]></textFieldExpression>
</textField>
<rectangle>
<reportElement style="myStyle" x="150" y="0" width="150" height="20" uuid="d322e0df-0d39-4370-90e6-58305d449852"/>
</rectangle>
</band>
</detail>
</jasperReport>
new Boolean($V{GroupCnt}.intValue()%2==0)
、new Boolean
和 intValue()
仅用于与旧的 jasper 报告版本兼容,最新版本不需要它
结果