我是 JSF 的新手,我正在使用 primefaces-2.2.RC2。我有一个commandButton,当我单击时,我希望它填充一个使用lazyLoading 的dataTable(我的工作基于primefaces 展示)。当我单击 commandButton 时,未加载数据。
这是我的一段代码:
<p:commandButton value="Search"
action="#{tableBean.search()}"
update="carList"
/>
<p:dataTable id="carList"
var="car"
value="#{tableBean.lazyDataModel}"
paginator="true"
rows="10"
dynamic="true"
lazy="true"
paginatorPosition="bottom"
>
安德在我的豆子上:
@ViewScoped
public class TableBean {
LazyDataModel<Car> lazyDataModel;
public TableBean() {
lazyDataModel = new LazyDataModel<Car>() {
@Override
public List<Car> load(int i, int i1, String string, boolean bln, Map<String, String> map) {
return new ArrayList<Car>();
}
};
}
public LazyDataModel<Car> getLazyDataModel() {
return lazyDataModel;
}
public void search() {
lazyDataModel = new LazyDataModel<Car>() {
@Override
public List<Car> load(int first, int pageSize, String sortField,boolean sortOrder, Map<String, String> filters) {
return fetchLazyData(first, pageSize);
}
@Override
public void setRowIndex(int rowIndex) {
setPageSize(10);
if (rowIndex == -1 || getPageSize() == 0) {
super.setRowIndex(-1);
} else {
super.setRowIndex(rowIndex % getPageSize());
}
}
public List<Car> fetchLazyData(int first, int pageSize) {
System.out.println("Loading the lazy car data between " + first + " and " + (first + pageSize));
List<Car> lazyCars = new ArrayList<Car>();
for (int i = 0; i < pageSize; i++) {
int offset = i + first;
lazyCars.add(new Car("Model_" + offset, (int) (Math.random() * 60 + 1960), "Brand_" + offset, "Color_" + offset));
}
return lazyCars;
}
};
lazyDataModel.setRowCount(10000);
}
}
更新:我发现我必须绑定我的 DataTable 并且在我的 bean 上我必须调用 loadLazyData() 函数。