0

按照http://www.primefaces.org/showcase-labs/ui/datatableRowSelectionRadioCheckbox.jsf上的 Primefaces 数据表示例,我已经能够构建一个数据表,该数据表显示来自我的数据源的正确标题和行数。但是,没有数据显示;它显示空白单元格。

调试后,我发现问题出在我的 DataModel 中的以下代码部分:

@Override
public Resource getRowData(String rowKey) {
    List<Resource> resources = (List<Resource>) getWrappedData();

    for(Resource resource : resources) {
        if(resource.getResourceId().equals(rowKey))
            return resource;
    }
    return null;
}

if(resource.getResourceId().equals(rowKey))条件评估为假,因此不返回任何对象Resource

我似乎看不出我做错了什么。我的相关部分bean如下所示:

public class NewIncidentWizardBean implements Serializable {

private static final long serialVersionUID = 1L;

private static Logger logger =  Logger.getLogger(NewIncidentWizardBean.class.getName());

private static Resource[] tmpResources;

static {
    tmpResources = new Resource[12];

    tmpResources[0] = new Resource(new Long(1), ..., new Long(2));
    tmpResources[1] = new Resource(new Long(2), ..., new Long(4));
    tmpResources[2] = new Resource(new Long(3), ..., new Long(10));
    tmpResources[3] = new Resource(new Long(4), ..., new Long(10));
    tmpResources[4] = new Resource(new Long(5), ..., new Long(10));
    tmpResources[5] = new Resource(new Long(6), ..., new Long(10));
    tmpResources[6] = new Resource(new Long(7), ..., new Long(10));
    tmpResources[7] = new Resource(new Long(8), ..., new Long(10));
    tmpResources[8] = new Resource(new Long(9), ..., new Long(10));
    tmpResources[9] = new Resource(new Long(10), ..., new Long(10));
    tmpResources[10] = new Resource(new Long(11), ..., new Long(7));
    tmpResources[11] = new Resource(new Long(12), ..., new Long(90));
}

private List<Resource> resources;
private ResourceDataModel resourcesModel;
private Resource selectedResource;
private Resource[] selectedResources;

public NewIncidentWizardBean() {
    resources = new ArrayList<Resource>();
    for(int i = 0; i < tmpResources.length; i++)
        resources.add(tmpResources[i]);

    resourcesModel = new ResourceDataModel(resources);
}

public Resource getSelectedResource() {
    return selectedResource;
}

public void setSelectedResource(Resource selectedResource) {
    this.selectedResource = selectedResource;
}

public Resource[] getSelectedResources() {
    return selectedResources;
}

public void setSelectedResources(Resource[] selectedResources) {
    this.selectedResources = selectedResources;
}

public Resource getResource() {
    return resource;
}

public void setResource(Resource resource) {
    this.resource = resource;
}

public List<Resource> getResources() {
    return resources;
}

public void setResources(List<Resource> resources) {
    this.resources = resources;
}

public ResourceDataModel getResourcesModel() {
    return resourcesModel;
}
}

在我看来,相关部分是:

<p:dataTable id="resources" var="resource" value="#{newDisasterWizardBean.resourcesModel}" paginator="true" rows="10"  
             selection="#{newDisasterWizardBean.selectedResources}">  

    <p:column selectionMode="multiple" style="width:18px" />  

    <p:column headerText="Resource Name">  
        #{resource.name}  
    </p:column>  

    <p:column headerText="Description">  
        #{resource.description}  
    </p:column>  

    <p:column headerText="Resource Type" >  
        #{resource.type}  
    </p:column>  

    <p:column headerText="Units Required">  
        #{resource.units}  
    </p:column>  

</p:dataTable> 

我还无法发现我可能做错了什么。任何帮助将不胜感激。提前致谢。

4

1 回答 1

1

你能留下一个说明你正在使用哪个PF版本吗?
从 3.3 开始,数据表的延迟加载处理发生了变化。试一试 3.2 然后你可以看到你的代码是否正常工作。
此外,我定义了 rowKey 标签。我想你应该定义它!为了触发选择/取消选择,您还应该定义这些 ajax 事件:

    <p:dataTable id="myid"
        value="#{myvalue}" var="valuevar"
        selection="#{value.valueSelected}"
        rowKey="#{valuevar.onevalue}">
        <p:ajax event="rowSelect"
            process="@this"
            update="@this" />
        <p:ajax event="rowUnselect"
            process="@this"
            update="@this" />
                    ...................
    </p:dataTable>

我目前在 3.3.1 中也坚持使用 PrimeFaces issue 2993中提到的 lazy=true 参数。

希望有帮助。

于 2012-06-18T10:03:50.307 回答