java Richfaces 3 如何让 datascroller 做真正的分页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14203379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Richfaces 3 how to make datascroller do true pagination
提问by Catfish
I'm trying to figure out how to do true pagination with richfaces datascroller.
我试图弄清楚如何使用richfaces datascroller 进行真正的分页。
Currently my query pulls all of the data from a table and i want it so when you click the next button of the datascroller, it will query the db for the next 20 rows.
目前,我的查询从表中提取所有数据,我想要它,因此当您单击数据滚动条的下一个按钮时,它将查询数据库中接下来的 20 行。
I understand how to write the query to limit the rows, but i'm not sure how to get the datascroller's next button to call a specific method.
我了解如何编写查询来限制行,但我不确定如何让数据滚动条的下一步按钮调用特定方法。
I see there are many people posting on the Richfaces forums with potential solutions, but they all use a dataModel. Since i'm not using an ORM, i'm not sure how i can fit these solutions to what i have.
我看到很多人在 Richfaces 论坛上发布了潜在的解决方案,但他们都使用 dataModel。由于我没有使用 ORM,因此我不确定如何将这些解决方案应用于我所拥有的。
Any help is appreciated.
任何帮助表示赞赏。
<rich:extendedDataTable id="resultsTable" value="#{tableBacking.results}" var="results" sortMode="single" rowKeyVar="row">
<rich:columns value="#{tableBacking.columns == null ? '' : tableBacking.columns}" var="columns" index="ind" id="column#{ind}" sortBy="#{results[ind].data}" rendered="#{tableBacking.columns != null}">
<f:facet name="header">
<h:outputText value="#{columns.columnDescription}" />
</f:facet>
<h:outputText value="#{results[ind].data}" />
</rich:columns>
</rich:extendedDataTable>
<rich:datascroller for="resultsTable" page="#{tableBacking.page}" renderIfSinglePage="false" />
Backing bean
支撑豆
public class TableBacking {
private List<List<TableData>> results = null;
private int page = 0;
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public List<List<TableData>> getResults() throws Exception {
return results;
}
public void setResults(List<List<TableData>> results) {
this.results = results;
}
private void getData(String whereClause) {
try {
DataDao dd = new DataDao();
results = dd.getData(); // dd.getData just runs a query on a table and puts the results in a List of lists.
} catch (Exception e) {}
}
回答by Carlos León
Try to do this :
尝试这样做:
JSF Stuff:
JSF的东西:
<rich:dataTable id="listTable" var="record" rows="10" value="#{personBean.data}">
....
<f:facet name="footer">
<rich:column colspan="8">
<rich:dataScroller page="#{personBean.page}" for="listTable"
scrollListener="#{personBean.scrollPage}" render="listTable" />
</rich:column>
Records : #{personBean.data.size()}
</f:facet>
</rich:dataTable>
Java BackBean Stuff:
Java BackBean 的东西:
public class PersonBean {
// List of Persons
private PagedList<Person> data;
// Current page
private Integer page;
// Default rows to display
private Integer rows = 10;
// Persistence layer
private DaoService daoService;
@PostConstruct
public void init(){
page= 1;
}
// Event trigger on scroll page
public void scrollPage(DataScrollEvent ev) {
log.debug("Scroll event");
reset();
// getPersons execute 2 queries : the first query is the count(*) and the result is set in TotalElements
// the second query gets the 'n' records defined in rows , something like this :
// select * from Persons LIMIT page*rows, (page*rows) + rows and the result is set in Content
Page<Person> pageResult = this.daoService.getPersons(filter, page - 1, rows);
// Now create a pagedList with the constructor
this.data = new PagedList<Person>(pageResult.getContent(), (int) pageResult.getTotalElements(), this.pageSize);
}
}
Java PagedList
Java分页列表
public final class PagedList<E> extends AbstractList<E> {
private final List<E> lista;
private final int total;
private final int pageSize;
public PagedList(List<E> lista, int total, int pageSize) {
this.lista = lista;
this.total = total;
this.pageSize = pageSize;
}
public int size() {
return total;
}
public E get(int i) {
i = i % this.pageSize;
return lista.get(i);
}
}
The trick is to set the total property of PagedList
class with the count of the dataTable
in the database and only set the paged elements in the list of PageList
.
诀窍是PagedList
用dataTable
数据库中的计数设置类的总属性,并只设置列表中的分页元素PageList
。
Sorry for my bad english :P
抱歉我的英语不好:P
回答by Martín Straus
The best solution I've found is mapping the "value" attribute of the dataTable to a org.richfaces.model.ExtendedTableDataModelproperty. You then initialize the ExtendedTableDataModel with an instance of org.richfaces.model.DataProvider. Sample:
我发现的最佳解决方案是将 dataTable 的“value”属性映射到 org.richfaces.model.ExtendedTableDataModelproperty。然后使用 org.richfaces.model.DataProvider 的实例初始化 ExtendedTableDataModel。样本:
In the view:
在视图中:
<rich:dataTable id="table" value="#{bean.model}" rows="#{bean.rows}">
<!-- columns -->
<f:facet name="footer">
<rich:dataScroller for="table"/>
</f:facet>
</rich:dataTable>
In the bean:
在豆中:
@ManagedBean
public class Bean {
private int rows = 15; // the ammount of rows per page.
private org.richfaces.model.ExtendedTableDataModel model = new org.richfaces.model.ExtendedTableDataModel(
new org.richfaces.model.DataProvider(
public int getRowCount() {
// count your rows.
}
public List<T> getItemsByRange(int first, int quantity) {
// fetch the page's rows.
}
public T getItemByKey(Object o) {
return null; // no need to implement this.
}
public Object getKey(T t) {
return null; // no need to implement this.
)
);
public int getRows() { return rows; }
public ExtendedTableDataModel getModel() { return model; }
}
回答by Xtreme Biker
You need to bind your rich:extendedDataTable
to an attribute in your backing bean.
您需要将您的绑定rich:extendedDataTable
到支持 bean 中的一个属性。
Java
爪哇
private HtmlExtendedDataTable Table;
//Getter and setter
After that you bind your view
之后你绑定你的观点
xhtml
html
<rich:extendedDataTable id="resultsTable" value="#{tableBacking.results}" var="results" sortMode="single" rowKeyVar="row" binding="#{tableBacking.Table}">
<rich:extendedDataTable id="resultsTable" value="#{tableBacking.results}" var="results" sortMode="single" rowKeyVar="row" binding="#{tableBacking.Table}">
Richfaces HtmlExtendedDataTable
has a method to check how many rows are displayed per page, called getRows()
. When bean initializes just load the first page values, according to that (you can load only the id's of the elements on the rest of the pages and later when you're going to access the page load them based on the id).
RichfacesHtmlExtendedDataTable
有一个方法来检查每页显示多少行,称为getRows()
. 当 bean 初始化时,只加载第一个页面值,根据那个(你可以只加载其余页面上元素的 id,稍后当你要访问页面时,根据 id 加载它们)。
<rich:datascroller/>
also has an event which is fired when a page button is clicked and you can acquire that from your backing bean in this way:
<rich:datascroller/>
还有一个在单击页面按钮时触发的事件,您可以通过以下方式从支持 bean 中获取该事件:
xhtml
html
<f:facet name="footer">
<rich:datascroller id="ds"
scrollerListener="#{tableBacking.actionOnPageChange}" />
</f:facet>
Java
爪哇
public String actionOnPageChange(DataScrollerEvent event)
public String actionOnPageChange(DataScrollerEvent event)
This DataScrollerEvent
includes what page has been clicked by the user, which you can get using getNewScrolVal()
. When that happens just load the values of the corresponding page in your table and that's it.
这DataScrollerEvent
包括用户单击了哪个页面,您可以使用getNewScrolVal()
. 发生这种情况时,只需加载表中相应页面的值即可。