Java 如何在运行时显示/隐藏列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/116053/
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 show/hide a column at runtime?
提问by Dónal
I'd like to show/hide a column at runtime based on a particular condition. I'm using "Print when expression" to conditionally show/hide this column (and it's header) in my report. When the column is hidden, the space it would have occupied is left blank, which is not particularly attractive.
我想在运行时根据特定条件显示/隐藏列。我正在使用“表达式时打印”在我的报告中有条件地显示/隐藏此列(及其标题)。当列被隐藏时,它本应占据的空间是空白的,这不是特别吸引人。
I would prefer if the extra space was used in a more effective manner, possibilities include:
我更希望以更有效的方式使用额外的空间,可能性包括:
- the width of the report is reduced by the width of the hidden column
- the extra space is distributed among the remaining columns
- 报表的宽度减少了隐藏列的宽度
- 额外的空间分布在剩余的列中
In theory, I could achieve the first by setting the width of the column (and header) to 0, but also indicate that the column should resize to fit its contents. But JasperReports does not provide a "resize width to fit contents" option.
理论上,我可以通过将列(和标题)的宽度设置为 0 来实现第一个,但也指示列应该调整大小以适应其内容。但是 JasperReports 不提供“调整宽度以适应内容”选项。
Another possibility is to generate reports using the Jasper API instead of defining the report template in XML. But that seems like a lot of effort for such a simple requirement.
另一种可能性是使用 Jasper API 生成报告,而不是在 XML 中定义报告模板。但是对于这样一个简单的要求来说,这似乎需要付出很多努力。
回答by Jacob Schoen
If it is just one column, is it possible to place this column to the far right, and then use the print when expression. That way there is not a hole in the middle. I know this is not ideal, as I had tried to do what you are currently trying to accomplish in the past, and could not find what I call a good solution.
如果只是一列,是否可以将这一列放在最右边,然后使用print when表达式。这样中间就没有洞了。我知道这并不理想,因为我过去曾尝试做您目前正在尝试完成的工作,但找不到我所说的好的解决方案。
A second idea would to be create a second report based on the first with out the column, and then when calling the report check the condition, to decide which one to call. Again not ideal, but would work.
第二个想法是基于第一个报告创建第二个报告,而不是列,然后在调用报告时检查条件,以决定调用哪个。再次不理想,但会工作。
I know this is not really the answer you were looking for, but one of these suggestions may work for you.
我知道这并不是您真正要寻找的答案,但这些建议之一可能对您有用。
回答by Doug
A slight variation on the "second report" theme that I have used is to isolate the part of the report where you have an optional column into it's own subreport, and then create two subreports, one with and one without the column, and then use conditions to determine which subreport to print.
我使用的“第二个报告”主题的一个细微变化是将报告中具有可选列的部分隔离到它自己的子报告中,然后创建两个子报告,一个有列,一个没有列,然后使用条件来确定要打印的子报表。
回答by Maksim
Check THISIn that tutorial they are using XML template with Velocity framework. This is pretty complex. And to make it simpler you can us DynamicJasper. This library is an open source Java API that works over JasperReports that solves the dynamic columns issue.
检查这个在那个教程中,他们使用带有 Velocity 框架的 XML 模板。这是相当复杂的。为了更简单,您可以使用DynamicJasper。这个库是一个开源 Java API,它在解决动态列问题的 JasperReports 上工作。
回答by Rudi
Remove line when blank: This option takes away the vertical space occupied by an object, if it is not visible; the element visibility is determined by the value of the expression contained in the Print when expression attribute. Think of the page as a grid where the elements are placed, with a line being the space the element occupies. Figure 4-17 highlights the element A line; in order to really remove this line, all the elements that share a portion of the line have to be null (that is, they will not be printed).
空白时删除行:如果对象不可见,此选项会删除对象占用的垂直空间;元素可见性由包含在 Print when 表达式属性中的表达式的值确定。将页面视为放置元素的网格,线条是元素占据的空间。图 4-17 突出显示元素 A 线;为了真正删除这一行,所有共享该行一部分的元素都必须为空(也就是说,它们不会被打印出来)。
回答by Ricardo
I recommend to use DynamicReports, it's open source and based on JasperReports. The main benefit of this library is a dynamic report design and no need for a visual report designer.
我建议使用DynamicReports,它是开源的并且基于 JasperReports。这个库的主要好处是动态报告设计,不需要可视化报告设计器。
回答by W. Goeman
I guess this answer comes way too late, but I add it for the record. In my case I could solve it without any additional dependencies or tools. In the JRXML file, I just added the textfields width a dynamic width multiple times. Once per possible width that is. Then on each textfield, I have set that it should only be printed in case of a certain condition.
我想这个答案来得太晚了,但我将其添加为记录。就我而言,我可以在没有任何额外依赖项或工具的情况下解决它。在 JRXML 文件中,我只是多次将文本字段宽度添加为动态宽度。每个可能的宽度一次。然后在每个文本字段上,我设置它应该只在特定条件下打印。
This might not be as elegant as setting the width dynamically, but it does the trick without any hassle with extra libraries.
这可能不像动态设置宽度那么优雅,但它可以在没有额外库的情况下解决问题。
回答by Cid
JasperDesignis used to modify the template object (JasperReport) from within the code at runtime. I guess this might fit in your case.
JasperDesign用于在运行时从代码中修改模板对象 (JasperReport)。我想这可能适合你的情况。
回答by Petter Friberg
In later version (v5 or above) of jasper reports you can use the jr:table
component and trulyachieve this (without the use of java code as using dynamic-jasper or dynamic-reports).
在 jasper 报告的更高版本(v5 或更高版本)中,您可以使用该jr:table
组件并真正实现这一点(无需像使用 dynamic-jasper 或 dynamic-reports 那样使用 java 代码)。
The method is using a <printWhenExpression/>
under the <jr:column/>
该方法是使用<printWhenExpression/>
下<jr:column/>
Example
例子
Sample Data
样本数据
+----------------+--------+
| User | Rep |
+----------------+--------+
| Jon Skeet | 854503 |
| Darin Dimitrov | 652133 |
| BalusC | 639753 |
| Hans Passant | 616871 |
| Me | 6487 |
+----------------+--------+
Sample jrxml
示例 jrxml
Note: the parameter $P{displayRecordNumber}
and the <printWhenExpression>
under first jr:column
注意:参数$P{displayRecordNumber}
和<printWhenExpression>
下一个jr:column
<?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="reputation" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="a88bd694-4f90-41fc-84d0-002b90b2d73e">
<style name="table">
<box>
<pen lineWidth="1.0" lineColor="#000000"/>
</box>
</style>
<style name="table_TH" mode="Opaque" backcolor="#F0F8FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table_CH" mode="Opaque" backcolor="#BFE1FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<subDataset name="tableDataset" uuid="7a53770f-0350-4a73-bfc1-48a5f6386594">
<field name="User" class="java.lang.String"/>
<field name="Rep" class="java.math.BigDecimal"/>
</subDataset>
<parameter name="displayRecordNumber" class="java.lang.Boolean">
<defaultValueExpression><![CDATA[true]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[]]>
</queryString>
<title>
<band height="50">
<componentElement>
<reportElement key="table" style="table" x="0" y="0" width="555" height="47" uuid="76ab08c6-e757-4785-a43d-b65ad4ab1dd5"/>
<jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
<datasetRun subDataset="tableDataset" uuid="07e5f1c2-af7f-4373-b653-c127c47c9fa4">
<dataSourceExpression><![CDATA[$P{REPORT_DATA_SOURCE}]]></dataSourceExpression>
</datasetRun>
<jr:column width="90" uuid="918270fe-25c8-4a9b-a872-91299cddbc31">
<printWhenExpression><![CDATA[$P{displayRecordNumber}]]></printWhenExpression>
<jr:columnHeader style="table_CH" height="30" rowSpan="1">
<staticText>
<reportElement x="0" y="0" width="90" height="30" uuid="5cd6da41-01d5-4f74-99c2-06784f891d1e"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Record number]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="table_TD" height="30" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="90" height="30" uuid="5fe48359-0e7e-44b2-93ac-f55404189832"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$V{REPORT_COUNT}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="90" uuid="7979d8a2-4e3c-42a7-9ff9-86f8e0b164bc">
<jr:columnHeader style="table_CH" height="30" rowSpan="1">
<staticText>
<reportElement x="0" y="0" width="90" height="30" uuid="61d5f1b6-7677-4511-a10c-1fb8a56a4b2a"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Username]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="table_TD" height="30" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="90" height="30" uuid="a3cdb99d-3bf6-4c66-b50c-259b9aabfaef"/>
<box leftPadding="3" rightPadding="3"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{User}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="90" uuid="625e4e5e-5057-4eab-b4a9-c5b22844d25c">
<jr:columnHeader style="table_CH" height="30" rowSpan="1">
<staticText>
<reportElement x="0" y="0" width="90" height="30" uuid="e1c07cb8-a44c-4a8d-8566-5c86d6671282"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Reputation]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="table_TD" height="30" rowSpan="1">
<textField pattern="#,##0">
<reportElement x="0" y="0" width="90" height="30" uuid="6be2d79f-be82-4c7b-afd9-0039fb8b3189"/>
<box leftPadding="3" rightPadding="3"/>
<textElement textAlignment="Right" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{Rep}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</band>
</title>
</jasperReport>
Output with $P{displayRecordNumber}=true
输出 $P{displayRecordNumber}=true
Output with $P{displayRecordNumber}=false
输出 $P{displayRecordNumber}=false
As you can see the columns adapts nicely on the basis of which are displayed.
正如您所看到的,列在显示的基础上很好地适应。