Search code examples
jenkins-pluginsjelly

onclick in jelly is not working, jenkins plugin


I'm trying to make use of the @onclick when developing a jenkins plugin but nothing happens when the checkbox which has this attribute is clicked (same with @onchange).

I have a checkbox for each job implemented as a ListViewColumn with the corresponding column.jelly file

<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">

<td>
<f:checkbox name="selected" onclick="${it.itClicked()}" onchange="${it.itClicked()}" />
</td>
</j:jelly>

and the method itClicked() resides in the corresponding java class of this jelly file:

class CheckboxTemplate extends ListViewColumn  {

    ...

    public void itClicked() {
        System.out.println("it clicked!");
    } 

    ...

    @Extension
    public static class DescriptorImpl extends ListViewColumnDescriptor {

    ...

    }

But this apparently is not working...

I've made another plugin before which called a method in the java class from the predefined object "it" from the jelly file and it worked fine, so having the method defined in the working place is not the problem (I assume).

Anyone who has any idea of why itClicked() is not executed when the checkbox is checked/changed?

If there is information that I missted posting, tell me, and I'll come back with it asap!


Solution

  • I managed to solve this by adding

    <st:bind var="myItem" value="${it}"/>
    

    in my jelly file and changing @clicked to

    onclick="myItem.mark('${job.fullName}')"
    

    where mark() was defined in the backend with the particular annotation

    @JavaScriptMethod
    public void mark(String job) {
       // do what you need to do
    }
    

    job if of course a predefined variable in the column.jelly file which is not required for this to work, I only passed it to map the checkbox with the corresponding job name.

    Hope this is useful for others who struggle with the documentation (and with the nonexisting complete examples) on these topics.