一..SearchContainer的基本使用我就不讲了,这里主要讲讲RowChecker的使用,我们先看下效果
使用RowChecker的步骤
1.在<liferay-ui:search-container这个标签中添加属性 rowChecker="<%= new RowChecker(renderResponse) %>"
2.在<liferay-ui:search-container标签外添加<aui:form name="fm">包裹它.作用:为了后面取值
3.添加一个按钮用来获取多选框中已勾选的值,需要执行一段js,如下:
<aui:button cssClass="btn btn-danger" onClick='<%= renderResponse.getNamespace() + "delete();" %>' value="Delete" />
<aui:script>
Liferay.provide(
window,
'<portlet:namespace />delete',
function() {
var checkBoxValue = Liferay.Util.listCheckedExcept(document.<portlet:namespace />fm, "<portlet:namespace />allRowIds");
alert(checkBoxValue);
},
['liferay-util-list-fields']
);
</aui:script>
注意点:因为每一行的id是根据primarykey生成的,所以keyProperty="xxx"这个属性必须有,这个是service.xml中的实体id
<liferay-ui:search-container-row
className="com.skythinking.portlet.member.model.Member"
keyProperty="memberId" modelVar="member">
完整的view.jsp如下:
<%@page import="com.liferay.portal.kernel.dao.search.RowChecker"%> <%@page import="com.skythinking.portlet.member.service.MemberLocalServiceUtil"%> <%@page import="com.skythinking.portlet.member.service.MemberLocalService"%> <%@page import="com.liferay.portal.kernel.util.ListUtil"%> <%@page import="com.skythinking.portlet.member.service.persistence.MemberPersistenceImpl"%> <%@page import="com.skythinking.portlet.member.service.persistence.MemberPersistence"%> <%@page import="com.skythinking.portlet.member.model.Member"%> <%@ include file="/html/common/init.jsp"%> <%@ page contentType="text/html; charset=UTF-8"%> <aui:form name="fm"> <liferay-ui:search-container emptyResultsMessage="没有任何数据,请添加!" delta="5" rowChecker="<%= new RowChecker(renderResponse) %>" > <liferay-ui:search-container-results> <% List<Member> tempResults = MemberLocalServiceUtil .getMembers(renderRequest); results = ListUtil.subList(tempResults, searchContainer.getStart(), searchContainer.getEnd()); total = tempResults.size(); pageContext.setAttribute("results", results); pageContext.setAttribute("total", total); %> </liferay-ui:search-container-results> <!-- modelVar="member" 添加了这个属性,才可以在el表达式中获取member的各种属性 --> <liferay-ui:search-container-row className="com.skythinking.portlet.member.model.Member" keyProperty="memberId" modelVar="member"> <liferay-ui:search-container-column-text name="Id" property="memberId" /> <liferay-ui:search-container-column-text name="会员名" property="memberName" /> <liferay-ui:search-container-column-text name="联系电话" property="memberPhone" /> <liferay-ui:search-container-column-text name="创建时间" property="createDateFormat" /> <liferay-ui:search-container-column-text name="actions"> <portlet:actionURL var="deleteUrl" name="delete"> <portlet:param name="memberId" value="${member.memberId}" /> </portlet:actionURL> <liferay-ui:icon-delete image="delete" url="${deleteUrl}" /> </liferay-ui:search-container-column-text> </liferay-ui:search-container-row> <!-- 迭代标签 --> <liferay-ui:search-iterator /> </liferay-ui:search-container> </aui:form> <portlet:renderURL var="addUrl"> <portlet:param name="mvcPath" value="/html/member/edit.jsp" /> </portlet:renderURL> <aui:button cssClass="btn btn-primary" value="添加" href="${addUrl}" /> <aui:button cssClass="btn btn-danger" onClick='<%= renderResponse.getNamespace() + "delete();" %>' value="Delete" /> <aui:script> Liferay.provide( window, '<portlet:namespace />delete', function() { var checkBoxValue = Liferay.Util.listCheckedExcept(document.<portlet:namespace />fm, "<portlet:namespace />allRowIds"); alert(checkBoxValue); }, ['liferay-util-list-fields'] ); </aui:script>
如何进行表单的提交呢?请看下面的代码
<aui:button cssClass="btn btn-warning" value="编辑" onClick='<%= renderResponse.getNamespace() + "edit();" %>'/> <aui:button cssClass="btn btn-danger" value="删除" onClick='<%= renderResponse.getNamespace() + "delete();" %>'/> <aui:script> Liferay.provide( window, '<portlet:namespace />edit', function() { document.<portlet:namespace />fm.method = "post"; var ids = document.<portlet:namespace />fm.<portlet:namespace />rowIds.value = Liferay.Util.listCheckedExcept(document.<portlet:namespace />fm, '<portlet:namespace />allRowIds'); if (confirm(ids.length)>1){ alert(); } submitForm(document.<portlet:namespace />fm, '<portlet:actionURL name="editSelected"></portlet:actionURL>'); }, ['liferay-util-list-fields'] ); </aui:script> <aui:script> Liferay.provide( window, '<portlet:namespace />delete', function() { if (confirm('确定要删除吗?')) { document.<portlet:namespace />fm.method = "post"; document.<portlet:namespace />fm.<portlet:namespace />rowIds.value = Liferay.Util.listCheckedExcept(document.<portlet:namespace />fm, '<portlet:namespace />allRowIds'); submitForm(document.<portlet:namespace />fm, '<portlet:actionURL name="deleteSelected"></portlet:actionURL>'); } }, ['liferay-util-list-fields'] ); </aui:script>
public void editSelected(ActionRequest actionRequest, ActionResponse actionResponse) { long[] ids = ParamUtil.getLongValues(actionRequest, "rowIds"); for (int i = 0; i < ids.length; i++) { System.out.println("----编辑-----:" + ids[i]); } } public void deleteSelected(ActionRequest actionRequest, ActionResponse actionResponse) { long[] ids = ParamUtil.getLongValues(actionRequest, "rowIds"); for (int i = 0; i < ids.length; i++) { try { MemberLocalServiceUtil.deleteMember(ids[i]); } catch (Exception e) { e.printStackTrace(); } } }
代码下载地址:http://download.csdn.net/detail/u013310075/8387659
导入工程时,请选择导入到SDK目录下portlets目录下