简体   繁体   English

跳过jsf dataTable中的某些行

[英]Skip some row in jsf dataTable

How to skip some rows to be displayed using dataTable: 如何跳过一些要使用dataTable显示的行:

<h:dataTable cellspacing="0" id="dogs" value="#{dogBean.dogs}" var="dog" rendered="#{dogBeans.dogs != null}">

<h:column id="nameColumn">

    <h:outputText value="#{dog.name}"/>
</h:column>

<h:column id="breedColumn">

    <h:outputText value="#{dog.breed}"/>
</h:column>

</h:dataTable>

I want to display all dogs, but those how have an age greater than 10. dog.age > 10. 我想显示所有的狗,但是要显示年龄大于10岁的狗。dog.age> 10。

I'm using Seam. 我正在使用Seam。

You can't do this nicely in the view side. 您不能在视图方面很好地做到这一点。 You can at most set the rendered attribute of every cell contents to false , but this doesn't avoid the <tr> element being rendered. 您最多可以将每个单元格内容的rendered属性设置为false ,但这不能避免<tr>元素被渲染。 You would see a blank row and its appearance may not be consistent among browsers. 您会看到一个空白行,并且它的外观在浏览器之间可能不一致。

Best is to filter the rows beforehand in the (post)construct, action(listener) or maybe lazily in the getter. 最好是预先在(post)构造,action(listener)中过滤行,或者在getter中延迟过滤。

List<Dog> dogsOlderThan10 = new ArrayList<Dog>();
for (Dog dog : dogs) {
    if (dog.getAge() > 10) dogsOlderThan10.add(dog);
}

Or, just send a new SQL query returning exactly the data you need. 或者,只需发送一个新的SQL查询,即可返回所需的确切数据。

You can write your own renderer for datatable. 您可以编写自己的数据表渲染器。 For example, I use richfaces and I have the folowing renderer: 例如,我使用richfaces并具有以下渲染器:

public class DetailDataTableRenderer extends DataTableRenderer {

    @Override
    public void encodeOneRow(FacesContext context, TableHolder tableHolder) throws IOException {
        Object obj = tableHolder.getTable().getRowData();
        if (obj instanceof BasicDTO) {
            BasicDTO dto = (BasicDTO)obj;
            if (dto.isSkipRow()) {
                return;
            }
        }
        super.encodeOneRow(context, tableHolder);
    }
}

and I registered my renderer as 我将渲染器注册为

<renderer>
    <component-family>org.richfaces.DataTable</component-family>
    <renderer-type>org.richfaces.DataTableRenderer</renderer-type>
    <renderer-class>myframework.view.component.DetailDataTableRenderer</renderer-class>     
</renderer>

I hope it helped. 希望对您有所帮助。

Cesar. 塞萨尔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM