Menu

Using if statement with foreach

2014-04-14
2021-05-03
  • Mark Vejvoda

    Mark Vejvoda - 2014-04-14

    I would like to use the <jt:if> tag to check if a collection if empty, and if so output a blank cell (so that cell formatting is kept) otherwise output the value from the collection. Something like this pseudo code:

    <jt:forEach items="${dates}" var="date" copyRight="true">
      <jt:if test="${myItems.getDataFor(date).size()==0}" then=" " else="<jt:forEach items=\"${myItems.getDataFor(date)}\" var=\"mydata\">${mydata.text}</jt:forEach>\" />
    </jt:forEach>
    

    I don't know how to use the if tag and then in a condition to loop through a collection? Is this possible or is there some other way?

     

    Last edit: Mark Vejvoda 2014-04-14
  • Randy Gettman

    Randy Gettman - 2014-04-22

    I would use the form of the jt:if tag that takes a body. The body of an if tag, if present, is like the then attribute of a bodiless if tag. The elseAction attribute is valid for if tags with a body, and "clear" seems to do what you want, i.e. clear the cell without shifting, leaving formatting in place.

    Try the following:

    <jt:if test="${myItems.getDataFor(date).size() != 0}" elseAction="clear">
      <jt:forEach items="${myItems.getDataFor(date)}" var="mydata">${mydata.text}</jt:forEach>
    </jt:if>
    

    That should all be in one cell. It's a lot for one cell, but that should work.

     
  • Mark Vejvoda

    Mark Vejvoda - 2014-06-12

    Yes works great, thanks. The clear keeps cell formatting which is perfect.

     
  • thrishala

    thrishala - 2021-04-29

    <jt:if test="${header.accountExecutives.size() != 0}" elseaction="clear">
    <jt:foreach items="${header.accountExecutives}" var="ae">${ae.name}</jt:foreach>
    </jt:if>
    When I try as above approach , it gives me java.lang.NullPointerException: null
    at org.apache.commons.jexl2.Interpreter.sizeOf(Interpreter.java:1474) ~[commons-jexl-2.1.1.jar:2.1.1]
    at org.apache.commons.jexl2.Interpreter.visit(Interpreter.java:1398) ~[commons-jexl-2.1.1.jar:2.1.1]
    at org.apache.commons.jexl2.parser.ASTSizeMethod.jjtAccept(ASTSizeMethod.java:18) ~[commons-jexl-2.1.1.jar:2.1.1]
    at org.apache.commons.jexl2.Interpreter.visit(Interpreter.java:1317) ~[commons-jexl-2.1.1.jar:2.1.1]
    at org.apache.commons.jexl2.parser.ASTReference.jjtAccept(ASTReference.java:18) ~[commons-jexl-2.1.1.jar:2.1.1]
    at org.apache.commons.jexl2.Interpreter.visit(Interpreter.java:1195) ~[commons-jexl-2.1.1.jar:2.1.1]
    What is the wrong with this?

     

    Last edit: thrishala 2021-04-29
  • Etienne Canaud

    Etienne Canaud - 2021-04-30

    Maybe the expression on which you call .size() evaluates to null.

    Can you share the expressions you're using in your jexl tags?

     
    • thrishala

      thrishala - 2021-05-03

      Thanks for replying. Yes you are correct .Finally myself identified the issue which is with the the data set ,I have used .

       

      Last edit: thrishala 2021-05-03

Log in to post a comment.