我正在使用Primefaces 3.5和JSF 2.1。我需要针对每一行显示复选框以将其标记为删除,例如this ShowCase。那个ShowCase使用ListDataModel
,我不是。我正在使用LazyDataModel
来启用排序(我不确定是否可以使用ListDataModel
进行排序。是否可以?)。
通过我的尝试,会显示复选框,但无法检查它们。
JSF页面:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>State</title>
</h:head>
<h:body>
<h:form id="form">
<p:dataTable var="state" value="#{stateManagedBean}" id="stateTable" lazy="true" paginator="true" rows="10" rowKey="#{state.stateId}" rowsPerPageTemplate="5,10,15" selectionMode="multiple" selection="#{stateManagedBean.selectedStates}">
<f:facet name="header">
Data
</f:facet>
<p:ajax event="rowSelectCheckbox" listener="#{stateManagedBean.check}"/>
<p:column selectionMode="multiple" style="width:2%" />
<p:column headerText="id" sortBy="#{state.stateId}" filterBy="#{state.stateId}">
<h:outputText value="#{state.stateId}" />
</p:column>
<p:column headerText="State" sortBy="#{state.stateName}" filterBy="#{state.stateName}">
<h:outputText value="#{state.stateName}" />
</p:column>
<p:column headerText="Country" sortBy="#{state.countryId}" filterBy="#{state.countryId.countryName}">
<h:outputText value="#{state.countryId.countryName}" />
</p:column>
<f:facet name="footer">
<p:commandButton id="multiViewButton" value="View" icon="ui-icon-search" update=":form:displayMulti" oncomplete="multiCarDialog.show()"/>
</f:facet>
</p:dataTable>
<p:dialog id="multiDialog" header="State Details" resizable="false" widgetVar="multiCarDialog" height="300" showEffect="fade" hideEffect="explode">
<p:dataList id="displayMulti" value="#{stateManagedBean.selectedStates}" var="selectedState">
Id: #{selectedState.stateId}, State: #{selectedState.stateName}
</p:dataList>
</p:dialog>
</h:form>
</h:body>
</html>
JSF管理的bean:
package admin.mangedbean;
import admin.dao.service.StateService;
import java.util.List;
import java.util.Map;
import org.springframework.context.annotation.Scope;
import model.StateTable;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SelectableDataModel;
import org.primefaces.model.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Controller
@Scope("request")
public final class StateManagedBean extends LazyDataModel<StateTable> //implements SelectableDataModel<StateTable>
{
@Autowired
private final StateService stateService=null;
private List<StateTable>list;
private StateTable[] selectedStates;
public StateManagedBean()
{
}
@Override
public StateTable getRowData(String rowKey)
{
for(StateTable stateTable:list)
{
if(stateTable.getStateId().toString().equals(rowKey))
{
return stateTable;
}
}
return null;
}
@Override
public Object getRowKey(StateTable stateTable)
{
return stateTable.getStateId();
}
public StateTable[] getSelectedStates()
{
return selectedStates;
}
public void setSelectedStates(StateTable[] selectedStates)
{
this.selectedStates = selectedStates;
}
@Override
public List<StateTable> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters)
{
setRowCount(stateService.rowCount().intValue());
return stateService.getList(first, pageSize, sortOrder, sortField);
}
public void check(SelectEvent event)
{
System.out.println("checked");
}
}
我希望它可以使用这个AJAX事件,
<p:ajax event="rowSelectCheckbox" listener="#{stateManagedBean.check}"/>
哪个侦听器绑定到托管bean内的check()
方法。单击复选框时无法选中复选框。在我的情况下显示的对话框是完全没有必要的,因为应该通过选择要删除的相应行的复选框来标记要删除的行。是否可以使用LazyDataModel
?