Вы находитесь на странице: 1из 11

JasperReports Interactivity: The

Table Component
JasperReports Interactivity: The Table Component
https://www.jaspersoft.com/blog-entry/jasperreports-interactivity-the-table-component

Posted by Matthew Geise Tuesday, January 24, 2012 at 11:58am

We have all had experience looking at static reports and wishing we could filter, re-sort,

or even highlight particular data points as we analyze what the data means. Many times,

this has involved exporting the data to another tool, such as Excel, and doing additional

analysis there – or worse, asking the report developer to create yet another report to

make changes that we should be able to do on our own.

With the table component for JasperReports combined with the JasperReports Server

platform (Community and Commercial Editions), we have introduced the beginning of

what will be some very exciting interactivity features for the consumers of your reports.

The table component adds additional structural elements so that JasperReports not only

understands the layout of the individual elements that make up the columns and rows

within your tables, but also recognizes the relationship between these elements. With

this understanding of the element relationships, we can now introduce additional

intelligence within the final report output that will make the reports more interactive and
allow you to conduct the additional analysis of information, that you may not have

anticipated prior to seeing the data, directly through the report output to make faster,

more informed decisions.

Creating Your First Report using the Table Component


Let’s take a brief look at how to implement a report using the Table Component to

generate a simple tabular representation of data. The following are the basic steps we

will take:

1. Set up a data source connection within iReport from where we will pull data for
our report
2. Create a new report within iReport, using the report wizard and remove the report
bands that we won’t need for our report
3. Set up a new data set for the table component to use
4. Add and format the table component to display the data we are interested in
5. Deploy the report to the JasperReports Server repository

Step 1: Create a datasource/database connection


Our first step will be establish a database connection within iReport that we can use to

provide data for our report. To make things simple, we will use the foodmart data that is

included as sample data with JasperReports Server.

1. Open iReport
2. Click on the Report Datasources icon to add a new datasource

3. Click “New”
4. Select “Database JDBC connection” as the datasource type, and click “Next”
5. Fill in a name and the database connectivity information to the foodmart data

within your database, and click “Save”

Step 2: Create a New Report


Next, let’s create a new report in iReport.

Note: Because the table component uses it’s own dataset, I will set up my main report

query to have a dummy query of: Select 1 as dummy_field

You now have a simple report showing various bands. For the purposes of this example,

we will only use the Title and Summary bands.

1. Click on “Step 2: create a new report”


2. Select “Report” on the left side of the resulting window that opens and the “Blank
Letter Landscape” template on the right and then click “Launch Report Wizard”
3. Provide a name and file location to save your report and click “Next”
4. Select the datasource that we created in step 1 and fill in a main report query and
click “Next”
5. Choose the fields for your report by moving them to the right side and Clicking
“Next”
6. Skip the grouping – click “Next”
7. Click “Finish”
8. Right click on each of the bands, except for the Title and Summary bands, and
select “Delete Band” to remove the band from the report
9. Set the title and summary band height to your desired height – for my example, I
will set the title band height to 40 and the summary band height to 50.
10. Now, add a Title to your report
1. Drag the Static Text element from the palette into the Title Band
2. Size the element to fill the title band between the margins
3. Format the text based on how you would like the title to appear.
(Increasing the font size/centering/bold, etc.)
4. Set the text to whatever you want your Report Title to be

Step 3: Add a dataset to populate the table component


The table component does not get its data from the main report query. Rather, it requires

a separate data set to populate from. This is convenient in cases where you want to have

a chart as well as a table as the main report query can be used to populate the chart

independently from the table.

1. In the repository inspector pane within iReport, right click on the top level of the
report tree representing the Report itself and select “Add Dataset”
2. Name your dataset – for this example, I will name it “Table Dataset”
3. Select to create the dataset from a connection or datasource and click “Next”
4. Select the data source that we created in step 1 and enter the following query
and click “OK”:

5. SELECT fullname AS customer_fullname,

6. country AS customer_country,

7. gender AS customer_gender,

8. total_children AS customer_total_children,

9. occupation AS customer_occupation,

10. houseowner AS customer_houseowner,

11. yearly_income AS customer_income

FROM customer
Step 4: Add the Table Component to the Report
Now that we have a report with a dataset defined for our table component, we are ready

to add the table component to the report.

Once you have set and accepted the formatting, the table will open with the fields

populated in the table as well as the field names as the heading in the column header.

You can change the heading text to any text that you wish by double clicking on the cell

and typing the text you want.

Lastly, we need to make sure that the table component is sized in our main report so that

it will fit properly. Each of the columns in the table is automatically sized to 90 pixels,

giving us a total of 630 pixels across. Looking at the main report, there is 752 pixels

available from margin to margin.

1. Within iReport, drag the Table from the Palette into the Summary Band
2. When the Table Wizard opens, select the Dataset we defined in Step 3 as the
dataset from which the table should be created and click “Next”
3. Select all of the fields and move them to the right column in the field selector and
click “Next” (Note that the order here is the order that the fields will appear in your
table.)
4. Select to use the same connection used to fill the master report and click “Next”
5. Now set the formatting for your table:
1. Select the color scheme you want to use within your table
2. Check the box to use an alternated detail row background
3. Set the border style based on your preference
4. Select the headers and footers you want to include in the report – for the
purposes of this example, I will choose to only include the column header
5. Click “Finish”
6. Right click on the table component in the main report and select “Size > Adapt to
Parent” in order to automatically set the table component available size to the same
as the Summary band.

Now that we have everything in the report set up, we can confirm that it works as

expected by running it within iReport.

1. Make sure that the datasource we set up in Step 1 is selected as the current
Datasource in iReport.
2. Now click “Preview”

The report should run, pulling data from the datasource and presenting the first page as

expected.
Step 5: Deploy the report to JasperReports Server
We will assume that you have already set up a connection to your installation of the

JasperReports Server. Using the Repository Navigator within iReport, we will now take

the report that we created and deploy it to JasperReports Server.

In our case, since we are using the sample data that came with the JasperReports

Server, a data source is already defined that points at the Foodmart data that we

developed our report for.

1. Change from the preview back to the Designer mode within iReport
2. Navigate in the Repository Navigator to the folder where you want to deploy your
report
3. Right click on the folder and select “Add > JasperServer Report”
4. Give the report a unique ID, a descriptive name, and a Descriptive description and
click “Next”
5. Select to use a “Locally Defined” JRXML file and click “Get source from current
opened report” an then click “Next”
6. Click to select a Data Source from the repository and click “Browse”
7. Browse to “analysis/datasources” and select the “FoodMartDataSource” and click
“Open Resource”
8. Finally, click “Finish”

Now you are ready to login to your JasperReports Server instance and run your report.

Using the First Set of Interactive Features


Once you login to JasperReports Server and run your report that uses the table

component, you will be able to access interactive features such as sorting and filtering.

Sorting
To sort the data, click on the column header for the field you want to sort by:
 Clicking once will sort ascending and present an indicator in the column header
that the data is sorted
 Clicking a second time will change the sort to descending and change the
direction of the sort indicator
 Clicking a third time will eliminate the sorting and remove the sort indicator

Sorting a second column will result in the data first being sorted by the first column and

then by the second.

Filtering
To filter the data, right click on the column header for the field you want to filter based

on. There are various filter options such as equals, between, greater than, etc. based on

the type of data you are filtering.

Adding a filter to a column will result in the data being filtered and a filter indicator

appearing in the column header.

To remove a filter, simply right click on the column again and click “Clear” on the filter.

An Example:
Taking the sample report we created, we can use filtering and sorting to identify

answers to questions that we may not have thought to ask when we were designing the

report initially.

For example, let’s say that we wanted to know which female customers were from

Canada, had more than 4 children and owned a home. We want this list sorted by name.

We can now filter and sort our way to the answer:

 Add a filter on the gender column to “Equals” F


 Add a filter on the # of Children to “Greater Than” 4
 Add a filter on the Country to “Equals” Canada
 Add a filter on the Home Owner column to “Equals” Y
 Sort on the name column

Using these simple sorting and filtering features, we now have a 2 page list of customers

that meet the criteria that we were looking for from our original list of over 380 pages.

We can now export the resulting report in the format we wish to share it in.
You can see the value of the table component and the interactivity it introduces when

combined with the server platform. Over time, we will expand on the capabilities and the

value will only increase. I hope this post will help you to make use of the table

component wherever you can to put even more power in the hands of your users.

Want the report source for this example? Download it here.

Get involved with our community and provide you input on the interactivity features by

going to our forums and trackers on JasperForge.org.

Community, How-To, Interactivity, jasperreports server, Table Component

Вам также может понравиться