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

How to create a dynamic table from JRTableModelDataSource


OreoCookie

Recommended Posts

Hi everyone,
I'm really new to Jasper and I'm on a deadline, I would appreciate any help or ideas.
I'm using JasperReports 6.16.0 to build and design a template for reports I need to generate from my application.
I've been trying to create a report template with a table that would be dynamic and change for each report.
Basically, my goal is to create reports on different objects every time using the same template, so I want to save the 'columns' (object's fields' names) and the data as string arrays.
I've tried many things and non has worked.
For now, I'm using a dummy code to see if something like that could possibly work.
I've created a class that extends JRTableModelDataSource that basically has two fields
    private String[] columns;
    private String[][] data;
In my java code, I create a table and sent it as a parameter

            String[] data1 = {"45315", "fraud", "143.65", "normal"};            String[] data2 = {"43534165", "fraud", "23167.56", "critical"};            String[] data3 = {"65876", "check", "855", "normal"};            String[] data4 = {"09866", "fraud", "9657", "high"};            String[][] data = new String[][] {data1, data2, data3,data4};            String[] columns = new String[] {"id","type","amount","severity"};            TableModel table = new DefaultTableModel(data, columns);            TableDatasource myTable = new TableDatasource(table); //this class extends JRTableModelDataSource            myTable.setColumns(columns);            myTable.setData(data);                        params.put("MyTable", myTable);[/code]

I was able to generate a table with data when creating fields calling specifically to those columns, but the names of the columns and the number of columns can be changed
I need something that could maybe loop over the array and build the table on its own.

This is an example of what I did that worked, but it is not enough for what I need, I need to be able to generate the table without declaring the specific columns and fields.

capture.png.1bfbedb5cfcb62904c909ae405540153.png

The report generated:
report.png.1bf91ca2180da2eede14e9e1d1089b21.png

I tried working with crosstab, but it didn't work at all. I've tried searching the web back and forth and couldn't find an example similar to what I need.
It is not possible for me to create many subreports and each subreport would represent a different object with declared fields on the table because there are thousands of them and the report changes according to different factors on my application apart from this template logic.

Thank you all!
    

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Posted Images

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...