Primefaces上的延迟加载+命令按钮

时间:2013-10-28 16:06:44

标签: jsf jsf-2 primefaces datatable lazy-loading

我是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()函数。

1 个答案:

答案 0 :(得分:1)

我发现我必须将我的DataTable与我的bean绑定在我的bean上我必须调用loadLazyData()函数来通知表加载它。