Jasper Reports - check if table is empty (no records) and hide table label - jasper-reports

My scenario: 2 tables on Summary field, each table has its own dataset. Also, each table is labelled so there is a label just on top each table.All is ok, however if there is no data in table, table is not printed but table label is still visible.
I need to hide table label if table has no data. Is it even possible? I can't find any solution how to determine if table is empty or not, to set is as expression that will hide label.
Any ideas?

There is no problem to get the record count from your table component this is done by returning value
<returnValue fromVariable="REPORT_COUNT" toVariable="TABLE_COUNT"/>
However this will not solve you problem since you can not set the evalutationTime of the printWhenExpression, you can only set evalutationTime of the textFieldExpression
The problem is normally solved by moving your "table label" where it is expected to be, in the table header
From IDE group your columns and add the textField in the groupHeader inside the table header.
Example (jrxml result)
<componentElement>
<reportElement key="table" style="table" x="0" y="33" width="360" height="50" uuid="6a7d5ab9-f15d-4676-85b2-1e48f016c155"/>
<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="tableData" uuid="fa5df3de-f4c5-4bfc-8274-bd064e8b81e6">
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
</datasetRun>
<jr:columnGroup width="217" uuid="c96f6ab0-8cce-4dc7-b686-da6928e7cabb">
<jr:tableHeader height="61" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="217" height="61" uuid="98aa260d-7e84-4df7-89cc-fc1eabdcf656"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA["MY TABLE LABEL"]]></textFieldExpression>
</textField>
</jr:tableHeader>
<jr:column width="90" uuid="889a828a-ad48-40a0-81f4-326a95d6585c">
<jr:columnHeader style="table_CH" height="60" rowSpan="1"/>
<jr:detailCell style="table_TD" height="43" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="90" height="43" uuid="82b2a835-4373-4638-b75a-d43f531678ba"/>
<textFieldExpression><![CDATA[$F{myField1}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="127" uuid="004c3afe-c39f-44e2-a82d-5c96697ebd7e">
<jr:columnHeader style="table_CH" height="60" rowSpan="1"/>
<jr:detailCell style="table_TD" height="43" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="90" height="43" uuid="90287319-765f-49a0-9f80-0851f40f2b13"/>
<textFieldExpression><![CDATA[$F{myField2}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:columnGroup>
</jr:table>
</componentElement>

Related

Jasper report table can't fit on the current page

I have a Jasper report that is iterating over a list of data returned by a query and displaying the data in tabular format. Depending on the number of table rows the contents of the table might be split across two pages.
Is there a way to prevent this data splitting and cause the entire table to appear on the next page if it doesn't all fit on the current page?
The detail band of my report is as follows:
<detail>
<band height="100" splitType="Prevent">
<componentElement>
<reportElement x="0" y="0" width="800" height="20" isPrintWhenDetailOverflows="true" uuid="3e389075-14ca-4f57-9d02-175fbc34a639"/>
<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="output" uuid="acf7113d-1f5a-4acc-ad2c-70efb491bb4f">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRMapCollectionDataSource($F{output})]]></dataSourceExpression>
</datasetRun>
<jr:listContents height="60" width="800">
<textField>
<reportElement isPrintRepeatedValues="false" x="0" y="20" width="555" height="20" uuid="156e4ef8-b8d9-4dc4-8a50-4f27e0f36a83"/>
<textFieldExpression><![CDATA[$F{reference}]]></textFieldExpression>
</textField>
<componentElement>
<reportElement x="0" y="40" width="200" height="20" uuid="3e389075-14ca-4f57-9d02-175fbc34a639"/>
<jr:list printOrder="Vertical">
<datasetRun subDataset="unitDetails" uuid="acf7113d-1f5a-4acc-ad2c-70efb491bb4f">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRMapCollectionDataSource($F{units})]]></dataSourceExpression>
</datasetRun>
<jr:listContents height="40" width="800">
<textField>
<reportElement x="0" y="0" width="100" height="20" uuid="ead7d8ba-12e2-4a0e-af92-66a50b34148f"/>
<textFieldExpression><![CDATA[$F{businessUnit}]]></textFieldExpression>
</textField>
<componentElement>
<reportElement x="0" y="20" width="800" height="20" uuid="85e29233-08b3-4717-b260-82c9e4a003c5"/>
<jr:table>
<datasetRun subDataset="codeDetails" uuid="1f5dfa57-f961-4574-b663-8637b2aa6f6e">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRMapCollectionDataSource($F{codes})]]></dataSourceExpression>
</datasetRun>
<jr:column width="100" uuid="f832562c-1459-44a4-89df-fe5feadfe7ee">
<jr:detailCell height="20">
<textField>
<reportElement x="0" y="0" width="100" height="20" uuid="e2ce6b92-bf8a-45f8-936a-af99b2ced4e7"/>
<textElement textAlignment="Left"/>
<textFieldExpression><![CDATA[$F{code}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="150" uuid="b8c51d7d-f4d2-47c9-90a8-2449dff2a9f9">
<jr:columnHeader height="20">
<staticText>
<reportElement x="0" y="0" width="150" height="20" uuid="2fc57842-d68d-4398-a0df-4eddd74316fd"/>
<textElement textAlignment="Right"/>
<text><![CDATA[Total]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell height="20">
<textField>
<reportElement x="0" y="0" width="150" height="20" uuid="b2953321-ce5d-4e5f-94e3-8b48b49bbf90"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$F{total}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="150" uuid="f8e255a7-951d-43ad-962b-f4ee32efaa80">
<jr:columnHeader height="20">
<staticText>
<reportElement x="0" y="0" width="150" height="20" uuid="69deede1-0fbc-4fd0-b430-4f0f3cacdc4f"/>
<textElement textAlignment="Right"/>
<text><![CDATA[Approved]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell height="20">
<textField>
<reportElement x="0" y="0" width="150" height="20" uuid="200263e0-f9fb-4626-8cee-3eb838d51add"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$F{approved}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="150" uuid="b4d8feb1-4134-4302-bad1-902685238d3e">
<jr:columnHeader height="20">
<staticText>
<reportElement x="0" y="0" width="150" height="20" uuid="096581a7-beec-43bc-a17e-ce96c4384838"/>
<textElement textAlignment="Right"/>
<text><![CDATA[Rejected]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell height="20">
<textField>
<reportElement x="0" y="0" width="150" height="20" uuid="3c5d019f-c4af-479b-b180-d52a6606d4e0"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$F{rejected}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</jr:listContents>
</jr:list>
</componentElement>
</jr:listContents>
</jr:list>
</componentElement>
</band>
</detail>
The report output looks like the following where the bottom of the page has the table headings but not the contents because the contents don't fit.

How to set table column footer to the bottom of the detail band?

I'm using table in jasperreport and I need to add table column footer to the bottom just before "group footer" band like
But currently it generates table like here
as you can see a lot of spaces between the table (which in detail band) and group footer band..
<detail>
<band height="80">
<componentElement>
<reportElement key="table1" style="tableStyle" x="1" y="0" width="552" height="80" isPrintWhenDetailOverflows="true" uuid="9a24d27b-9228-4f23-8c02-628268479882">
<printWhenExpression><![CDATA[$V{REPORT_COUNT} == 1]]></printWhenExpression>
</reportElement>
<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" whenNoDataType="AllSectionsNoDetail">
<datasetRun subDataset="TableDataset" uuid="aad66998-86ba-485c-b8f4-3f2f6eda4796">
<dataSourceExpression><![CDATA[$P{datasourceItems}]]></dataSourceExpression>
</datasetRun>
<jr:column width="25" uuid="eb2f74b3-8006-4762-a52e-838f5e08a86f">
<jr:columnHeader height="40" rowSpan="2">
<staticText>
<reportElement style="tableProduct_CH" x="0" y="0" width="25" height="40" uuid="6ce8147d-c282-467a-b348-0d5cf3def735"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[No]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter style="tableProductFooterNoLine" height="20" rowSpan="1"/>
<jr:detailCell height="20" rowSpan="1">
<textField isStretchWithOverflow="true">
<reportElement style="tableProduct_TD" x="0" y="0" width="25" height="20" uuid="fc581740-46a3-4602-b769-18beca53d355"/>
<textElement textAlignment="Center"/>
<textFieldExpression><![CDATA[$F{no}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column> ...
</jr:table>
</componentElement>
</band>
</detail>
AFIK: This is not possible in jasper reports version <=6.7 (current version at date of answer) using the jr:table component.
You could draw the lines in the background band and then add the footer for example in a groupFooter band, but I would instead suggest that you create a subreport to avoid problems with page break.
The subreport will mimic the table using the standard detail and columnFooter band, again you will be adding the vertical lines in the background band.
How the columnFooter will behave is define by the attribute isFloatColumnFooter hence in your case false

How to identify the end of page while rendering the table?

I am new to jasper world.
I want to render table on left and right side of page like...
Item Name Item Price Item Name Item Price
Item1 20 Item11 40
Item2 30 Item12 10
Item3 40 Item13 30
. .
. .
Item10 30 Item20 30
<Here Page ends> <Here Page ends Moved to next page>
I have created below simple code using Jaspersoft to render the table and its rendering properly, but I want to add Continue.. text at the place of <Here Page ends> and <Here Page ends Moved to next page>.
<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="ItemDataSet" uuid="e335b742-7913-4832-93bc-8f211fc4c73f">
<dataSourceExpression><![CDATA[$P{ItemDataSource}]]></dataSourceExpression>
</datasetRun>
<jr:column width="90" uuid="d5eb0cc8-2ae8-43fd-acfe-209e1948adef">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column1"/>
<jr:columnHeader style="Table_TH" height="30" rowSpan="1">
<staticText>
<reportElement x="0" y="0" width="90" height="30" uuid="1e5654a6-312f-4974-a84a-02d99741e6d5"/>
<text><![CDATA[Item Name]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="90" height="30" uuid="e0af50c5-c6d2-4d10-8a03-656dd20a790a"/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="100" uuid="1f5a81e4-5c5f-4044-a08b-659ffb3e4311">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column2"/>
<jr:columnHeader style="Table_TH" height="30" rowSpan="1">
<staticText>
<reportElement x="0" y="0" width="100" height="30" uuid="be3258e1-1a01-4c19-818f-395a77b8c9d0"/>
<text><![CDATA[Item Price]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="100" height="30" uuid="ef399751-bba2-49d7-99d9-84bc93a1e50d"/>
<textFieldExpression><![CDATA[$F{price}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
Thanks :)

How to use table component and pass parameters(Map) or variables as datasource to the table

I have created the table using the table component in ireport 4.5.0.I want to pass data through map in my code.So i gave the datasource through Dataset Run.
In that i have tried all the way by giving 'Connection/Datasource exp','Parameters' and 'Parameters map exp',but i didnt get the table.
I am getting just a line not even the empty cell.I created the map parameter in subdataset also and also in table component,i can't guess what step i missed..
subDataset
<subDataset name="Table Dataset 2">
<parameter name="Details" class="java.util.Map"/>
</subDataset>
Table Component
<componentElement>
<reportElement key="table 1" style="table 1" x="0" y="0" width="555" height="230"/>
<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="Table Dataset 2">
<datasetParameter name="Details">
<datasetParameterExpression><![CDATA[$P{Details}.get("Name")]]></datasetParameterExpression>
</datasetParameter>
</datasetRun>
<jr:column width="25">
<jr:detailCell style="table 1_TD" height="44" rowSpan="1">
<staticText>
<reportElement x="0" y="0" width="25" height="44"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[1]]></text>
</staticText>
</jr:detailCell>
</jr:column>
<jr:column width="255">
<jr:detailCell style="table 1_TD" height="44" rowSpan="1">
<staticText>
<reportElement x="0" y="0" width="255" height="44"/>
<box leftPadding="5"/>
<textElement verticalAlignment="Middle">
<font isStrikeThrough="false"/>
</textElement>
<text><![CDATA[Name]]></text>
</staticText>
</jr:detailCell>
</jr:column>
<jr:column width="275">
<jr:detailCell style="table 1_TD" height="44" rowSpan="1">
<textField isStretchWithOverflow="true">
<reportElement x="0" y="0" width="275" height="44"/>
<box leftPadding="5"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$P{Details}.get("Name")]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
I also tried the dataset Run as
<datasetRun subDataset="Table Dataset 2">
<parametersMapExpression><![CDATA[$P{Details}.get( "Name" )]]></parametersMapExpression>
</datasetRun>
and
<datasetRun subDataset="Table Dataset 2">
<parametersMapExpression><![CDATA[$P{Details}]]></parametersMapExpression>
</datasetRun>
and
<datasetRun subDataset="Table Dataset 2">
<parametersMapExpression><![CDATA[$P{REPORT_PARAMETERS_MAP}]]></parametersMapExpression>
</datasetRun>
But i got only a single line as output,How to pass the map to the table,what mistake i made?Help me pls.Thanks..

New lines in server generated report text field but not in iReport

I have a table in iReport which generates to a PDF correctly:
<componentElement>
<reportElement key="server vendor table" style="table" x="272" y="437" width="150" height="115"/>
<jr:table>
<jr:column width="50">
<jr:columnHeader style="table_CH" height="15" rowSpan="1">
<staticText>
<reportElement x="5" y="0" width="45" height="15"/>
<textElement/>
<text><![CDATA[Server Vendor]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="table_TD" height="18" rowSpan="1">
<textField>
<reportElement x="5" y="0" width="45" height="15"/>
<textElement/>
<textFieldExpression><![CDATA[$F{vendor}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="50">
<jr:columnHeader style="table_CH" height="15" rowSpan="1">
<staticText>
<reportElement x="5" y="0" width="45" height="15"/>
<textElement/>
<text><![CDATA[# Hosts]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="table_TD" height="18" rowSpan="1">
<textField>
<reportElement x="5" y="0" width="45" height="15"/>
<textElement/>
<textFieldExpression><![CDATA[$F{count}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="50">
<jr:columnHeader style="table_CH" height="15" rowSpan="1">
<staticText>
<reportElement x="5" y="0" width="45" height="15"/>
<textElement/>
<text><![CDATA[Host %]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="table_TD" height="18" rowSpan="1">
<textField>
<reportElement x="5" y="0" width="45" height="15"/>
<textElement/>
<textFieldExpression><![CDATA[$F{pc}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
When generating the report from iReport, the PDF is generated correctly, the result is:
But when generated using Java the text entry for "HP" in the first column is split across multiple lines:
Note that only the "HP" text is split - longer text with a space in it (e.g. "Dell inc.") is not split.
I'm guessing that I need to set some properties, but I can't see figure out which property I need to set.
Class.forName("org.postgresql.Driver");
Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/hardwaredata","???","???");
InputStream input = new FileInputStream(new File("/reports/hardware_report.jrxml"));
JasperDesign design = JRXmlLoader.load(input);
input.close();
Map<String, Object> param_map = new HashMap<String, Object>();
JasperReport jReport = JasperCompileManager.compileReport(design);
JasperPrint jPrint = JasperFillManager.fillReport(jReport, param_map, conn);
String path = application.getRealPath("/") + "/"+"static_report.pdf";
JasperExportManager.exportReportToPdfFile(jPrint, path);
I have seen this before. I don't recall finding the true cause. Equally strangely (but happily) I found that strings like "HP " don't exhibit this behavior.
Try changing your text field to display this: $F{Vendor} + " "
Be sure to also log a bug! But that might be good enough to solve immediate problems.
In the properties for the text that is aligning vertically check-mark the field titled "Stretch with overflow"