These are – roughly – the steps that you need to follow if you are planing to use the displaytag library in your portlet.
First of all, we are going to include the libraries in our portlet war. Download them from http://displaytag.sourceforge.net and include them in your WEB-INF/lib or, if you are using maven, include the dependencies in your pom.xml:
<dependency> <groupId>displaytag</groupId> <artifactId>displaytag</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>displaytag</groupId> <artifactId>displaytag-portlet</artifactId> <version>1.2</version> </dependency>
Using the Valuelist pattern we are going to implement the org.displaytag.pagination.PaginatedList interface, not without extending it first to adapt it to our needs, of course
like adding the PortletRequest to get access to the attributes in there.
public interface ExtendedPaginatedList extends PaginatedList { public static int DEFAULT_PAGE_SIZE = 10; /** * set results list * * @param resultList */ public void setList(List resultList); /** * Set the Total * * @param total */ public void setFullListSize(int total); /** * set the Page Size * * @param pageSize */ public void setObjectsPerPage(int pageSize); /** * Set the page number * * @param pageNumber */ public void setPageNumber(int pageNumber); /** * get the first record index */ public int getFirstRecordIndex(); /** * PortletRequest * * @return */ public PortletRequest getPortletRequest(); }
And the implementation:
public class ExtendedPaginatedListImpl implements ExtendedPaginatedList, Serializable { private static Log _log = LogFactory.getLog(ExtendedPaginatedListImpl.class); /** * Request params as constants. */ public static final String PAGE_NUMBER = "page"; public static final String SORT_DIRECTION = "dir"; public static final String SORT_FIELD = "sort"; public static final String SORT_ASC = "asc"; public static final String SORT_DESC = "desc"; /** * current page index, starts at 0 */ private int pageNumber; /** * number of results per page */ private int objectsPerPage; /** * total number of results */ private int fullListSize; /** * list for the current page */ private List list; /** * PortletRequest */ private PortletRequest portletRequest; /** * sort criteria */ private String sortCriterion; /** * default sorting order */ private SortOrderEnum sortDirection = SortOrderEnum.ASCENDING; /** * default constructor */ public ExtendedPaginatedListImpl() { } public ExtendedPaginatedListImpl(PortletRequest portletRequest, int pageSize) { this(SortOrderEnum.ASCENDING, portletRequest, pageSize); } public ExtendedPaginatedListImpl(SortOrderEnum sortDirection, PortletRequest portletRequest, int pageSize) { setPortletRequest(portletRequest); if (pageSize == 0) { setObjectsPerPage(DEFAULT_PAGE_SIZE); } else { setObjectsPerPage(pageSize); } int pageN = getParamPageNumber() == null ? 1 : Integer.parseInt(getParamPageNumber()); setPageNumber(pageN); this.sortCriterion = portletRequest.getParameter(SORT_FIELD); String sortDirRequested = StringUtils.trimToNull(portletRequest.getParameter(SORT_DIRECTION)); if ((sortDirRequested != null) && StringUtils.equals(sortDirRequested, SORT_ASC)) sortDirection = SortOrderEnum.ASCENDING; else if ((sortDirRequested != null) && StringUtils.equals(sortDirRequested, SORT_DESC)) sortDirection = SortOrderEnum.DESCENDING; this.sortDirection = sortDirection; if (_log.isDebugEnabled()) _log.debug(this.toString()); } public String getParamPageNumber() { String paramPageNumber = ""; if (portletRequest != null) { paramPageNumber = portletRequest.getParameter(PAGE_NUMBER) != null ? portletRequest.getParameter(PAGE_NUMBER) : ""; } return StringUtils.trimToNull(paramPageNumber); } public void setPortletRequest(PortletRequest portletRequest) { this.portletRequest = portletRequest; } @Override public PortletRequest getPortletRequest() { return this.portletRequest; } // More get and set methods
Now we can get the list of objects that you are planing to fill the table with and set it in your portlet request (or session in this case) to access it from the JSP:
@RenderMapping() public String view(RenderRequest renderRequest) throws PortalException, SystemException { Integer networkId = setAndGetNetworkId(renderRequest); setBehavioursList(renderRequest, networkId); return ViewKeys.MANAGEMENT_VIEW; } protected void setBehavioursList(PortletRequest portletRequest, Integer networkId) { ExtendedPaginatedList behavioursPaginatedList = paginatedListFactory.getPaginatedListInstance(portletRequest); Long total = behaviourManagerService.countBehaviours(DeletedType.no, networkId); behavioursPaginatedList.setFullListSize(total.intValue()); behavioursPaginatedList.setList( behaviourManagerService.getBehaviours( DeletedType.no, networkId, behavioursPaginatedList.getFirstRecordIndex(), behavioursPaginatedList.getObjectsPerPage())); portletRequest.getPortletSession().setAttribute(RequestKeys.BEHAVIOURS_LIST, behavioursPaginatedList, PortletSession.PORTLET_SCOPE); }
Include the tld in your JSP:
<%-- Displaytag --%> <%@ taglib uri="http://displaytag.sf.net" prefix="display" %>
And access the paginatedList in the session to fill in the table:
<liferay-portlet:renderURL var="viewListURL"/> <display:table id="behaviour" name="${portletSessionScope.paginatedBehaviourList}" requestURI="${viewListURL}" sort="external" htmlId="behavioursListTable"> <display:column property="idNetwork" title="Network"/> <display:column property="idBehaviour" title="Behaviour"/> <display:column property="name" title="Name"/> <display:column property="category.name" title="Category"/> <display:column property="client.name" title="Client"/> <display:column property="type.type" title="Type"/> <display:column title="Actions" autolink="true"> <a href='<portlet:renderURL windowState="MAXIMIZED"> <portlet:param name="action" value="UPDATE"/> <portlet:param name="idBehaviour" value="${behaviour.idBehaviour}"/> </portlet:renderURL>'> Update </a> </display:column> </display:table>
To customise your table you can add a displaytag.properties to your classpath:
factory.requestHelper=org.displaytag.portlet.PortletRequestHelperFactory paging.banner.placement=bottom css.table=taglib-search-iterator css.tr.even=portlet-section-body results-row css.tr.odd=portlet-section-alternate results-row alt css.th.sorted=sort-column css.th.ascending=sort-asc css.th.descending=sort-desc
Have a look at the configuration properties here: displaytag configuration
And also, customise the appearance including a displaytag.css:
div.exportlinks { background-color: #EEEEEE; border: 1px dotted #999999; margin: 2px 0 10px; padding: 2px 4px; width: 99%; } span.export { cursor: pointer; display: inline-block; padding: 0 4px 1px 20px; } span.excel { background-image: url(../img/ico_file_excel.png); } span.csv { background-image: url(../img/ico_file_csv.png); } span.xml { background-image: url(../img/ico_file_xml.png); } span.pdf { background-image: url(../img/ico_file_pdf.png); } span.rtf { background-image: url(../img/ico_file_rtf.png); } span.pagebanner { background-color: #EEEEEE; border-color: #999999 #999999 -moz-use-text-color; border-style: dotted dotted none; border-width: 1px 1px medium; display: block; margin-top: 10px; padding: 2px 4px; width: 99%; } span.pagelinks { background-color: #EEEEEE; border-color: -moz-use-text-color #999999 #999999; border-style: none dotted dotted; border-width: medium 1px 1px; display: block; margin-bottom: -5px; padding: 2px 4px; width: 99%; } .group-1 { border-top: 1px solid black; font-weight: bold; padding-bottom: 10px; } .group-2 { border-top: 1px solid black; font-style: italic; } .subtotal-sum, .grandtotal-sum { font-weight: bold; text-align: right; } .subtotal-header { border-top: 1px solid white; padding-bottom: 0; } .subtotal-label, .grandtotal-label { border-top: 1px solid white; font-weight: bold; } .grouped-table tr.even { background-color: #FFFFFF; } .grouped-table tr.odd { background-color: #FFFFFF; } .grandtotal-row { border-top: 2px solid black; }
If you are using Liferay you can include the css file in your portlet war and then, in the liferay-portlet.xml:
<portlet> <portlet-name>BehaviourManagementPortlet</portlet-name> <instanceable>true</instanceable> <private-request-attributes>false</private-request-attributes> <private-session-attributes>false</private-session-attributes> <ajaxable>true</ajaxable> <header-portlet-css>/css/displaytag.css</header-portlet-css> </portlet>
Popularity: 100% [?]

