Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplify default display one row when outputdata list is empty

Tags:

jsp

jstl

el

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

like image 970
bumbumpaw Avatar asked Jan 25 '16 03:01

bumbumpaw


2 Answers

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

like image 69
Ken Bekov Avatar answered Oct 20 '22 12:10

Ken Bekov


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>   
like image 38
Roman C Avatar answered Oct 20 '22 12:10

Roman C