如何显示群组的数量? [英] How to show the number of Group?

查看:73
本文介绍了如何显示群组的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个报告来生成具有部门列表的用户。我需要计算组,但不是组内的记录。

I created a report to generate user which has a list of departments. I need to count the group but not the records within the group.

如何使用 JasperReports 进行操作?

例如: -

**No: 1**
Name      : User1
Department: Depart1
            Depart2
            Depart3

**No: 2**
Name      : User2
Department: Depart1
            Depart2
            Depart3

**No: 3**
Name      : User3
Department: Depart1
            Depart2
            Depart3

在上面的例子中,我需要计算3个用户的计数1,2,3。我不希望编号计算用户内的记录,这意味着我不需要计数来计算每个用户的部门数量。

In the above example, I need to count the count them 1, 2, 3 for 3 users. I don't want the numbering to count the records within the user, means I do not need the count to count the numbers of department for each user.

目前,我为用户创建了一个组,并且自动创建了一个变量User_COUNT。我在我的细节乐队中使用它,但它似乎计算用户组中的每个记录。

Currently, I create a group for the User and there is a variable automatically created "User_COUNT". I use that in my detail band but it seems counting each records in the user group.

推荐答案

您可以在您的群组中使用该变量。

You can use the variable on your Group.

示例:

<?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="count_groups" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="7ca8f323-3cc7-4980-9222-77fb0f8f556b">
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="Name" class="java.lang.String"/>
    <field name="Department" class="java.lang.String"/>
    <variable name="cntUser" class="java.lang.Integer" incrementType="Group" incrementGroup="userGroup">
        <variableExpression><![CDATA[($V{userGroup_COUNT} == 1) ? $V{cntUser} + 1 : $V{cntUser}]]></variableExpression>
        <initialValueExpression><![CDATA[1]]></initialValueExpression>
    </variable>
    <group name="userGroup">
        <groupExpression><![CDATA[$F{Name}]]></groupExpression>
        <groupHeader>
            <band height="50">
                <textField>
                    <reportElement uuid="da974bc0-323d-4169-b584-eddb4ffcfa50" x="0" y="0" width="100" height="20"/>
                    <textElement/>
                    <textFieldExpression><![CDATA["No: " + $V{cntUser}]]></textFieldExpression>
                </textField>
                <staticText>
                    <reportElement uuid="57fcf335-26f2-44b4-89ad-11c1223c9539" x="0" y="30" width="100" height="20"/>
                    <textElement markup="none"/>
                    <text><![CDATA[Name         :]]></text>
                </staticText>
                <textField>
                    <reportElement uuid="16ccb7f0-eb39-403e-b7c4-1c6f35989f3d" x="100" y="30" width="100" height="20"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$F{Name}]]></textFieldExpression>
                </textField>
            </band>
        </groupHeader>
    </group>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement uuid="4647a9aa-229e-4f5d-8d08-aca4cda1df2f" x="100" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{Department}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement uuid="bc4bd9ae-6c2f-43c2-823e-1501b76ef39a" x="0" y="0" width="100" height="20">
                    <printWhenExpression><![CDATA[$V{userGroup_COUNT} == 1]]></printWhenExpression>
                </reportElement>
                <textElement/>
                <text><![CDATA[Department:]]></text>
            </staticText>
        </band>
    </detail>
</jasperReport>

iReport 中的报告设计如下所示:

The report design in iReport is looks like this:

对于此输入数据(我在样本中使用了 CSV 数据源):

For this input data (I've used CSV datasource in sample):

Name,Department
User1,Depart1
User1,Depart2
User1,Depart3
User2,Depart2
User2,Depart3
User3,Depart1
User3,Depart4
User3,Depart4
User3,Depart4

结果将是(通过 iReport 预览):

The result will be (via preview in iReport):

详细信息

在此示例中,我使用了变量 cntUser 增量类型等于 群组 (对于名为 userGroup <的群组/ em>的)。此变量仅针对每个中的第一条记录递增(请参阅 variableExpression )(检查
$ V {userGroup_COUNT } == 1 )。

Details:
In this sample I've used variable cntUser with Increment Type equals to Group (for group named userGroup). This variable incremented (see the variableExpression) only for the first record in each Group (the check $V{userGroup_COUNT} == 1).

注意:如果你不要忘记排序数据正在使用分组

Note: Don't forget to sort data if you are using the Grouping

这篇关于如何显示群组的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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