使用LazyDataModel

时间:2017-09-04 14:02:38

标签: jsf primefaces

我正在使用PrimeFaces 6.1。我已经实现了LazyDataModel并且我能够加载行,但是当我更新单元格时,调用onCellEdit方法但是event.rowKey始终为null。

     <p:dataTable emptyMessage="Nessun record trovato."
                                         style="margin-top: 50px" id="cars" var="produit"
                                         value="#{gecomparBean.lazyModel}"  
                                         editable="true" editMode="cell" widgetVar="carsTable"
                                         filteredValue="#{gecomparBean.filteredCars}" 
                                         rows="10"
                                         lazy="true"
                                         scrollRows="10"
                                         scrollable="true" liveScroll="true" scrollHeight="250"


                                         resizableColumns="true" resizeMode="expand"
                                         draggableColumns="true" 

                                         rowStyleClass="#{produit.id == gecomparBean.selectedObject.id ? 'selectedRow' : null}"
                                         >



                                <p:ajax event="colReorder"
                                        listener="#{gecomparBean.onColumnReorder}" />
                                <p:ajax event="cellEdit" listener="#{gecomparBean.onCellEdit}"  
                                        oncomplete="updateTable()"  update=":form:remote" />

Managed Bean:[会话范围]

 wrapper.setSession(activeSession.getId());
        lazyModel = new LazyProductDataModel(wrapper);

LazyProductDataModel

public class LazyProductDataModel extends LazyDataModel<ProductSessionDTO> implements Serializable {

private List<ProductSessionDTO> products;
private ProductSearchWrapper wrapper;

public LazyProductDataModel(ProductSearchWrapper wrapp) throws RestRequestException {
    super();
    wrapper = wrapp;

}

@Override
public ProductSessionDTO getRowData(String rowKey) {
    for (ProductSessionDTO car : getProducts()) {
        if (car.getId() == (Long.parseLong(rowKey))) {
            return car;
        }
    }

    return null;
}

@Override
public Object getRowKey(ProductSessionDTO car) {
    return car.getId();
}

@Override
public List<ProductSessionDTO> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
    List<ProductSessionDTO> data = new ArrayList<ProductSessionDTO>();
    try {
        System.out.println("First " + first + " Max " + pageSize);
        wrapper.setFirst(first);
        wrapper.setPageSize(pageSize);
        products=ServiceUtils.getInstance().getProducts(wrapper);

        if (getProducts() != null) {

            Comparator<ProductSessionDTO> lengthComparator = new Comparator<ProductSessionDTO>() {
                @Override
                public int compare(ProductSessionDTO o1, ProductSessionDTO o2) {
                    return Long.compare(o1.getId(), o2.getId());
                }
            };
            Collections.sort(products, lengthComparator);

        }
    } catch (RestRequestException ex) {
        Logger.getLogger(LazyProductDataModel.class.getName()).log(Level.SEVERE, null, ex);
    }

    data = products;

    //rowCount
    int dataSize = data.size();
    this.setRowCount(1000);

    //paginate
    if (dataSize > pageSize) {
        try {
            return data.subList(first, first + pageSize);
        } catch (IndexOutOfBoundsException e) {
            return data.subList(first, first + (dataSize % pageSize));
        }
    } else {
        return data;
    }
}

}

注意:我发现一篇帖子提到我们需要删除rowkey以便自动调用getRowKey() - 所以我删除了该属性。

0 个答案:

没有答案