Re: Jenkins configure-entries - Select Views to display using a Map

133 views
Skip to first unread message
Message has been deleted

Ullrich Hafner

unread,
Dec 21, 2016, 5:42:18 AM12/21/16
to Jenkins Developers
Are you writing your own dashboard? Wouldn’t it make more sense to just write some portlets for the dashboard view plugin (https://wiki.jenkins-ci.org/display/JENKINS/Dashboard+View)?

Am 21.12.2016 um 01:24 schrieb PF66 <pedrodi...@gmail.com>:

I'm creating a dashboard plugin on Jenkins where I want to show just some of my views and their content inside it.

For that, I pretend to create a list of check boxes similar to the 'View' configuration page, where you can select which Jobs to include (like the folowing):


enter image description here

I'm fairly new to jelly and Jenkins itself, so it's being a trouble to find the solution.

What I'm trying to do is:

  1. Create a Map<K, V> in my Dashboard where I want to populate with all the Views of my Jenkins instance and being 'false' as default.

Constructor:


private Map<String, Boolean> selectedViews;

public MyDashboard(String name, String viewName) {
  super(name);
  //more variables inside
  this.selectedViews.putAll(initSelectedViews());
}

protected Object readResolve() {
  //more variables inside
  if (selectedViews == null || selectedViews.isEmpty()){
      this.selectedViews.putAll(initSelectedViews());
}


And the initializing function


    public Map<String, Boolean> initSelectedViews(){
    Map<String,Boolean> selectedViews = new HashMap<>();

    for (View v: Jenkins.getInstance().getViews()) {
        //Debugged here, never happens a single iteration. 
        //Maybe getViews() returns 'null' while creating the plugin?
        selectedViews.put(v.getViewName(), false);
    }
    return selectedViews;
    }


The problem is that in other functions, where I want to iterate through views, Jenkins.getInstance().getViews() works just fine. But when I try to initialize this Map, I always get an empty array.

Should I use other methods to get the Views? Or save them in a different variable?

  1. On the configure-entries.jelly I want to save in the selectedViews Map which are those selected, and so far I have:

    <f:section title="${%Views Filter}">
    <f:entry title="Views: ${it.selectedViews}"/>
    <f:entry title="${%Select views to display}">
        <j:forEach  var="view" items="${it.selectedViews}">
            <span>
                <f:checkbox name="${it.selectedViews.view}" title="${view.key}" checked="${view.value}" json="true"/>
                <br/>
            </span>
        </j:forEach>
    </f:entry>

Where they display correctly both 'Name' and 'Value' because I already tried with an hardcoded Map, but can't manage to save them properly.


An alternative I was also trying, was to push every value of selected checkboxes to an Array, that will then pass to the backend, as you can see in the following code. But don't know how to pass the array variable with each name of the selections:



Introduzir código aqui...<f:section title="${%Views Filter}">

    <f:entry title="${%Select views to display:}">
<script type="text/javascript">
var array;

if('${it.selectedViews.isEmpty()}'){
array = new Array();
}else{
array = new Array('${it.selectedViews}');
}

function selectView(name){
var i = array.indexOf(name);

if(i == -1){
array.push(name);
}else{
array.splice(i, 1);
}
array.sort(); //This is the array I want to pass when I click 'Apply'

}
</script>
<j:forEach var="view" items="${it.views}">
<span class="selectedBox">
<f:checkbox title="${view.displayName}" value="${view.displayName}" checked="${it.selectedViews.contains(view.displayName)}" onclick="selectView(this.value)"/>
<br/>
</span>
</j:forEach>
</f:entry>
</f:section>



Any help or suggestion is appreciated.


--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-de...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/37dadad8-e25e-446e-96c0-8dce89296b20%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

PF66

unread,
Dec 21, 2016, 6:36:58 AM12/21/16
to Jenkins Developers
Yes, I'm creating it almost from scratch, based from the Mission Control (https://wiki.jenkins-ci.org/display/JENKINS/Mission+Control+Plugin) but instead of all the Jobs, I want to select Views and which ones.
It is working right now by opening all the Views, but want to filter them and can't find the solution.

By coincidence, I saw you replied on another topic with the same issue, but I couldn't well understand it: https://groups.google.com/forum/#!searchin/jenkinsci-dev/jelly$20views$20ullrich%7Csort:relevance/jenkinsci-dev/qqIHUb_eWRs/2a2L92Txum0J
I didn't post it there because it's from '12, thought would be better to create my own topic, but the doubt is exactly the same.
What's the best way of selecting from the Views in checkboxes and saving them after click on apply/ok in the configure-entries ?

Ullrich Hafner

unread,
Dec 21, 2016, 7:38:26 AM12/21/16
to Jenkins Developers
Is the size of the list of views fixed? 
yes: use several booleans as parameter in your @DataBoundConstructor constructor. 
no: you need a list of views in the @DataBoundConstructor constructor  

PF66

unread,
Dec 21, 2016, 7:56:54 AM12/21/16
to Jenkins Developers
Sorry but I still don't understand. I never used @DataBoundConstructor so no idea where to start
.
The Views list is dynamic, and I can access it on my config.jelly with '{it.views}', create all the checkboxes and whatnot:

<j:forEach  var="view" items="${it.views}">
    <span class="selectedBox">
        <f:checkbox title="${view.displayName}" value="${view.displayName}" checked="${it.selectedViews.contains(view.displayName)}" onclick="selectView(this.value)"/>
        <br/>
    </span>
</j:forEach>

The 'onclick' function is the one messing me. What I want to pass to my Java is an ArrayList<String> of selected views, then I can process what I need.

I have no problem passing single values like Int or String by never using the BoundConstructor.
Can you provide some sample of how should I do it?
Reply all
Reply to author
Forward
0 new messages