jasper-reports - 当组发生变化时如何应用条件样式?

标签 jasper-reports

有很多关于如何在报告的上设置斑马条纹的教程。像这样:

+-------+-------+
| 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 |
+-------+-------+

我使用“值”列作为组的表达式,并且我的数据按“值”排序。 “black”是当组为黑色时要打印的黑色矩形。 “白色”是没有黑色矩形。我想要一个可以放入黑色矩形的“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 BooleanintValue() code> 只是用来与旧的 jasper 报告版本兼容,在最新版本中不需要它

结果

Result

关于jasper-reports - 当组发生变化时如何应用条件样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35158278/

相关文章:

jasper-reports - 在报告中包含文本文件

http - 通过 REST 放置 reportUnit 失败 - Tomcat 错误

java - Noto-sans 不与 Jasper 合作

java - 当组更改时如何更改细节带颜色?

java - 如何使用 DynamicReports 加载后自动打印报告?

jakarta-ee - 如何获取jasper文件中定义的参数?

java - 在 Jaspersoft 中处理复杂类型(列表和对象)的正确方法是什么?

jasper-reports - 具有预定义列数的报告中的列文本重叠的问题

maven - jasperreports-maven-plugin 的稳定版本是什么?

java - 如何通过 API 从 JasperReport 对象(.jasper 编译文件)获取子报表名称?