简体   繁体   English

如何在Jasper报表中动态显示列?

[英]How to dynamically show columns in a Jasper Report?

I am generating a Jasper Report, which has 5 fixed columns. 我正在生成一个Jasper报告,其中包含5个固定列。 The client has requested the ability to select the number of columns at run time. 客户端已请求能够在运行时选择列数。

For example if he checks 2 columns from a displayed JTable then there should be 2 columns in Jasper report, if he checks 4 then there should be four columns in jasper report. 例如,如果他检查显示的JTable中的2列,那么Jasper报告中应该有2列,如果他检查4,那么jasper报告中应该有4列。 To try to solve this I am thinking of adding condition in jasper report. 为了解决这个问题,我正在考虑在碧玉报告中添加条件。 If the client selects column A and BI will set that as a parameter in the report and use that to determine whether to display the column. 如果客户端选择列A,则BI将在报表中将其设置为参数,并使用该参数确定是否显示该列。

In order to do these, I need to use conditions in the Jasper XML. 为了做到这些,我需要在Jasper XML中使用条件。 Where should I set the conditions in the report. 我应该在哪里在报告中设定条件。

You can try the following 您可以尝试以下

  1. Create a HashMap and set the required properties in it 创建一个HashMap并在其中设置所需的属性

     Map params = new HashMap(); params.put("DISPLAY_COLUMN_ONE", "Y") 
  2. Pass this params map to Jasper in the method 通过方法将此params映射传递给Jasper

    JasperFillManager.fillReport

  3. In your JRXML, create a parameter corresponding to each property set in the hashmap above: 在您的JRXML中,创建与上面的哈希图中的每个属性集相对应的参数:

    <parameter name="DISPLAY_COLUMN_ONE" class="java.lang.String"/>

  4. Display the columns based on the parameter value 根据参数值显示列

    <printWhenExpression><![CDATA[$P{DISPLAY_COLUMN_ONE}.equals("Y")]]></printWhenExpression>

you should pass columnCount parameter to jasper and check condition in each column's Print when Expression . 您应该将columnCount参数传递给jasper,并在Expression时检查每列的Print中的条件。

first column Print when Expression is 第一列当Expression为时打印

$P{columnCount} >=1

Second column Print when Expression is 第二列当Expression为时打印

$P{columnCount} >=2

Third column Print when Expression is 第三列当Expression为时打印

$P{columnCount} >=3

Fourth column Print when Expression is 第四列表达式为时打印

$P{columnCount} >=4

Hope this help you. 希望这对您有所帮助。

I would just make multiple copies of your jasper report, one for each variation of number of columns. 我只会为您的jasper报告制作多份副本,每列数量的变化都要一份。 It may be a lot of redundancy, but it's very easy as long as the number of possible variations isn't too high. 它可能有很多冗余,但是只要可能的变体的数量不太高,它就非常容易。 The report you end up calling depends on the user's selection. 您最终调用的报告取决于用户的选择。

I can't think of any way of varying the columns within a report based on a conditional. 我想不出任何根据条件改变报告中列的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM