Jenkins UI control library supports dynamic databinding, including some useful jelly tags like <dropdownList> and <radioBlock>.
Add <repository>
and <dependency>
in pom.xml:
<repositories>
<repository>
<id>tags</id>
<name>Custom Tags</name>
<url>https://raw.githubusercontent.com/t-wanl/Jenkins-Dynamic-Databinding-UI-Control-Library/master/jar/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>albertxavier</groupId>
<artifactId>jenkins-dynamic-databinding-ui-control-library</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
Please refer to Demo-Jenkins-UI for demo.
In <j:jelly>
defines xmlns:ui="/ui"
, then use namespace ui
in tag defined in this library. E.g. <ui:dropdownList>
The value of the selected <dropdownListBlock>
in <dropdownList>
can be obtained just like <select>
tag.
...
<ui:dropdownList title="Fruit" selectedName="dropdownListSelected" name="dropdownListContent">
<f:dropdownListBlock title="Apple" value="apple" selected="true">
<f:entry title="Number of apple" field="number">
<f:textbox/>
</f:entry>
</f:dropdownListBlock>
</ui:dropdownList>
...
{
...
"dropdownListSelected": "apple",
"dropdownListContent": {
"number": "1"
},
...
}
A certain option in and its can be triggered by other UI controllers.
<f:entry title="Select Fruit" field="preSelect">
<f:select/>
</f:entry>
<ui:dropdownList title="Fruit" selectedName="dropdownListSelected2" name="dropdownListContent">
<f:dropdownListBlock title="Apple" value="apple" selected="true">
<f:entry title="Number of apple">
<f:textbox/>
</f:entry>
</f:dropdownListBlock>
<f:dropdownListBlock title="Banana" value="banana" selected="false">
<f:entry title="">
<f:checkbox name="yellow" checked="true" title="Yellow?"/>
</f:entry>
</f:dropdownListBlock>
</ui:dropdownList>
Note that selectedName
defines the variable stores the selected option and name
defines the variable stores the content in <dropdownListBlock>
Add options to <select>
tag.
public ListBoxModel doFillPreSelectItems() throws IOException, ServletException {
ListBoxModel model = new ListBoxModel();
model.add("Apple", "apple");
model.add("Banana", "banana");
return model;
}
Define trigger rules.
public String doFillDropdownListSelected2Items(
@QueryParameter String preSelect
) throws IOException, ServletException {
if (preSelect.equalsIgnoreCase("apple")) {
return "apple";
}
return "banana";
}
doFillDropdownListSelected2Items
function depends on preSelect
, and return the value of the dropdownListBlock
which you what to select.
<ui:radioBlock name="fruit" value="apple" title="Apple" checked="true">
<ui:radioBlock name="color" value="red" title="Red" checked="false">
<ui:tab><p>Everyone likes red apples.</p></ui:tab>
<ui:radioBlock name="size" value="small" title="Small" checked="true">
<ui:tab><p>Tom like small apple.</p></ui:tab>
</ui:radioBlock>
<ui:radioBlock name="size" value="big" title="Big" checked="false">
<ui:tab><p>Mike like big apple.</p></ui:tab>
</ui:radioBlock>
</ui:radioBlock>
<ui:radioBlock name="color" value="green" title="Green" checked="true">
<ui:tab><p>Who likes green apples?</p></ui:tab>
</ui:radioBlock>
</ui:radioBlock>
In order to support dynaminc databinding, you need to modify both your jelly tag definition, Java code and Javascript code.
fillUrl
attribute defines tag itself, and Stapler can render it withdoFillXyzItems
dunction.fillDependsOn
attribute defines the tag field which the <dropdownList> depends on.MorphTagLibrary
makes a tag support dynamically set attributes: e.g. In <select>Please refer to MorphTagLibrary for more details.<m:select xmlns:m="jelly:hudson.util.jelly.MorphTagLibrary" class="setting-input ${attrs.checkUrl!=null?'validated':''} select ${attrs.clazz}" name="${attrs.name ?: '_.'+attrs.field}" value="${value}" ATTRIBUTES="${attrs}" EXCEPT="field clazz">
- Defines
doFillXyzItems
with@QueryParameter
that representfillDependsOn
attribute. calFillSettings
inDescriptor.java
computes the list of other form fields that the given field depends on, via thedoFillXyzItems
method, and sets that as thefillDependsOn
attribute. Also computes the URL of thedoFillXyzItems
and sets that as thefillUrl
attribute. Add${descriptor.calcFillSettings(field,attrs)}
to your jelly code to call this function. Please refer to calFillSettings and select.js for more details.- Update view in Javascript.
hudson-behavior.js defines
jenkinsRules
, while behavior.js uses css selectors to apply javascript behaviors to enable unobtrusive javascript in html documents. You need to write your own Javascript code for updating your custom jelly tag. Please refer to select.js for more details. Finally reference your Javascript code like this in custom jelly tag:<st:adjunct includes="lib.form.select.select"/>
. Please refer to select.jelly for more details.