How to retrieve element from array in a jelly tag

mahudg2
Kilo Expert

Sorry I understand this might be a very simple question, but despite my research I've hit a wall here. Here is my code below

<g:evaluate>
var gr = new GlideRecord('u_uksystemstatus');
var statuses = new Array();
var servicestates = new Array();
var i = 0;
gr.query();
while(gr.next())
{
statuses[i] = gr.u_servicetitle.toString();
servicestates[i] = gr.u_servicestate.toString();
i = i + 1;
}
</g:evaluate>

<j:forEach var="jvar_word" items="${words}">
   <p>${jvar_word}</p>
</j:forEach>
<j:forEach var="jvar_status" items="${statuses}">
                         <li>${jvar_status} ${servicestates}</li>
</j:forEach>
</j:jelly>

My goal   is to display each status in the statuses array with the corresponding service state, but I'm not sure how to retrieve a specific element in an array that corresponds to the element that is being retrieved in the <j:forEach> tag. Does anyone have any ideas?

 

Thanks!

12 REPLIES 12

conmic
Mega Guru

I'm doing something similar and fail too...



            <g:evaluate var="jvar_ship_to_array" jelly="true" object="true">


              var SysIds2 = jelly.jvar_mrl_ids;


              var shiptoGr = new GlideRecord('proc_po_item');


              shiptoGr.addQuery('sys_id','IN',SysIds2);


              shiptoGr.query();


              var shipto = [];


              while (shiptoGr.next()){


                var objShipto = new Object();


                var getId = shiptoGr.purchase_order.ship_to.getValue();


                var getDisplay = shiptoGr.purchase_order.ship_to.getDisplayValue();


                objShipto.sysid = getId;


                objShipto.label = getDisplay;


                shipto.push(objShipto);


              }


              var test = shipto[0];


              shipto;


            </g:evaluate>



            ${test.sysid}




            <j:forEach items="${jvar_ship_to_array}" var="jvar_shipto">


              ${jvar_shipto.sysid}


              ${jvar_shipto.label}


            </j:forEach>





Strange is that the ${test.sysid} returns the value properly... <j:forEach> seems not to work, or what am I doing wrong?


I found the solution in this discussion:


Jelly: Over and Over Again...



You have actually to evaluate each object value as Jelly seems not to be capable of doing so. See line 19 and 21 below



            <g:evaluate var="jvar_ship_to_array" jelly="true" object="true">


              var SysIds2 = jelly.jvar_mrl_ids;


              var shiptoGr = new GlideRecord('proc_po_item');


              shiptoGr.addQuery('sys_id','IN',SysIds2);


              shiptoGr.query();


              var shipto = [];


              while (shiptoGr.next()){


                var objShipto = new Object();


                var getId = shiptoGr.purchase_order.ship_to;


                var getDisplay = shiptoGr.purchase_order.ship_to.getDisplayValue();


                objShipto.sysid = getId;


                objShipto.label = getDisplay;


                shipto.push(objShipto);


              }


              shipto;


            </g:evaluate>



              <j:forEach items="${jvar_ship_to_array}" var="jvar_shipto">


                  <g:evaluate var="jvar_shipto_id" jelly="true" expression="var shiptoSysid = jelly.jvar_shipto.sysid; shiptoSysid;"/>


                  ${jvar_shipto_id}


                  <g:evaluate var="jvar_shipto_display" jelly="true" expression="var shiptoDisplay = jelly.jvar_shipto.label; shiptoDisplay;"/>


                  ${jvar_shipto_display}


              </j:forEach>


All,



I am also having trouble.   I copied a functioning <j:forEach from another UI Page but it doesn't work in the code below.   I have had to hard code the <option> tags which will make this a single purpose UI Page unless I can get it resolved.



<div id="divContainer" class="container">


<g:ui_form onsubmit="return invokePromptCallBack();">


  <g2:evaluate>


  var title = "${RP.getWindowProperties().get('title')}";


  title =   new GlideStringUtil().unEscapeHTML(title);


  var message1 = "${RP.getWindowProperties().get('message1')}";


  message1 =   new GlideStringUtil().unEscapeHTML(message1);


  var message2 = "${RP.getWindowProperties().get('message2')}";


  message2 =   new GlideStringUtil().unEscapeHTML(message2);


  var question = "${RP.getWindowProperties().get('question')}";


  question =   new GlideStringUtil().unEscapeHTML(question);


  var useNone = "${RP.getWindowProperties().get('useNone')}";


  var options = "${RP.getWindowProperties().get('options')}";


  options = options.toString();


  var arrOptions = options.split(',');


  //arrOptions = ['No', 'Yes'];


  </g2:evaluate>


  <h1>$[title]</h1>


  <div class="message">$[message1]</div>


  <div class="message">


  <j:forEach items="${arrOptions}" var="jvar_option" indexVar="jvar_idx">


  <span>${jvar_option}</span>


  </j:forEach>


</div>


  <div class="question">


  <label for="question1"><g:no_escape>$[question]</g:no_escape></label>$[SP]$[SP]


  <select id="question1">


  <j2:if test="$[useNone == 'Yes']">


  <option value="" id="option_none"> - None - </option>


  </j2:if>


  <j:forEach items="${arrOptions}" var="jvar_option" indexVar="jvar_idx">


  <option value="${jvar_option}" id="option_${jvar_option}">${jvar_option}</option>


  </j:forEach>


  <option value="No" id="option_No" selected="true">No</option>


  <option value="Yes" id="option_Yes">Yes</option>


  </select>


  </div>


  <div class="message">$[message2]</div>


  <table border="0" width="100%">


  <tr>


  <g:macro


    ok="actionOK()"


    cancel="GlideDialogWindow.prototype.locate(this).destroy(); return false"


    ok_id="ok_button"


    cancel_id="cancel_button"


    ok_type="submit"


    cancel_type="submit"


  />



  <td class="dialog_buttons">


  <g:dialog_button


  id="${jvar_ok_id}"


  type="button"


  onclick="invokePromptCallBack('ok');"


  style="width: 5em;">${gs.getMessage('OK')}</g:dialog_button>


  </td>


  <td class="dialog_buttons">


  <g:dialog_button


  id="${jvar_ok_id}"


  type="button"


  onclick="invokePromptCallBack('cancel')"


  style="width: 5em;">${gs.getMessage('Cancel')}</g:dialog_button>


  </td>


  </tr>


  </table>


</g:ui_form>


</div>



Any help would be greatly appreciated.



Respectfully,


Robert


Hey, Robert



arrOptions is being defined in the second phase - indicated by the 2 in g2:evaluate.


Your j:forEach is running in first phase which will be why it isn't working (assuming there are no other issues).


Update your j:forEach to be j2:forEach and it should work.



Best regards,


James