Simplify default display one row when outputdata list is empty
Asked Answered
M

2

8
<table>
<c:if test="${output.list == nul}">
<tr><td><input type="text" /><select></select><input type="text" />
</td>
</tr>
</c:if>
<c:forEach var="iter" items="${output.list}">
<tr><td><input type="text" /><select></select><input type="text" value="${iter.getVal()}" />
</td>
</tr>
</c:forEach>
</tbody>
</table>

If my ${list} is empty,how can I display .clone row without duplicating codes or using javascript?

Motive answered 25/1, 2016 at 3:6 Comment(4)
would you like to show just empty row?Piddle
@KenBekov No,currently it display empty row but I want it to display the tr .cloneMotive
To show .clone you need iter anyway. Where from you going to get iter if your list is empty?Piddle
Do you have any idea? Just want to eliminate the hard coded js as mention above.Motive
P
2

I do not know whether I understood your problem. If you want output one row with all content, when list is empty, try next approach:

  <table>
        <c:forEach var="i" begin="0" end="${not empty list?(fn:length(list)-1):0}">
          <tr class="clone">
            <td>
               <input type="text" />
               <select></select>
               <input type="text" value="${list[i]!=null?list[i].getVal():''}" />
            </td>
          </tr>
        </c:forEach>
 </tbody>

For use fn: namespace just add in begin of your file <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

Udate: changed according question changes

Piddle answered 25/1, 2016 at 3:53 Comment(0)
U
1

If the list is empty then add an empty value to the list. You can do it in the servlet or JSP but in JSP you have to write additional java code to modify the list.

<table>
<c:set var="list" value="${output.list}"/>
<c:if test="${empty list && list != null}">
  ${list.add(null)} 
</c:if>
<c:forEach var="iter" items="${list}">
<tr><td><input type="text" /><select></select><input type="text" value="${iter.getVal()}" />
</td>
</tr>
</c:forEach>
</tbody>
</table>   
Unspeakable answered 29/1, 2016 at 9:7 Comment(2)
What if this is not the last place where list is used? What if the list will be used somewhere else? And there is no one expects that empty list contains null.Piddle
@KenBekov I don't see the empty list is used somewhere else. The list that is used in the JSP could be instantiated if it's empty.Unspeakable

© 2022 - 2024 — McMap. All rights reserved.