原始数据实时滚动不工作与惰性加载 [英] Primefaces datatable live scrolling not working with lazy loading
问题描述
参考此处中提供的建议,我尝试使用实时滚动来实现延迟加载来处理大型数据集,但是当使用datatable的行和 scrollRows 属性时,不会发生实时滚动。如果我删除行属性,则不会记录任何记录显示。这是我的代码片段,我试过。有人请帮助我,如果我做错了。
JSF代码片段
< p:dataTable id =arcRecListvar =data
value =#{archivedRecordBean.archModel}lazy =true
tableStyle =table-layout:auto; width:80%; styleClass =datatable
scrollable =truescrollWidth =84%scrollHeight =81%
columnClasses =columnwidthliveScroll =truescrollRows =20
filteredValue =#{archivedRecordBean.filteredArchiveItems}
resizableColumns =truerows =20>
< p:column headerText =InsuredfilterBy =#{data.insuredName}
sortBy =#{data.insuredName}filterMatchMode =contains
style =width:15%; white-space:pre-line; escape =false
filterStyle =width:80%!important; margin-top:25px;
sortFunction =#{archivedRecordBean.sortColumn}>
< h:outputText value =#{data.insuredName}/>
<! - style =width:250px - >
< / p:column>
。
。
< / p:dataTable>
托管bean
code> @ManagedBean
@ViewScoped
public class ArchivedRecordBean实现Serializable {
private List< LPINFO> archiveItems = null;
private List< LPINFO> filteredArchiveItems;
private LPINFO objLPINFO = null;
JdbcConnection jdbcConnection = null;
连接连接= null;
语句selectStmt = null;
ResultSet rs = null;
private transient LazyDataModel< LPINFO> archModel;
public ArchivedRecordBean()
{
getArchiveFields();
}
@PostConstruct
public void init()
{
archModel = new LazyArchiveDataModel(archiveItems);
}
public List< LPINFO> getArchiveItems(){
System.out.println(inside getter);
return archiveItems;
}
public void setArchiveItems(List< LPINFO> archiveItems){
this.archiveItems = archiveItems;
}
public LPINFO getObjLPINFO(){
return objLPINFO;
}
public void setObjLPINFO(LPINFO objLPINFO){
this.objLPINFO = objLPINFO;
}
public List< LPINFO> getFilteredArchiveItems(){
return filteredArchiveItems;
}
public void setFilteredArchiveItems(List< LPINFO> filteredArchiveItems){
this.filteredArchiveItems = filteredArchiveItems;
}
public LazyDataModel< LPINFO> getArchModel(){
return archModel;
}
public void setArchModel(LazyDataModel< LPINFO> archModel){
this.archModel = archModel;
}
public void getArchiveFields()
{
System.out.println(inside getArchiveFields);
ArchiveRecordsDao daoObject = new ArchiveRecordsDao();
archiveItems = daoObject.getArchiveRecords();
}
}
/ pre>
DAO类
public class ArchiveRecordsDao {
JdbcConnection con = null;
连接连接;
语句selectStmt;
public ResultSet rs = null;
私人列表< LPINFO> archiveItems = null;
public LPINFO objWorkSpaceItem = null;
public List< LPINFO> getArchiveRecords()
{
try
{
con = new JdbcConnection();
connection = con.getJdbcConnection();
selectStmt = connect.createStatement();
String query =select * from LPINFO where LPINFO.ClearDate<(select TOP 1 Tbl_CurrentYear.CurrentYear from dbo.Tbl_CurrentYear)
+AND(LPINFO.ClearDate is not null);
rs = selectStmt.executeQuery(query);
archiveItems = new ArrayList< LPINFO>();
while(rs.next())
{
objWorkSpaceItem = new LPINFO();
objWorkSpaceItem.setInsuredName(rs.getString(InsuredName));
。
。
。
archiveItems.add(objWorkSpaceItem);
}
}
catch(异常e)
{
e.printStackTrace();
}
finally
{
try {
connection.close();
} catch(SQLException e){
e.printStackTrace();
}
}
return archiveItems;
}
}
LazyDataModel类
public class LazyArchiveDataModel extends LazyDataModel< LPINFO> {
私人列表< LPINFO>数据源;
public LazyArchiveDataModel(List< LPINFO> datasource){
this.datasource = datasource;
}
@Override
public void setRowIndex(int rowIndex){
/ *
*以下是在祖先(LazyDataModel)中:
* this.rowIndex = rowIndex == -1? rowIndex:(rowIndex%pageSize);
* /
if(rowIndex == -1 || getPageSize()== 0){
super.setRowIndex(-1);
}
else
super.setRowIndex(rowIndex%getPageSize());
}
@Override
public LPINFO getRowData(String rowKey){
for(LPINFO lpinfo:datasource){
if(lpinfo.getLPID() .equals(rowKey))
return lpinfo;
}
返回null;
}
@Override
public Object getRowKey(LPINFO lpinfo){
return lpinfo.getLPID();
}
@Override
public List< LPINFO> load(int first,int pageSize,String sortField,SortOrder sortOrder,Map< String,String> filters){
列表< LPINFO> data = new ArrayList< LPINFO>();
// filter
for(LPINFO lpinfo:datasource){
boolean match = true;
for(Iterator< String> it = filters.keySet()。iterator(); it.hasNext();){
try {
String filterProperty = it.next ();
String filterValue = filters.get(filterProperty);
String fieldValue = String.valueOf(lpinfo.getClass()。getField(filterProperty).get(lpinfo));
if(filterValue == null || fieldValue.startsWith(filterValue)){
match = true;
}
else {
match = false;
break;
}
} catch(Exception e){
match = false;
}
}
if(match){
data.add(lpinfo);
}
}
// sort
/ * if(sortField!= null){
Collections.sort(data,new LazySorter(sortField,排序));
} * /
// rowCount
int dataSize = data.size();
this.setRowCount(dataSize);
// paginate
if(dataSize> pageSize){
try {
return data.subList(first,first + pageSize);
}
catch(IndexOutOfBoundsException e){
return data.subList(first,first +(dataSize%pageSize));
}
}
else {
返回数据;
}
}
}
解决方案据了解,使用行的文档将与scrollRows冲突。
以下是有关这两个属性的PrimeFaces文档:
行:(整数)每个页面显示的行数(分页)
scrollRows :(整数)活动滚动滚动)
现在这里的问题不仅仅是行,我认为这是
scrollWidth =84% scrollHeight =81%
作为文档,这两个属性是整数,所以你不会看到任何记录因为scrollHeight被定义为错误。
scrollHeight :(整数)滚动视口高度。 em>
scrollWi dth (整数)滚动视口宽度。
尝试将它们设置为固定的整数像素,如500
希望这有帮助。
With reference to the suggestion given in the post here i tried implementing lazy loading with live scrolling to handle large data sets,but live scrolling doesn't happen when both rows and scrollRows attributes of datatable are used.If i remove rows attribute then no records would be displayed.Here is my code snippet that i tried.Can someone please help me out if i am doing anything wrong.
JSF code snippet
<p:dataTable id="arcRecList" var="data" value="#{archivedRecordBean.archModel}" lazy="true" tableStyle="table-layout:auto; width:80%;" styleClass="datatable" scrollable="true" scrollWidth="84%" scrollHeight="81%" columnClasses="columnwidth" liveScroll="true" scrollRows="20" filteredValue="#{archivedRecordBean.filteredArchiveItems}" resizableColumns="true" rows="20"> <p:column headerText="Insured" filterBy="#{data.insuredName}" sortBy="#{data.insuredName}" filterMatchMode="contains" style="width:15%;white-space:pre-line;" escape="false" filterStyle="width:80% !important; margin-top:25px;" sortFunction="#{archivedRecordBean.sortColumn}"> <h:outputText value="#{data.insuredName}" /> <!-- style="width:250px" --> </p:column> . . . </p:dataTable>
Managed bean
@ManagedBean @ViewScoped public class ArchivedRecordBean implements Serializable { private List<LPINFO> archiveItems=null; private List<LPINFO>filteredArchiveItems; private LPINFO objLPINFO=null; JdbcConnection jdbcConnection=null; Connection connection=null; Statement selectStmt=null; ResultSet rs=null; private transient LazyDataModel<LPINFO> archModel; public ArchivedRecordBean() { getArchiveFields(); } @PostConstruct public void init() { archModel=new LazyArchiveDataModel(archiveItems); } public List<LPINFO> getArchiveItems() { System.out.println("inside getter"); return archiveItems; } public void setArchiveItems(List<LPINFO> archiveItems) { this.archiveItems = archiveItems; } public LPINFO getObjLPINFO() { return objLPINFO; } public void setObjLPINFO(LPINFO objLPINFO) { this.objLPINFO = objLPINFO; } public List<LPINFO> getFilteredArchiveItems() { return filteredArchiveItems; } public void setFilteredArchiveItems(List<LPINFO> filteredArchiveItems) { this.filteredArchiveItems = filteredArchiveItems; } public LazyDataModel<LPINFO> getArchModel() { return archModel; } public void setArchModel(LazyDataModel<LPINFO> archModel) { this.archModel = archModel; } public void getArchiveFields() { System.out.println("inside getArchiveFields"); ArchiveRecordsDao daoObject=new ArchiveRecordsDao(); archiveItems=daoObject.getArchiveRecords(); } }
DAO class
public class ArchiveRecordsDao { JdbcConnection con = null; Connection connection; Statement selectStmt; public ResultSet rs = null; private List<LPINFO> archiveItems = null; public LPINFO objWorkSpaceItem = null; public List<LPINFO> getArchiveRecords() { try { con=new JdbcConnection(); connection=con.getJdbcConnection(); selectStmt=connection.createStatement(); String query="select * from LPINFO where LPINFO.ClearDate < (select TOP 1 Tbl_CurrentYear.CurrentYear from dbo.Tbl_CurrentYear)" +"AND (LPINFO.ClearDate is not null)"; rs=selectStmt.executeQuery(query); archiveItems=new ArrayList<LPINFO>(); while(rs.next()) { objWorkSpaceItem=new LPINFO(); objWorkSpaceItem.setInsuredName(rs.getString("InsuredName")); . . . archiveItems.add(objWorkSpaceItem); } } catch(Exception e) { e.printStackTrace(); } finally { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } return archiveItems; }
}
LazyDataModel class
public class LazyArchiveDataModel extends LazyDataModel<LPINFO> { private List<LPINFO> datasource; public LazyArchiveDataModel(List<LPINFO> datasource) { this.datasource = datasource; } @Override public void setRowIndex(int rowIndex) { /* * The following is in ancestor (LazyDataModel): * this.rowIndex = rowIndex == -1 ? rowIndex : (rowIndex % pageSize); */ if (rowIndex == -1 || getPageSize() == 0) { super.setRowIndex(-1); } else super.setRowIndex(rowIndex % getPageSize()); } @Override public LPINFO getRowData(String rowKey) { for(LPINFO lpinfo : datasource) { if(lpinfo.getLPID().equals(rowKey)) return lpinfo; } return null; } @Override public Object getRowKey(LPINFO lpinfo) { return lpinfo.getLPID(); } @Override public List<LPINFO> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String,String> filters) { List<LPINFO> data = new ArrayList<LPINFO>(); //filter for(LPINFO lpinfo : datasource) { boolean match = true; for(Iterator<String> it = filters.keySet().iterator(); it.hasNext();) { try { String filterProperty = it.next(); String filterValue = filters.get(filterProperty); String fieldValue = String.valueOf(lpinfo.getClass().getField(filterProperty).get(lpinfo)); if(filterValue == null || fieldValue.startsWith(filterValue)) { match = true; } else { match = false; break; } } catch(Exception e) { match = false; } } if(match) { data.add(lpinfo); } } //sort /*if(sortField != null) { Collections.sort(data, new LazySorter(sortField, sortOrder)); }*/ //rowCount int dataSize = data.size(); this.setRowCount(dataSize); //paginate if(dataSize > pageSize) { try { return data.subList(first, first + pageSize); } catch(IndexOutOfBoundsException e) { return data.subList(first, first + (dataSize % pageSize)); } } else { return data; } } }
解决方案as I understand the documentation using rows would conflict with scrollRows.
Here's the PrimeFaces documentation about these two attributes:
rows: (Integer) Number of rows to display per page. (Pagination)
scrollRows: (Integer) Number of rows to load on live scroll. (Scroll)
Now the problem here is not only the rows, I think it's
scrollWidth="84%" scrollHeight="81%"
As the documentation, these two attributes are Integer(s), so you won't see any records infact because the scrollHeight is defined wrong.
scrollHeight: (Integer) Scroll viewport height.
scrollWidth (Integer) Scroll viewport width.
Try to set them into fixed integer pixels, like 500.
Hope this helps.
这篇关于原始数据实时滚动不工作与惰性加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!