Search code examples
jasper-reports

Jasper report pass field value to javascript method as parameter


I am Jasper report, I would like to create hyperlink which will call javascript method in return.

To achieve this I am trying to pass field "myFieldName" to the javascript method, when I execute this method it simply pass javacode String expression as string.

Any suggestion is appreciated

<hyperlinkReferenceExpression>
"javascript:myFun('{[CDATA[$F{"myFieldName"}]]}')"
<hyperlinkReferenceExpression/>

Solution

  • What you need is something like this:

    <hyperlinkReferenceExpression><![CDATA["javascript:myFun('" + $F{myFieldName} + "')"]]></hyperlinkReferenceExpression>
    

    Note that this doesn't handle cases when the field values contains apostrophe or other characters that cannot be directly used in JavaScript string literals. If you need to cover such cases as you'll need to find a utility method that escapes strings as JavaScript literals.