在p:dataTable标记上使用带有延迟加载数据的primeface时,h:commandLink不起作用

时间:2012-08-28 17:39:15

标签: jsf primefaces

我正在使用p:dataTable的primefaces,我需要将一个动作关联到每一行(编辑和删除),当我使用h:commandLink似乎工作正常但是如果我将lazyLoading添加到p:dataTable h:commandLink不再起作用(不再调用该动作)。

这是一个错误,还是有一种特殊的方法让p:dataTable在每一行上使用lazyLoading和h:commandLink?

编辑:谢谢@Heidarzadeh,我添加了源代码。

locationList.xhtml:

<ui:composition template="/template/layout.xhtml">
    <ui:define name="content">
        <div class="span10">
            <h2>Listado de Bodegas</h2>
            <h:form id="locationsForm">
                <fieldset>
                    <p:dataTable 
                        id="locationTable"
                        var="row"
                        value="#{locationBackingBean.list()}" 
                        paginator="true" 
                        rows="10"
                        lazy="true"
                        dynamic="true"
                        rowsPerPageTemplate="10,30,50"
                        paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}">

                        <p:column headerText="#">
                            <h:outputText style="" value="#{row.locationId}"/>
                        </p:column>

                        <p:column headerText="Nombre">
                            <h:outputText style="" value="#{row.name}"/>
                        </p:column>

                        <p:column headerText="Descripción">
                            <h:outputText style="" value="#{row.description}"/>
                        </p:column>

                        <p:column headerText="Sede">
                            <h:outputText
                                value="#{officeBackingBean.findById(row.officeId).description}"/>
                        </p:column>

                        <p:column >
                            <h:commandLink id="edit"
                                action="#{locationBackingBean.view(row.locationId)}">
                                <h:graphicImage styleClass="icon-edit" />
                            </h:commandLink>
                        </p:column>

                        <p:column headerText="Elminar">
                            <p:commandLink id="remove"
                                action="#{locationBackingBean.remove(row.locationId,row.recordVersion)}">
                                <p:graphicImage styleClass="icon-remove" />
                            </p:commandLink>
                        </p:column>
                    </p:dataTable>
                    <br />
                    <h:link styleClass="btn btn-primary" value="Agregar"
                        outcome="/config/locationView" />
                </fieldset>
            </h:form>

        </div>
    </ui:define>
</ui:composition>

托管bean:

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.primefaces.model.LazyDataModel;

@ManagedBean(name = "locationBackingBean")
@RequestScoped
public class LocationBackingBean implements Serializable {

@EJB
private LocationBean locationBean;
private InvLocationDTO invLocationDTO;
private LazyLocationDataModel lazyInvLocationDTO;

public LocationBackingBean() {
    super();
    invLocationDTO = new InvLocationDTO();
}

@PostConstruct
public void init() throws InventoryException {
    try {
        lazyInvLocationDTO = new LazyLocationDataModel(locationBean);
    } catch (Exception ex) {
        throw new InventoryException(ex.getMessage(), ex);
    }
}

public String view(Integer locationId) throws InventoryException {
    try {
        if (locationId != null) {
            invLocationDTO.setLocationId(locationId);
            invLocationDTO = locationBean.findByID(invLocationDTO);
        }
    } catch (InventoryException ex) {
        throw ex;
    } catch (Exception ex) {
        log.error(ex.getMessage(), ex);
        throw new InventoryException(ex.getMessage(), ex);
    }
    return "/config/locationView";
}

public String remove(Integer locationId, Integer recordVersion) throws InventoryException {
    try {
        if (locationId != null) {
            invLocationDTO.setLocationId(locationId);
            invLocationDTO.setRecordVersion(recordVersion);
            Integer rows = locationBean.remove(invLocationDTO);
            if (rows == 0) {
                // TODO Fix the handle of error, this will be, because the record doesn't exists or record version didn't match.
                throw new InventoryException("Registro no fue eliminado.");
            }
        }
        return "/config/locationList";
    } catch (InventoryException ex) {
        throw ex;
    } catch (Exception ex) {
        log.error(ex.getMessage(), ex);
        throw new InventoryException(ex.getMessage(), ex);
    }
}    

public InvLocationDTO getInvLocationDTO() {
    return invLocationDTO;
}

public void setInvLocationDTO(InvLocationDTO invLocationDTO) {
    this.invLocationDTO = invLocationDTO;
}

public LazyDataModel<InvLocationDTO> list() throws InventoryException {
    return lazyInvLocationDTO;
}
}

懒惰数据模型:

import java.util.List;
import java.util.Map;

import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

public class LazyLocationDataModel extends LazyDataModel<InvLocationDTO> {

    private LocationBean locationBean;
    private List<InvLocationDTO> datasource = null;

    public LazyLocationDataModel(LocationBean locationBean) {
        super();
        this.locationBean = locationBean;
    }

    public List<InvLocationDTO> load(int first, int pageSize, String sortField,
            SortOrder sortOrder, Map<String, String> filters) {
        try {
            datasource = locationBean.findAll(first, pageSize);
            // Row Count
            this.setRowCount(locationBean.countFindAll());
        } catch (InventoryException ex) {
            log.error(ex.getMessage(), ex);
        }
        return datasource;
    }

    @Override
    public void setRowIndex(int rowIndex) {
        if (rowIndex == -1 || getPageSize() == 0) {
            super.setRowIndex(-1);
        } else
            super.setRowIndex(rowIndex % getPageSize());
    }

    public List<InvLocationDTO> getDatasource() {
        return datasource;
    }

    @Override  
    public InvLocationDTO getRowData(String rowKey) {  
        for(InvLocationDTO location : datasource) {  
            if(location.getLocationId().equals(Integer.valueOf(rowKey)))  
                return location;  
        }  

        return null;  
    }  

    @Override  
    public Object getRowKey(InvLocationDTO location) {  
        return location.getLocationId();  
    }      

}

0 个答案:

没有答案