drop-down-menuliferayportletliferay-7.3

Dynamic select list in a Liferay MVCPortlet


You can create a select list with static options in Liferay MVCPortlet JSP page like this:

<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<aui:form>
 <aui:select name="items">
  <aui:option value="item1">Item1</aui:option>
  <aui:option value="item2">Item2</aui:option>
 </aui:select> 
</aui:form>

What is the recommended way of creating the options dynamically for a list of objects stored in portlet session?


Solution

  • Use a foreach tag: https://www.tutorialspoint.com/jsp/jstl_core_foreach_tag.htm

    <%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
    <%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
    <aui:form>
     <aui:select name="items">
       <c:forEach items="<%=yourList%>" var="yourlistItem">
        <aui:option value="${yourlistItem.value}">${yourlistItem.name}</aui:option>
       </c:forEach>
     </aui:select> 
    </aui:form>