如何在运行时显示/隐藏列? [英] How to show/hide a column at runtime?

查看:121
本文介绍了如何在运行时显示/隐藏列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据特定条件在运行时显示/隐藏列。我正在使用表达时打印来有条件地在我的报告中显示/隐藏此列(及其标题)。当列被隐藏时,它占用的空间是空白的,这不是特别有吸引力。

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:


  • 报告的宽度减少了隐藏列的宽度

  • 额外的空间分布在剩余的列中

理论上,我可以通过设置列的宽度来实现第一个(和标题)为0,但也表示该列应调整大小以适应其内容。但JasperReports没有提供调整宽度以适应内容选项。

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.

另一种可能性是使用Jasper API生成报告,而不是使用XML定义报告模板。但这对于这样一个简单的要求似乎付出了很多努力。

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报告的后续版本(v5或更高版本)中您可以使用 jr:table 组件和真正实现此目的(不使用java代码作为使用动态jasper或动态报告)。

In later version (v5 or above) of jasper reports you can use the jr:table component and truly achieve this (without the use of java code as using dynamic-jasper or dynamic-reports).

该方法在< jr下使用< printWhenExpression /> :column />

The method is using a <printWhenExpression/> under the <jr:column/>

示例数据

+----------------+--------+
|      User      |  Rep   |
+----------------+--------+
| Jon Skeet      | 854503 |
| Darin Dimitrov | 652133 |
| BalusC         | 639753 |
| Hans Passant   | 616871 |
| Me             |   6487 |
+----------------+--------+

示例jrxml

注意:参数 $ P {displayRecordNumber} 和第一个 jr:列下的< printWhenExpression>

Note: the parameter $P{displayRecordNumber} and the <printWhenExpression> under first 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>

输出$ P {displayRecordNumber} = true

输出$ P {displayRecordNumber} = false

正如您所看到的那样,列很好地适应了显示的基础。

这篇关于如何在运行时显示/隐藏列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆