Jump to content
We've recently updated our Privacy Statement, available here ×
  • Is there a simple way to add field/column sorting to a JRXML report?


    abresci
    • Version: v5.2, v5.1, v5.0, v4.8, v4.7, v4.6, v4.5, v4.2, v4.1, v4.0.2 Product: iReport Designer

    Issue:

    I know that in iReport Designer, in the popup menu, if you click "edit query", on the "report query" screen there is a button for "sort options", which lets you statically choose one or more fields and/or variables on which to sort your report.

    My question is: is there any way to dynamically tell the report what column to sort on? Specifically, I want to be able to create a hyperlink, and in the hyperlink parameters I'd like to be able to specify something like a field/column name on which the target report should sort.

    Is there a way to do this, and does this work with Domain data sources as well as SQL?

    Thanks.


    Resolution:

    In the some current versions of JasperReports (4.2.0 for example), you will find a "sort" element in your iReport Designer palette. You can drag this element into your report layout (for example, in a header row), and you will see a toggling arrow (which can sort ascending or descending). After adding this sort option to your report, select it, and navigate to its "properties". In this view, you can assign a column to it, and other properties like styles, etc. When you run the report and click on the arrow, it will sort the report's data based on the column you selected, and the direction (ascending or descending). Here is an example of what this element looks like in your jrxml:

    <componentelement>
      <reportelement height="16" width="31" x="484" y="4">
        <sr:sort handlercolor="#ffffff"
                 handlerhorizontalalign="Center"
                 handlerverticalalign="Middle"
                 http:=""
                 jasperreports.sourceforge.net=""
                 sortfieldname="field_to_sort"
                 sortfieldtype="Field"
                 xmlns:sr="http://jasperreports.sourceforge.net/jasperreports/components"
                 xsd=""
                 xsi:schemalocation="http://jasperreports.sourceforge.net/jasperreports/components" />
      </reportelement>
    </componentelement>
    

    I have attached a zip enclosed jrxml file, below, that you can examine as well (it uses sample data from the /datasources/JServerJNDIDS data source in JR Server). When you run it, you will see an arrow to the right of the "Name" column header (left of "Title"). Clicking on this arrow will sort the report based on the Employees' last names. This works for SQL-based data sources, domains, and other data sources (the sorting is done in the JasperReports data set).

    *Edit by a user:

    In 4.7, 5.1, and 5.2 you can not insert sort options into the sr:sort object. It will be shown on a new line <sr:symbol>. Unfortunately this editor has not been able to get this system to work as of yet. Will update when successful 8/16/2013*


    Ref. Case #00022336 -- 01:10, 18 November 2011 (UTC)

    sort.zip


    User Feedback

    Recommended Comments

    There are no comments to display.



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