Friday, 22 August 2014

Liferay Grid



View.jsp :
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

<portlet:defineObjects />

This is the <b>NewPortlet</b> portlet in View mode.

<portlet:actionURL var="actionURL"></portlet:actionURL>

<a href="<%=actionURL %>" >view grid</a>
Grid.jsp:


Description:

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ page import="javax.portlet.PortletURL" %>
<%@ page import="org.apache.commons.collections.CollectionUtils" %>
<portlet:defineObjects />


<portlet:renderURL var="renderURL"></portlet:renderURL>

<a href="<%=renderURL %>" >Back to Home</a>

<%@ page import="java.util.*" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui"%>



<%
//creating Iterator URL.

PortletURL iteratorURL = renderResponse.createRenderURL();
iteratorURL.setParameter("jspPage", "/html/newportlet/grid.jsp");
%>

<%!
//Creating Global List Object

List list =new ArrayList();
%>
<%
//getting List object from Render Request Object
List originalList = (List)renderRequest.getAttribute("list");

System.out.println("originalList"+originalList);

//copying list into global list object
if(originalList!=null ){
      list= new ArrayList(originalList.size());
      for(Object b: originalList){
            list.add(b);
      }
}



%>


<liferay-ui:search-container delta="3" emptyResultsMessage="No Records" iteratorURL="<%=iteratorURL %>" deltaConfigurable="true" >
<%
//creating Sub list
List displayList= null;
if(list!=null && list.size()>0 ){
      displayList =list.subList(searchContainer.getStart(), list.size());
}

System.out.println(searchContainer.getEnd());
System.out.println(list.size());
if( searchContainer.getEnd() < list.size()){

      displayList=list.subList(searchContainer.getStart(), searchContainer.getEnd());

}

%>


<liferay-ui:search-container-results results="<%=displayList%>" total="<%=list.size()%>" >

</liferay-ui:search-container-results>



<liferay-ui:search-container-row className="com.test.Book" modelVar="book" >

<liferay-ui:search-container-column-text value="<%= book.getBookTitle() %>" name="Book Title" ></liferay-ui:search-container-column-text>
<liferay-ui:search-container-column-text value="<%= book.getAuthor()  %>" name="Author"></liferay-ui:search-container-column-text>



</liferay-ui:search-container-row >

<liferay-ui:search-iterator />


</liferay-ui:search-container>

Description:
1. Create iterator URL for same page. if we want to use pagination, we need this url. Based on this url, the paginator will know where to display further results.
2.  Get the List object from Action class.
3. Copy that list into Global List Object.
Note:
This is not the best way. But while using pagination, we are facing problems. That is we are not getting list object from Action class while click on next.  To fix that problem temporarily we followed this way.
4. Write the tag <liferay-ui:search-container>. It will create the searchContainer Object by supplying tag attributes and values to the SearchContainer class. After writing this tag, we can use searchContainer object in scriptlets.
Attributes:
1. delta: default no. of results per page.
2: emptyResultsMessage: To display default message when the list object is emty.
3. iteratorURL: On which we want to display further results.
4. deltaConfigurable: To select items per page dynamically
5. Create the sublist which will be displayed based on items per page. Write proper logic to create sub list. Because if the remaining object is samller than the items per page we will get errors.
Ex:
<%

List displayList= null;
if(list!=null && list.size()>0 ){
      displayList =list.subList(searchContainer.getStart(), list.size());
}

System.out.println(searchContainer.getEnd());
System.out.println(list.size());
if( searchContainer.getEnd() < list.size()){

      displayList=list.subList(searchContainer.getStart(), searchContainer.getEnd());

}

%>

6. Write the <liferay-ui:search-container-results> tag and supply subList and Total list size.
Ex:
<liferay-ui:search-container-results results="<%=displayList%>" total="<%=list.size()%>" >

</liferay-ui:search-container-results>

7. Write the Tag <liferay-ui:search-container-row> and Supply the model object name  and give it variable name. We can access property values by using this variable.  This tag will create a row of search container.
Ex: <liferay-ui:search-container-row className="com.test.Book" modelVar="book"
<liferay-ui:search-container-column-text/>
</liferay-ui:search-container->
className is the class name of the objects which are stored in the list object
8. Set the name and value attribute values for <liferay-ui:search-container-column-text/>.
Where name is column name of the Grid and value is result of that attribute.
For Ex:

<liferay-ui:search-container-column-text value="<%= book.getBookTitle() %>" name="Book Title" ></liferay-ui:search-container-column-text>
<liferay-ui:search-container-column-text value="<%= book.getAuthor()  %>" name="Author"></liferay-ui:search-container-column-text>

The above code displays two Columns in a row.
We are calling the methods of Model class using modelVar attribute values we set in the <liferay-ui:search-container-row> tag
Note:
If you want to display any datatype other than String in the column, Please use toString() method. Otherwise you will get Error
9. The Tag <liferay-ui:search-iterator /> takes care of iterating the list object until all the objects are displayed.

No comments:

Post a Comment