How to add a JSTL attribute under one condition?

Asked

Viewed 164 times

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 this way? Because it’s not working; it’s selecting all the values. I need to select only the values that have the statusSkill equal to Y;

  • 1

    What language is that? Another thing: try not to put code prints in your questions. Instead, paste the code itself. This makes it easier for people who use the site search.

  • 1

    I am using JSTL in html.

  • @Kennedyanderson Tag appropriate to the technology you are using in the question. So you’ll have a better chance of getting her to someone who knows the answer.

  • Thanks for your help, I’m making the necessary changes.

  • How much sense does it make to define the value of var equal to items on the tag forEach? Wouldn’t that override the variable? Try doing something like: ${var.statusKill == 'Y' ? "selected" : ""}

  • The name of var does not interfere because it works on other pages, such as "create operator". But I can test.

Show 1 more comment

3 answers

2


Use a ternary to check the condition make him write or not the selected, the way it 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>

Best view:

<option value="${skillsOperador.id}"
   ${skillOperador.operadorSkill.statusSkill == 'Y'
   ?
      'selected="selected"'
   : 
      ''
   }>
${skillsOperador.nome}</option>
  • It is even without the "value" ?

  • @Kennedyanderson has value yes, I omitted it pq copy the code of the posted image.

  • Right. Tested like this : <option value="${skillsOperator.id}" Selected="${skillsOperator.operatorsSkill.statusSkill == 'Y' ? 'Selected='Selected' : ' '}"> But it didn’t work and when I update the page a few times, it shows error 500. /pages/register/Alteraroperador.jsp (line: [241], column: [86]) Unterminated [${] tag

  • @Kennedyanderson you can’t leave the selected fixed in html

  • Hello @rray, good night! I tried to use this way, but returns error when accessing the operator to change. Stacktrace:] with root cause org.apache.el.parser.Parseexception: Encountered " "?" """ at line 1, column 51.

  • In the code you suggested I added an apostrophe to Selected. 'Selected'="Selected" .

  • @Kennedyanderson truth lacked close a single quote even.

  • how do I close this question ?

  • @Kennedyanderson mark with the green sign the answer that solved your problem.

Show 4 more comments

2

Good evening.

First thanks for the help. I managed to solve the problem.

Basically I check first those that value that have the key "Y" and if it exists, I already give a 'Selected' in them. If not, I list as default.

Thank you all very much.

<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>

1

I believe that expressions are not accepted.

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

eg.

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

the value of skillsOperator.operatorSkill.attrSelected or "Selected" or "".

  • I saw here that expressions are accepted yes, use a ternary then.

  • Got it, I’ll check then. Thanks for the help.

  • I still can’t get through the ternary =\

  • The code looks like this: <option value="${skillsOperadorPort.id } ${skillsOperadorPort.operatorsSkill.statusSkill eq 'Y' ? 'Selected' : ''}"> Only it is not selecting values that have "Y" as attribute.

  • You can do this check in Java itself, instead of using the variable statusSkill you can bring another one where there is already the value "Selected" or "" instead of 'Y' or 'N'. Hence avoid using the JSTL ternary.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.