Jump to content
We've recently updated our Privacy Statement, available here ×

Count of elements in the list


nkunkov

Recommended Posts

Hello,

I have a list element as part of the report and i'm required to display a size of the list before it's printed.  For example, if my list has 5 rows, i'd like to print "the list has 5 rows" before printing the actual list values.

I tried using REPORT_COUNT variable, but it prints a row number for each row while i just need the final count printed once.  Changing evaluation time didn't help much.  I need a final or max count and i need to print it only once.  Is this possible?

Any help would be appreciated.  Thanks

NK

 

Link to comment
Share on other sites

  • Replies 4
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

this can be achieved with

  1. first variable to count the records in list dataset
  2. second variable to get the highest from the first variable
  3. return value from second list variable to a variable in the main dataset
  4. display the return value on the main report

Here is a working sample.

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.2.0.final using JasperReports Library version 6.2.0  -->
<!-- 2016-12-14T13:52:12 -->
<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="list" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="d9bbb141-8dfb-40aa-b09d-81140ea6a430">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="PRODSUP"/>
    <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
    <subDataset name="list" uuid="216b8ad6-195c-44d2-96e6-5c750d85f459">
        <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
        <queryString>
            <![CDATA[select 1 as ID from dual
union
select 2 as ID  from dual
union
select 3 as ID from dual]]>
        </queryString>
        <field name="ID" class="java.math.BigDecimal"/>
        <variable name="Count" class="java.lang.Integer" calculation="Count">
            <variableExpression><![CDATA[$F{ID}]]></variableExpression>
        </variable>
        <variable name="max_count" class="java.lang.Integer" calculation="Highest">
            <variableExpression><![CDATA[$V{Count}]]></variableExpression>
        </variable>
    </subDataset>
    <queryString>
        <![CDATA[select * from dual]]>
    </queryString>
    <field name="DUMMY" class="java.lang.String"/>
    <variable name="Variable_1" class="java.lang.Integer" resetType="None"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <detail>
        <band height="30" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="100" height="10" uuid="44f9fd26-6561-4926-a0af-b4f8f55152dd"/>
                <textFieldExpression><![CDATA[$F{DUMMY}]]></textFieldExpression>
            </textField>
            <textField evaluationTime="Report">
                <reportElement x="180" y="0" width="200" height="30" uuid="7d866c3c-1171-4b8f-b447-0a23b7660d01"/>
                <textElement>
                    <font isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA["the list has " + $V{Variable_1} + " rows"]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <summary>
        <band height="20" splitType="Stretch">
            <componentElement>
                <reportElement x="180" y="0" width="200" height="20" uuid="2ebeeaba-1db8-4106-9444-59a926860a4f">
                    <property name="net.sf.jasperreports.export.headertoolbar.table.name" value=""/>
                </reportElement>
                <jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
                    <datasetRun subDataset="list" uuid="4dd410cc-f8bf-447a-9014-8643d98c0233">
                        <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                        <returnValue fromVariable="Count" toVariable="Variable_1"/>
                    </datasetRun>
                    <jr:listContents height="20" width="200">
                        <textField>
                            <reportElement x="0" y="0" width="100" height="20" uuid="fd84641d-4739-4134-b146-b2c664021a6d"/>
                            <textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
                        </textField>
                    </jr:listContents>
                </jr:list>
            </componentElement>
        </band>
    </summary>
</jasperReport>
 

Link to comment
Share on other sites

Thanks so much for your reply.  I did exactly as you recommended but i'm not getting a correct max_count.  It just always returns 1 even though it is set to be the highest of the count variable.  I have reset type set to "report" for all 3 variables, but it doesn't seem like the highest works or i'm still doing something wrong.  I'm expecting 2 to be the max_count in this case, but it seems that it gets evaluated and returned when it's still 1.

Any ideas?

Link to comment
Share on other sites

Yes, i was able to get this to work BUT i have a more complicated scenario...  I have repeating objects being printed in the detail band.  For each row i have a list of objects for which i want a count printed.  For example, below is what i'd like to see:

Row A

              list of object for Row A, count is 3

             object 1

             object 2

             object 3

Row B

             list of object for Row B, count is 2

            object 1

            object 2

Following your example i can get a count but only of the last list since the evaluation happens at Report time.  So for both Row A and Row B the count will be 2.  Instead of 3 for Row A and 2 for Row B.  

Is there a way to do what I require?  I have a feeling i need to create a group on the Row ID and make the evaluation time - Group.  But when i create a group i don't see the new group in the drow down of "Evaluation Time".  Is there a trick to properly adding group that would appear in the dropdown?  Is the group a right way to go?

Thank you!

N.K.

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...