Variable doubt in jelly-javascript-java for plugin development

77 views
Skip to first unread message

Virginia Mesa

unread,
Feb 5, 2018, 12:08:19 PM2/5/18
to Jenkins Developers
Hi,
I am developing a functionality in a Jenkins plugin, and I have a problem when passing a variable to a Java method.
Let's see if I explain it well, and if you can help me:

- What I want to do is to choose one of the options from a ComboBox, and this option to pass it as a parameter to the method, so that the internally shows me what I want. So:




1. When I choose an option of the ComboBox, I have an EventListener that collects that value ('SCT' in this case) and changes the value of the variable pack_var. Up to here, everything OK. 2. Then I want to use that variable, to pass it as a parameter of a Java method (the one I have declared inside the variable 'orderForPackage' that is: getOrdersForPackage (pack_var), but when passing the variable in this way, it does not do anything to me.


My question is how can I pass the value of the option obtained by parameter. To get this out: (Here I forced an option, but it would be the final result):









Resultados de la búsqueda

Mi duda es como puedo pasar el valor de la opción obtenido por parámetro. Para que me salga esto: (Aquí he forzado una opción, pero sería el resultado final)t


























































Thaks!
Auto Generated Inline Image 1
Auto Generated Inline Image 2

Ullrich Hafner

unread,
Feb 5, 2018, 5:24:34 PM2/5/18
to jenkin...@googlegroups.com
Your approach can’t work. The jelly part <j:set … > is evaluated and processed on the server side before the page is rendered in the client. I.e. it is called *before* your JS code is evaluated. You need to use AJAX to call a Java method in your event listener.

Am 05.02.2018 um 18:08 schrieb Virginia Mesa <v.mesa...@gmail.com>:

Hi,
I am developing a functionality in a Jenkins plugin, and I have a problem when passing a variable to a Java method.
Let's see if I explain it well, and if you can help me:

- What I want to do is to choose one of the options from a ComboBox, and this option to pass it as a parameter to the method, so that the internally shows me what I want. So:

<Auto Generated Inline Image 1.png>


1. When I choose an option of the ComboBox, I have an EventListener that collects that value ('SCT' in this case) and changes the value of the variable pack_var. Up to here, everything OK. 2. Then I want to use that variable, to pass it as a parameter of a Java method (the one I have declared inside the variable 'orderForPackage' that is: getOrdersForPackage (pack_var), but when passing the variable in this way, it does not do anything to me.


My question is how can I pass the value of the option obtained by parameter. To get this out:
(Here I forced an option, but it would be the final result): 

<Auto Generated Inline Image 2.png>







Resultados de la búsqueda

Mi duda es como puedo pasar el valor de la opción obtenido por parámetro. Para que me salga esto: (Aquí he forzado una opción, pero sería el resultado final)t


























































Thaks!

--
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/bd8d4bb9-0f99-48ba-95dd-0b64ffcf0155%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
<Auto Generated Inline Image 1.png><Auto Generated Inline Image 2.png>

signature.asc

Virginia Mesa

unread,
Feb 6, 2018, 4:32:31 AM2/6/18
to Jenkins Developers
Hello,

Yes! It was that.
Now, I have already gotten back the value of the Java method.
The problem is that the return is an Array..and in this way, it is returning the values ​​to me.

I explain:



When I pass it through Ajax, it returns the inside of the array. Is there any way to tell you that what I want to receive is an array? For example, in the res.responseObject () you can specify what type of object. Thank you very much for the help, Greetings.
Auto Generated Inline Image 1

Ullrich Hafner

unread,
Feb 6, 2018, 5:02:44 AM2/6/18
to Jenkins Developers
I’m not sure if I correctly understand your question. How does your Java method look like? Typically it will return a JSON object that will be parsed in JS.

Am 06.02.2018 um 10:32 schrieb Virginia Mesa <v.mesa...@gmail.com>:

Hello,

Yes! It was that.
Now, I have already gotten back the value of the Java method.
The problem is that the return is an Array..and in this way, it is returning the values ​​to me.

I explain:

<Auto Generated Inline Image 1.png>

When I pass it through Ajax, it returns the inside of the array. Is there any way to tell you that what I want to receive is an array? For example, in the res.responseObject () you can specify what type of object.

Thank you very much for the help,
Greetings.
--
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.
signature.asc

Virginia Mesa

unread,
Feb 6, 2018, 5:45:05 AM2/6/18
to Jenkins Developers
Hi,
Look,this is the Java method:



The method returns an array of string, does it always return a JSON? What happens is that then I use that array to fill a table, so I need it to be an array to go through it like this:




To that variable 'orderOfcomps', I want to give it the value of the array to be able to traverse it, and in this way I would already have it.

Thank you very much again, I hope I do not cause you much trouble. A greeting.
Auto Generated Inline Image 1
Auto Generated Inline Image 2

Baptiste Mathus

unread,
Feb 6, 2018, 6:12:34 AM2/6/18
to Jenkins Developers
Does this method work when called from the associated JUnit test? (BTW, this code is missing a finally block to correctly clean up resources and looks like sensitive to SQL injection attack, but let's table that for now I guess).

--
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-dev+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/296aa94d-a93d-4181-ae0b-046465b64cc2%40googlegroups.com.

Virginia Mesa

unread,
Feb 6, 2018, 6:22:20 AM2/6/18
to Jenkins Developers
Good,

the method works correctly. The problem has arisen when passing a variable that I have to collect previously. If, for example, I 'force' the parameter to be a specific string, I do it well.

Yes thanks! Once I get what I want, I will try to improve the code as you indicate :)

A greeting.

Ullrich Hafner

unread,
Feb 6, 2018, 6:48:15 AM2/6/18
to Jenkins Developers
Am 06.02.2018 um 11:45 schrieb Virginia Mesa <v.mesa...@gmail.com>:

Hi,
Look,this is the Java method:

<Auto Generated Inline Image 1.png>

Text would be preferred :-(

The method returns an array of string, does it always return a JSON?

I don’t know if Jenkins does convert the array of strings into a JSON object. But you will see the returned data structure in your browsers debugger.

I converted it in my views manually. My signature looks like

   @JavaScriptMethod
    public JSONObject getTableModel() {
        return JSONObject.fromObject(new IssuesTableModel().toJsonArray(getIssues()));
    }

In the jelly file I am working with the JSON object in JS:


<script>
      …

      view.getTableModel(function(t) {
      (function ($) {
      var table = $('#warnings').DataTable();
      table.rows.add(t.responseObject().data).draw()
      })(jQuery);
      })
</script>



What happens is that then I use that array to fill a table, so I need it to be an array to go through it like this:

<Auto Generated Inline Image 2.png>
To that variable 'orderOfcomps', I want to give it the value of the array to be able to traverse it, and in this way I would already have it.


You can’t use jelly expressions here, this needs to be done in JS.

Thank you very much again, I hope I do not cause you much trouble.

A greeting.

--
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.
<Auto Generated Inline Image 1.png><Auto Generated Inline Image 2.png>

signature.asc

Virginia Mesa

unread,
Feb 6, 2018, 7:28:51 AM2/6/18
to Jenkins Developers

Hello,
while I test what you have written to me ..
I've been looking, you know how the tag <g: evaluate> works?

I have seen that it allows to execute javascript and give value to the variable that you need. Could I do something like that? or something similar..:

var select = document.getElementById("packgeItm");
select.addEventListener('change',
function(){
var selectOption = this.options[select.selectedIndex];
//
Here I get what I have selected in the ComboBox (selectOption = String)
 
var myAction = <st:bind value="${it}" />
myAction.getOrdersForPackage(selectOption.value,function(res){
var result_array = res;
//
This returns the array I need, or JSON
 
})
});



I need the value of that array to be saved in this variable, to then go through it. Could I do something like this ?:

<g:evalute var="result" object="true">

--Insert the javascript code here--
result_array;

</g:evaluate>


This way, it does not work ... but some similar idea.. :(

Thanks!!!!

Reply all
Reply to author
Forward
0 new messages