Dynamic column generation

0
Hello,

Is there anyone who has idea how to generate columns dynamically? Means if result set has 10 records/columns than report will create 10 columns. If result set has 5 OR 7 records/columns then it will generate 7 columns.

In other way if record returns 7 OR 10 columns then it will generate columns(7 or 10) accordingly for that perticular record.

In short number of column is not fixed...

Reply as soon as possible....

Thanks in advance :)
Badrish.
HBK32's picture
41
Joined: May 23 2007 - 5:59am
Last seen: 12 years 6 months ago

17 Answers:

1
hi there, i got the same probs. what i know that it is possible to generate dynamic columns using crosstab element of jasper reports. but i dont know how to use it. instead i used a templating language callde 'velocity' and i used it to generate the jrxml file dynamically and it works. the main proc is you have to genereate/write a static jrxml and then put some velocity on it. then render the jrxml usign velocity complier. you can do it using java code. the next procs are as usual for jasper. but i want to use crosstab. so if you got any tutorial/sample on that dont forget to post it here.


thanks.
tfr
tfr's picture
tfr
13
Joined: Aug 31 2007 - 6:42pm
Last seen: 12 years 2 months ago
0
Hi,

I am also needing to implement dynamic columns within a Jasper Report. I was wondering if you could post an example of how you implemented Velocity to solve this problem.
Thanks

James
jimiohara's picture
Joined: Feb 28 2008 - 10:40am
Last seen: 11 years 8 months ago
0
You can use velocity template engine to generate reports dynamically.......
sudhagark's picture
Joined: Mar 4 2008 - 9:03pm
Last seen: 11 years 8 months ago
0
Or you can use the JasperReports Design API and get a list of the column names from your application, iterate through them and create the fields, column header and detail elements before you compile the report.

I used a CSV datasource, which can be just a string with the data delimited by a comma and "/r/n" character for new lines, and set the flag useFirstLineAsColumnHeader to be true. This uses the first line as the column names, so be sure the field names match your datasource column headers, or else there will be errors.

Hope this helps. I did have a problem with column headers with large names being cut off, and I havent tried having more columns than can fit on a page yet. If anyone has please let me know how it went.

Cheers,
Jim
jimiohara's picture
Joined: Feb 28 2008 - 10:40am
Last seen: 11 years 8 months ago
0
Hello tfr,
From your post i come to know that you have used velocity to generate dynamic columns in jrxml.
Can you post your sample code of it?
As i have to generate reports with dynamic columns.Also i have search on google about velocity and jaspers.But failed in getting proper way for reports.
Can you post your sample code with bit explanation so that i can get some guidance from it.
Or can you suggest me that how i can combine velocity and jrxml so that i can create dynamic reports from it.

Thanks in advance,

by,
thevoice.
thevoice's picture
Joined: Nov 4 2007 - 3:36pm
Last seen: 12 years 2 weeks ago
0
For dynamic columns reports, using JasperReports, best option is to use [url=http://dynamicjasper.sourceforge.net/]DynamicJasper

You can define columns in runtime using java.

[url=http://dynamicjasper.sourceforge.net/docs/getting_started.html]Documenta... bast enough, also many [url=http://dynamicjasper.sourceforge.net/docs/howto.html]examples there.

Here you have an example

Code:
<br />
      FastReportBuilder drb = new FastReportBuilder();<br />
        DynamicReport dr = drb.addColumn("State", "state", String.class.getName(),30)<br />
                .addColumn("Branch", "branch", String.class.getName(),30) // title, property to show, class of the property, width<br />
                .addColumn("Product Line", "productLine", String.class.getName(),50)<br />
                .addColumn("Item", "item", String.class.getName(),50)<br />
                .addColumn("Item Code", "id", Long.class.getName(),20)<br />
                .addColumn("Quantity", "quantity", Long.class.getName(),30)<br />
                .addColumn("Amount", "amount", Float.class.getName(),30)<br />
                .addGroups(2)   // Group by the first two columns<br />
                .addTitle("November 2006 sales report"«»)<br />
                .addSubtitle("This report was generateed at" + new Date())<br />
                .addUseFullPageWidth(true) //make colums to fill the page width<br />
                .build();       <br />
        <br />
        JRDataSource ds = new JRBeanCollectionDataSource(TestRepositoryProducts.getDummyCollection());   <br />
        JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), ds);<br />
        JasperViewer.viewReport(jp);    //finally display the report report   </td></tr></tbody></table>  <br />
<br />
and this is the results<br />
<br />
 size=404]<img src="http://dynamicjasper.sourceforge.net/images/examples/group-default-style.jpg" width="Array"></img><br />
<br />
Hope it helps<br />
bye bye<br>Post edited by: djmamana, at: 2008/06/28 07:00
djmamana's picture
Joined: Jul 19 2007 - 1:47pm
Last seen: 12 years 4 months ago
0

 So did anyone managed to integrate it into iReport? I am trying to create a custom component that will be a dynamic table basically. I am not sure if Dynamic Jasper is my best choice. I am just a newbie...

kbukina's picture
Joined: Jan 21 2011 - 6:20am
Last seen: 8 years 10 months ago
0

It's possible to generate a report with dynamic columns using the CrossTab element!

As a simple example, check the attachments. They are written in Portuguese, so for your understanding, the translations are:

linha = row

coluna = column

valor = value

Important to say I provide data to the report using a JRBeanCollectionDataSource.

ezmarques's picture
Joined: Jun 1 2010 - 7:28am
Last seen: 9 years 5 months ago

Dude can you send me the example ??

inacioboueres - 6 years 9 months ago
0

and what happen if you want to export an excel, is that posible??? because you write Row number, in your crossTabObject.

mad1980's picture
Joined: Aug 25 2011 - 3:19am
Last seen: 8 years 2 months ago
0

All the proposed methods sounds good, I would even skip the need to use DynamicJasper, but just because I know well the JasperReports APIs.

Whatever is your trick (dynamic change of the JasperDesign with JR or DynamicJasper or a special data source to fill a crosstab (which by default has a dynamic number of columns), the result can be exported in any format without problems.

A more "recent" approach whould be to play with a table component, instead of placing dynamic elements in the detail band... even easier.

 

Giulio

giulio's picture
27406
Joined: Jan 2 2007 - 4:15pm
Last seen: 9 months 1 week ago

I understand tables do not support dynamic columns. Since you are "staff" would you kindly provide a "simple" example of doing so with crosstabs ? Thanks.

m.gelbana - 6 years 5 months ago
0

How would you add page numbering to the table component so that the page number does not appear on the last page?

reuben's picture
1
Joined: Aug 25 2011 - 5:41am
Last seen: 8 years 2 months ago
0

and what happen if you only want to show the column???? i mean i don't want to show the row. because the excel file has the row number.

 

any idea??? or any web to get the idea????

mad1980's picture
Joined: Aug 25 2011 - 3:19am
Last seen: 8 years 2 months ago
0

hey hiii

i think u have done dynamic column generation.

i want to do this.

can u plz send me an example...??

so that i can learn from it.. 

gvjoshi25's picture
Joined: Dec 23 2011 - 7:42am
Last seen: 2 years 7 months ago
0

 look for the message that ezmarques  posted, there a couple of file, with this files you can make dynamic columns. 

 

what i couldn't get, it's how to change the width of a column dynamically.

 

if you get, please let me know.

 

bye

 

mad1980's picture
Joined: Aug 25 2011 - 3:19am
Last seen: 8 years 2 months ago
0

 Hi  djmamana  ,

 

I am using DynamicJasper-3.0.13 and jasperreports-3.5.1 . i 've written your code in my application 

FastReportBuilder drb = new FastReportBuilder();<br />
        DynamicReport dr = drb.addColumn("State", "state", String.class.getName(),30)<br />
                .addColumn("Branch", "branch", String.class.getName(),30) // title, property to show, class of the property, width<br />
                .addColumn("Product Line", "productLine", String.class.getName(),50)<br />
                .addColumn("Item", "item", String.class.getName(),50)<br />
                .addColumn("Item Code", "id", Long.class.getName(),20)<br />
                .addColumn("Quantity", "quantity", Long.class.getName(),30)<br />
                .addColumn("Amount", "amount", Float.class.getName(),30)<br />
                .addGroups(2)   // Group by the first two columns<br />
                .addTitle("November 2006 sales report"«»)<br />
                .addSubtitle("This report was generateed at" + new Date())<br />
                .addUseFullPageWidth(true) //make colums to fill the page width<br />
                .build();       <br />
        <br />
        JRDataSource ds = new JRBeanCollectionDataSource(TestRepositoryProducts.getDummyCollection());   <br />
        JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), ds);<br />
        JasperViewer.viewReport(jp);    //finally display the report report

and i got this error

I wrote other codes with dynamicreports but the same error persist !!!!! i can't found out a solution in the net .

any suggestions ?!!!!!!! thanks in advance

Code:
2012-05-08 10:50:01,578 INFO  [ar.com.fdvs.dj.core.DynamicJasperHelper] (http-127.0.0.1-8080-2) generating JasperPrint
 
2012-05-08 10:50:01,578 INFO  [ar.com.fdvs.dj.core.DynamicJasperHelper] (http-127.0.0.1-8080-2) generating JasperReport
 
2012-05-08 10:50:01,578 INFO  [ar.com.fdvs.dj.core.DJJRDesignHelper] (http-127.0.0.1-8080-2) obtaining DynamicJasperDesign instance
 
2012-05-08 10:50:01,578 WARN  [ar.com.fdvs.dj.core.DynamicJasperHelper] (http-127.0.0.1-8080-2) Can't find bundle for base name dj-messages, locale it_IT, usign default (dj-messages)
 
2012-05-08 10:50:01,859 SEVERE [javax.enterprise.resource.webcontainer.jsf.application] (http-127.0.0.1-8080-2) java.lang.reflect.InvocationTargetException
 
javax.faces.el.EvaluationException: java.lang.reflect.InvocationTargetException
 
	at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
 
	at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
 
	at javax.faces.component.UICommand.broadcast(UICommand.java:387)
 
	at org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:321)
 
	at org.ajax4jsf.component.AjaxViewRoot.broadcastEvents(AjaxViewRoot.java:296)
 
	at org.ajax4jsf.component.AjaxViewRoot.processPhase(AjaxViewRoot.java:253)
 
	at org.ajax4jsf.component.AjaxViewRoot.processApplication(AjaxViewRoot.java:466)
 
	at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
 
	at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
 
	at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
 
	at javax.faces.webapp.FacesServlet.service(FacesServlet.java:265)
 
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
 
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 
	at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
 
	at org.jboss.seam.web.IdentityFilter.doFilter(IdentityFilter.java:40)
 
	at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
 
	at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:86)
 
	at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
 
	at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
 
	at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
 
	at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
 
	at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
 
	at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
 
	at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
 
	at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:368)
 
	at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:495)
 
	at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:56)
 
	at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
 
	at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:60)
 
	at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
 
	at org.jboss.seam.web.HotDeployFilter.doFilter(HotDeployFilter.java:53)
 
	at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
 
	at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
 
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 
	at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
 
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
 
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
 
	at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
 
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:433)
 
	at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
 
	at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
 
	at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
 
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
 
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
 
	at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
 
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
 
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
 
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
 
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
 
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
 
	at java.lang.Thread.run(Thread.java:619)
 
Caused by: java.lang.reflect.InvocationTargetException
 
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 
	at java.lang.reflect.Method.invoke(Method.java:597)
 
	at org.jboss.seam.util.Reflections.invoke(Reflections.java:22)
 
	at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:32)
 
	at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
 
	at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:28)
 
	at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
 
	at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:77)
 
	at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
 
	at org.jboss.seam.bpm.BusinessProcessInterceptor.aroundInvoke(BusinessProcessInterceptor.java:51)
 
	at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
 
	at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:44)
 
	at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
 
	at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
 
	at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:185)
 
	at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:103)
 
	at com.mydomain.pkg.action.DynamicJasper_$$_javassist_seam_9.downloadPDF(DynamicJasper_$$_javassist_seam_9.java)
 
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 
	at java.lang.reflect.Method.invoke(Method.java:597)
 
	at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:335)
 
	at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:280)
 
	at org.jboss.el.parser.AstMethodSuffix.getValue(AstMethodSuffix.java:59)
 
	at org.jboss.el.parser.AstMethodSuffix.invoke(AstMethodSuffix.java:65)
 
	at org.jboss.el.parser.AstValue.invoke(AstValue.java:96)
 
	at org.jboss.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
 
	at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
 
	at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
 
	... 53 more
</td></tr></tbody></table>
houda_lamkahkah's picture
Joined: Apr 2 2012 - 1:24am
Last seen: 7 years 7 months ago
0

 hi all , the problem is solved , it was due to a conflict .

see https://community.jboss.org/thread/186326

 

 

 

houda_lamkahkah's picture
Joined: Apr 2 2012 - 1:24am
Last seen: 7 years 7 months ago
0

 Hi, guys.

As I don't wanna start a new topic to talk about the same thing, I'm posting it here.

I got the same problem. I have a report in which I got 5 fixed columns, and from the 3rd column to before the third last column I have dynamic columns...

Here it is (using excel):

http://img824.imageshack.us/img824/1691/semttulojkl.png

The columns betwenn the red line are the dynamic columns.

A would be the group header.

B would be the detail band, which I want to reset, group by, the service number.

C would be the footer.

 

I now how to do this using groups using iReport, but without being able to dynamically add or remove the columns.

I already tried DynamicJasper, but I'm having some issues grouping the data (the first column data is shown but the others just appear in the other row, and i've tested all the GroupLayouts)..

I'm sure you guys can help me. Thanks!



Post Edited by andreem at 05/11/2012 13:25
andreem's picture
Joined: May 24 2011 - 5:35am
Last seen: 8 years 5 months ago
Feedback