How to create a dynamic table from JRTableModelDataSource

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
            params.put("MyTable", myTable);

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.

The report generated:

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!

OreoCookie's picture
Joined: Jun 6 2021 - 3:20am
Last seen: 1 year 11 months ago

0 Answers:

No answers yet