How to add an attribute with JSTL under one condition?

0
<div class="body">
   <selected name ="multSelectSkill" id="optgroup" class="ms" multiple="multiple">
    <optgroup label="GRUPO 1">
       <c:forEach items="${skillsOperador}" var="skillsOperador">
          <option value="${skillsOperador.id}" selected="${skillsOperador.operadorSkill.statusSkill == 'Y'}>${skillsOperador.nome</option>

Is it possible to use the attribute in this way? Because it is not working; you are selecting all values. I need to select only the values that have statusSkill equal to Y ;

    
asked by anonymous 09.08.2017 / 14:30

3 answers

2

Use a ternary to check the condition whether or not it writes the selected , in the way that% is always added selected="false" causing all elements to have this attribute.

<option value="${skillsOperador.id}"  ${skillOperador.operadorSkill.statusSkill == 'Y' ? 'selected="selected"' : ''}>${skillsOperador.nome}</option>

Better visualization:

<option value="${skillsOperador.id}"
   ${skillOperador.operadorSkill.statusSkill == 'Y'
   ?
      'selected="selected"'
   : 
      ''
   }>
${skillsOperador.nome}</option>
    
09.08.2017 / 14:43
2

Expensive, good night.

First of all, thank you for the help. I was able to solve the problem.

Basically, I check first those who value having the "Y" key and if it exists, I already give a 'selected' in them. If not, I'll set the default.

Many thanks to all.

<select name="multSelectSkill" id="optgroup" class="ms" multiple="multiple">
   <optgroup label="GRUPO1">
   <c:forEach items="${skillsOperador }" var="skillsOperador">
      <c:choose>
         <c:when test="${skillsOperador.operadorSkill.statusSkill eq 'Y' }">
            <option value="${skillsOperador.id }" 
            selected="${skillsOperadorPorto.id }">${skillsOperadorPorto.nome }
            </option>
         </c:when>
         <c:otherwise>
            <option value="${skillsOperador.id }">${skillsOperador.nome}
            </option>
         </c:otherwise>
      </c:choose>
   </c:forEach> 
   </optgroup>
    
10.08.2017 / 04:35
1

I do not think expressions are accepted.

Do this check before sending the data by HTML, then you already bring the variable with the value ready. And it needs to "select" or "" for you to write the attribute itself, because if it is present in the option, regardless of the value, the element will be selected.

eg.

<option value="${skillsOperador.id}" ${skillsOperador.operadorSkill.attrSelected} >

being the value of skillsOperator.operatorSkill.attrSelected or "selected" or "".

    
09.08.2017 / 14:48