Java 如何在 JasperReports 中使用 printWhenExpression
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/294989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use printWhenExpression in JasperReports
提问by
Can somebody tell me how to use the printWhenExpression
of JasperReports?
有人能告诉我如何使用printWhenExpression
JasperReports 吗?
回答by VonC
Do you have an error relative to boolean ?
你有一个相对于 boolean 的错误吗?
Because you need to use Boolean instead of the primitive type.
因为你需要使用 Boolean 而不是原始类型。
So:
所以:
$F{mesure} != "PH"
($F{userfd4}).equals("1") ? true : false
would give cannot cast from boolean to Boolean
.
会给cannot cast from boolean to Boolean
。
( $F{mesure}.startsWith("PH") ? Boolean.TRUE:Boolean.FALSE )
($F{userfd4}).equals("1") ? Boolean.TRUE : Boolean.FALSE
would be correct.
会是正确的。
See also this example
另请参阅此示例
Update Nov 2015 (7 years later)
2015 年 11 月更新(7 年后)
Petter Fribergpoints out in the comments:
In jasper report6.0 this is not need:
You can return eitherboolean
orBoolean
a simple expression like$F{fieldName}.equals("hello")
will work.
在jasper report6.0 中,这不是必需的:
您可以返回一个boolean
或Boolean
一个简单的表达式,如$F{fieldName}.equals("hello")
将工作。
You can see a demo of that command in demo/samples/tableofcontents/reports/TocPart.jrxml
您可以在中看到该命令的演示 demo/samples/tableofcontents/reports/TocPart.jrxml
<reportElement style="Sans_Bold" positionType="Float" x="50" y="0" width="100" height="15" isRemoveLineWhenBlank="true" uuid="db8b68c6-4430-4199-8967-3ab5c077cb56">
<property name="local_mesure_unitx" value="pixel"/>
<property name="com.jaspersoft.studio.unit.x" value="px"/>
<printWhenExpression><![CDATA[$F{level} == 1]]></printWhenExpression>
</reportElement>
回答by Dónal
The other poster has done a good job of explaining the technical details of how to use it, so I'll try and explain the circumstances in which one might find it useful.
另一张海报在解释如何使用它的技术细节方面做得很好,所以我将尝试解释人们可能会发现它有用的情况。
Basically, it allows you to show or hide the contents of a cell based on a boolean expression. For example, you might want to show a person's name only if that person is over 18, then in the name field, using a printwhenexpression like:
基本上,它允许您根据布尔表达式显示或隐藏单元格的内容。例如,您可能希望仅在该人年满 18 岁时才显示该人的姓名,然后在姓名字段中使用如下所示的 printwhen 表达式:
$F{age} >= 18
回答by Dónal
You can also use the static method "Boolean.valueOf(boolean b)". It does exactly the same logic as "($F{mesure}.startsWith("PH") ? Boolean.TRUE:Boolean.FALSE)" and good rule of thumb is don't recreate the wheel.
您还可以使用静态方法“Boolean.valueOf(boolean b)”。它的逻辑与“($F{mesure}.startsWith("PH") ? Boolean.TRUE:Boolean.FALSE)”完全相同,好的经验法则是不要重新创建轮子。
Boolean.valueOf($F{mesure}.startsWith("PH"))
Boolean.valueOf($F{userfd4}).equals("1"))
Boolean.valueOf($F{mesure}.startsWith("PH"))
Boolean.valueOf($F{userfd4}).equals("1"))
回答by Thakhani Tharage
$P{mesure}.equals("200") should work without any additional details. It will print if the condition is true
$P{mesure}.equals("200") 应该可以在没有任何额外细节的情况下工作。如果条件为真,它将打印