How to show JRBeanCollectionDataSource data with help of Table component?
1.Send your datasource from the server as a parameter, and fill the report with a different one (can be empty).
JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataList);
Map parameters = new HashMap();
parameters.put("INFO", "Hello");
parameters.put("DS1", beanColDataSource);
JasperReport report = (JasperReport) JRLoader.loadObject("src/test/ireport/ShowPerson.jasper");
JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, new JREmptyDataSource());
2.Decalre a new parameter in the report of type JRBeanCollectionDataSource
<parameter name="DS1" class="net.sf.jasperreports.engine.JRBeanCollectionDataSource"/>
3.Set TableDatasource to use the $P{DS1} parameter.
<jr:table ...>
<datasetRun subDataset="Table Dataset 1">
<datasetParameter name="REPORT_DATA_SOURCE">
<datasetParameterExpression><![CDATA[$P{DS1}]]></datasetParameterExpression>
</datasetParameter>
</datasetRun>
.....
4.Declare the fields (name, age) in Table Dataset 1.
5.In the table, in the DetailBand add a TextField in each column with the corresponding field ($F{name} and $F{age} respectively).
JRBeanCollectionDataSource: How to show data from the java.util.List from JavaBean?
Here is the working sample.
The key points of this sample:
- using of the
_THIS
expression; - using List (jr:list) component in Detail band
The snippet of code for generating report:
public static void testBuildPdf() {
try {
Map<String, Object> params = new HashMap<String, Object>();
JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, getDataSource());
JasperExportManager.exportReportToPdfFile(jasperPrint, outputFileName);
} catch (Exception e) {
e.printStackTrace();
}
}
private static JRDataSource getDataSource() {
Collection<BeanWithList> coll = new ArrayList<BeanWithList>();
coll.add(new BeanWithList(Arrays.asList("London", "Paris"), 1));
coll.add(new BeanWithList(Arrays.asList("London", "Madrid", "Moscow"), 2));
coll.add(new BeanWithList(Arrays.asList("Rome"), 3));
return new JRBeanCollectionDataSource(coll);
}
The JavaBean code:
public class BeanWithList {
// The member's name can be any. The JR engine is using public getter for extracting field's value
private List<String> cities;
private Integer id;
public BeanWithList(List<String> cities, Integer id) {
this.cities = cities;
this.id = id;
}
// getter should be public
public List<String> getCities() {
return this.cities;
}
public Integer getId() {
return this.id;
}
}
The jrxml file:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ...>
<subDataset name="dataset1">
<field name="city" class="java.lang.String">
<fieldDescription><![CDATA[_THIS]]></fieldDescription>
</field>
</subDataset>
<field name="id" class="java.lang.Integer"/>
<field name="cities" class="java.util.Collection"/>
<title>
<band height="103" splitType="Stretch">
<staticText>
<reportElement x="138" y="28" width="258" height="20"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Bean with List sample]]></text>
</staticText>
</band>
</title>
<columnHeader>
<band height="20">
<staticText>
<reportElement x="0" y="0" width="100" height="20"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true" isUnderline="false"/>
</textElement>
<text><![CDATA[Id]]></text>
</staticText>
<staticText>
<reportElement x="100" y="0" width="100" height="20"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true" isUnderline="false"/>
</textElement>
<text><![CDATA[City name]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement stretchType="RelativeToTallestObject" x="0" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
</textField>
<componentElement>
<reportElement x="100" y="0" width="400" height="20"/>
<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="dataset1">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{cities})]]></dataSourceExpression>
</datasetRun>
<jr:listContents height="20" width="400">
<textField>
<reportElement x="0" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{city}]]></textFieldExpression>
</textField>
</jr:listContents>
</jr:list>
</componentElement>
</band>
</detail>
</jasperReport>
The result will be:
Other related questions are the How do I print a list of strings contained within another list in iReport? question and Passing the List of primitive type objects as datasource for subreport question.
In iReport 5.6.0 : Why is the first record missing from my table element when i use JRBeanCollectionDataSource?
This usually happens when people use the same dataSource twice in their reports:
- once for the main dataSet, when filling the report, and
- once again for a subDataSet-based component or a subReport placed in the detail band
Your main dataSet has already consumed the 1st record when the table component reuses the same dataSource.
In your case, you could easily fix this by:
using the so-called empty dataSource when filling:
jasperPrint = JasperFillManager.fillReport(reportTemplate, parameters, new JREmptyDataSource());
or by using a clone when passing it as a parameter:
parameters.put("transactionResponse", dataSource.cloneDataSource());
How to add collection of java bean in table in jaspersoft report
Your java bean needs to be something like this (correspond to your subDataset
named javabean).
public class JavaBean {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Then you need a List<JavaBean>
of the JavaBean's.
Lets imagine that you pass this List
as a parameter during the fill process (in the parameter map with the key listOfJavaBeans
).
in jrxml
define the parameter
<parameter name="listOfJavaBeans" class="java.util.List"/>
To pass it to your table as datasource use.
<datasetRun subDataset="javabean" uuid="31830583-c665-4c8d-af3a-1a5a0aba5306">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{listOfJavaBeans})]]></dataSourceExpression>
</datasetRun>
Table tool in showing one fewer/lesser database records
It is known feature (or bug?) of JR.
To solve this problem pass collection (not JRDataSource!) as parameter and use this collection in dataSource for table.
For example:
parameters.put("beanCollection", beanCollection);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,
parameters, new JREmptyDataSource());
In jrxml define parameter "beanCollection" as java.util.Collection
and define DataSource expression for table in jrxml as
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{beanCollection})
Table is not visible in Jasper Report Java bean
After following this post solved my problem. I forgot to pass datasource at jr:table component.
Correction made in jrxml:
<subDataset name="Dataset1">
<queryString>
<![CDATA[]]>
</queryString>
<field name="part" class="java.lang.String">
<fieldDescription><![CDATA[part]]></fieldDescription>
</field>
<field name="length" class="java.lang.Double">
<fieldDescription><![CDATA[length]]></fieldDescription>
</field>
<field name="width" class="java.lang.Double">
<fieldDescription><![CDATA[width]]></fieldDescription>
</field>
</subDataset>
Add data source expression in jr:table tag like this:
<datasetRun subDataset="Dataset1">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{parts})]]></dataSourceExpression>
</datasetRun>
This worked for me.
Using the Table feature in iReports and Jasper and java bean data sources
The package name you mentioned in the jrxml is wrong. It is net.sf.jasperreports.engine.data.JRBeanCollectionDataSource.
Related Topics
Difference Between Double and Double in Comparison
Iterating Through a List in Reverse Order in Java
What's the Difference Between Instance Method Reference Types in Java 8
Difference Between Oracle Jdbc Driver Classes
How to Convert from Cmyk to Rgb in Java Correctly
Performance Difference Between a Wild Card Import and the Required Class Import
What Is the Regex for "Any Positive Integer, Excluding 0"
Why Isn't a Qualified Static Final Variable Allowed in a Static Initialization Block
When to Use Atomicreference in Java
How to Quit a Java App from Within the Program
Exception in Initializer Error in Java When Using Netbeans
Spring Data: Override Save Method
Httpservletrequest - Setcharacterencoding Seems to Do Nothing
Problems with Newline in Graphics2D.Drawstring
Hibernate:How Override an Attribute from Mapped Super Class
Jackson - Serialization of Entities with Birectional Relationships (Avoiding Cycles)
Why Isn't This Code Causing a Concurrentmodificationexception