Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

jasper reports - How do I print a list of strings contained within another list in iReport?

I am creating a simple reporting program using java and iReport (from jasper), which is supposed to create a report in pdf showing PCs with their IP address, their location, whether it's idle or not at the moment (handled by another system), and a list of projects it is currently attached to (managed elsewhere, too).

I'm using iReport for this matter, and have created a dummy collection generating class as follows:

public class PCReports {

    public static java.util.Collection PC_collection;
    public static java.util.Collection generateCollection() {

        PC_collection = new ArrayList<PCLineDTO>();
        PCLineDTO line = new PCLineDTO();
        line.setIP("192.168.1.1");
        line.setLab("location");
        line.setActive(true);
        line.addProjectName("project1");
        line.addProjectName("project2");
        line.addProjectName("project3");
        PC_collection.add(line);

        line = new PCLineDTO();
        line.setIp("192.168.1.2");
        line.setLab("location2");
        line.setActive(false);
        line.addProjectName("project1");
        line.addProjectName("project2");
        PC_collection.add(line);

        return PC_collection;
    }
}

The entity class in this case being:

public class PCLineDTO {
    private String ip;
    private String lab;
    private Boolean active;
    private ArrayList<String> projects;
}

After some searching around the Internet, I found a way to do something similar, using subreports.

The thing is, I don't know how to print a collection of strings passed as a dataSource to this subreport.

In the examples I found on the Internet, for each item in the master collection, the subreports were passed a collection of objects -with their own getter methods for each attribute- instead of a collection of strings as is the case here. In those cases, they accessed the values they needed to use via the iReport syntax, which I was not able to use, for example:

$F{project}

Since iReport looks for a getProject method contained within the objects it receives, but in this case it's a simple String object (without a getProject method, as it were).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use a subreport or a subdataset.

Pass the subreport a collection datasource

JRBeanCollectionDataSource($F{Projects})

Then in the new subreport create a new field called "_THIS" exactly, this means the bean in the collection passed is the same as the value i want

For more info, check the source code of the class here: JRAbstractBeanDataSource

Note: this is available in JasperReport 3.0.0 im not sure if it exists in previous builds. Hope this helps

Update: just checked the SVN, seems like this feature is implemented in JasperReports 2.0.0


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...