How can I add a button in jasper report?

2019-02-25 14:37发布

Is it possible to add a button in the report, which any action will be fixed?

Example: Button "View", which will show more detailed statistic.

Example button

Comment:

I do not absolutely understand how it is possible to make a button of a text element

1条回答
趁早两清
2楼-- · 2019-02-25 14:38

To create a button, create a textField with hyperlinkType="Reference" and a hyperlinkReferenceExpression

Example

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="JddButton" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="2a88eaae-fbef-4a3d-b8bf-0b12bafc985b">
<title>
    <band height="80" splitType="Stretch">
        <textField hyperlinkType="Reference" hyperlinkTarget="Blank">
            <reportElement mode="Opaque" x="150" y="20" width="200" height="40" forecolor="#FFFFFF" backcolor="#009900" uuid="822deecb-059b-4921-bfb7-07ee7cbde26a">
                <property name="net.sf.jasperreports.export.html.class" value="btn"/>
                <property name="net.sf.jasperreports.export.html.id" value="idBtn"/>
            </reportElement>
            <textElement textAlignment="Center" verticalAlignment="Middle">
                <font size="24"/>
            </textElement>
            <textFieldExpression><![CDATA["VIEW"]]></textFieldExpression>
            <anchorNameExpression><![CDATA["myButton"]]></anchorNameExpression>
            <hyperlinkReferenceExpression><![CDATA["https://stackoverflow.com/users/5292302/petter-friberg"]]></hyperlinkReferenceExpression>
            <hyperlinkTooltipExpression><![CDATA["Click to view"]]></hyperlinkTooltipExpression>
        </textField>
    </band>
</title>
</jasperReport>

Will generate

Result

when clicked it will direct you to the value of the hyperlinkReferenceExpression, in example I have also set hyperlinkTarget="Blank" so it opens in a new page.

For additional information on the attributes see JRHyperLink API

Hey I want my button to change color when I pass over it....

No problem, (as long as export is html, in pdf you need to do some tricks with annotation but I will leave this out..).

I have added some css tags on the textElement see the property under the reportElement

So some simple css.

<style type="text/css">
    a {text-decoration: none}
    td.btn:hover {
        background-color: yellow !Important;
    }
</style>

Finally, how do I put this css in the output file?

Export custom HTML template

How to add css to jasper report server

查看更多
登录 后发表回答