问题
I have the list with very data. I want to show data in Primeface data table with lazy loading ability. now I show data normal in data Table, but it is slow. How can use lazy loading ability?
XHTML File :
<ui:composition template="template.xhtml">
<ui:define name="content">
<p:panel id="pnlLogInformationPage">
<h:form id="logInformation">
<div class="contentContainer-full-left">
<p:dataTable var="log" value="#{logInformationMB.logInformationList}" id="logTable"
width="100%" liveResize="true">
<p:column headerText="ID" sortBy="Id">
<h:outputText value="#{logInformation.Id}" />
</p:column>
<p:column headerText="Name" sortBy="Name">
<h:outputText value="#{logInformation.Name}" />
</p:column>
</p:dataTable>
</div>
</h:form>
</p:panel>
</ui:define>
ManageBean File:
@ManagedBean(name = "logInformationMB")
@ViewScoped
public class LogManagedBean implements Serializable {
@PostConstruct
public void initComponents() {
loadLogInformation();
}
public List<LogInformationDTO> getLogInformationList() {
return logInformationList;
}
public void setLogInformationList(final List<LogInformationDTO> pLogInformationList) {
logInformationList = pLogInformationList;
}
public void loadLoagInformation(final ComponentSystemEvent event) {
setLogLInformationlist(getLogInformationList();
}
public void loadInformationProtokolle() {
loadInformationProtokolle(null);
}
public List<LogInformationDTO> getLogInformation() {
final List<LogInformationDTO> lcResult = new ArrayList<LogInformationDTO>();
....
return lcResult;
}
}
回答1:
According to your code, you didn't use lazyloading. you just get all your datas. It will be slow when your data is too big.
See my steps below:
1- Instead of using #{logInformationMB.logInformationList}, you need to create new class which extend LazyDataModel class and create a service that can get data page by page.
public class LogInformationDataModel extends LazyDataModel<LogInformationDTO> {
private List<LogInformationDTO> logInformationList;
private LogInformationService logInformationService = new LogInformationService();
@Override
public List<LogInformationDTO> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
// TODO implement sort
setRowCount(logInformationService.count(filters));
logInformationList = logInformationService.list(first / pageSize, pageSize, sortField, sort, filters);
return logInformationList;
}
@Override
public LogInformationDTO getRowData(String rowKey) {
for(LogInformationDTO logInformation : logInformationList) {
if(logInformation.getId().equals(rowKey))
return logInformation;
}
return null;
}
@Override
public Object getRowKey(LogInformationDTO logInformation) {
return logInformation.getId();
}
}
2- Register data model in your Manage bean
@ManagedBean(name = "logInformationMB")
@ViewScoped
public class LogManagedBean implements Serializable {
private LogInformationDataModel dataModel = new LogInformationDataModel();
public LogInformationDataModel getDataModel() {
return dataModel;
}
}
3- Add lazy attribe (lazy="true") to your p:dataTable and using pagination
<p:dataTable var="log" value="#{logInformationMB.dataModel}" var="logInformation" id="logTable"
width="100%"
lazy="true"
paginator="true"
paginatorPosition="bottom"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="10,50,100"
>
<p:column headerText="ID" sortBy="Id">
<h:outputText value="#{logInformation.Id}" />
</p:column>
<p:column headerText="Name" sortBy="Name">
<h:outputText value="#{logInformation.Name}" />
</p:column>
</p:dataTable>
I Hope my answer could solve your issue.
Below is the link you can check :
Primefaces lazydatamodel
来源:https://stackoverflow.com/questions/42069206/use-list-for-source-in-datatable-with-lazyloading