JSF中每条记录多行? [英] Multiple rows per record in JSF?

查看:123
本文介绍了JSF中每条记录多行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个myfaces datatable,可能有2 每个记录?
我的简单的一行表格如下所示:

having a myfaces datatable, is is possible to have 2 rows for each record? my simple one-row table looks like the following:

<h:dataTable id="somelist" value="#{MyBean.somelist}" var="item">
    <h:column>
        <f:facet name="header">
            <h:outputText value="ID"/>
        </f:facet>
        <h:outputText value="#{item.id}"/>
    </h:column>
</h:dataTable>


推荐答案

是的,在这种情况下。您可以通过自定义的 outputText 的值DataModel.htmlrel =nofollow noreferrer> DataModel

Yes, in this case. You can control the value bound to the outputText via a custom DataModel.

public class TwoRowModel extends DataModel {

    private List<Pair> values = initData();
    private int index = -1;

    private List<Pair> initData() {
        List<Pair> list = new ArrayList<Pair>();
        for (int i = 0; i < 5; i++) {
            Pair pair = new Pair();
            pair.key = "Key " + i;
            pair.value = "Value " + i;
            list.add(pair);
        }
        return list;
    }

    @Override
    public int getRowCount() {
        return values.size() * 2;
    }

    @Override
    public Object getRowData() {
        Pair pair = values.get(index / 2);
        if (index % 2 == 0) {
            return pair.key;
        } else {
            return pair.value;
        }
    }

    @Override
    public int getRowIndex() {
        return index;
    }

    @Override
    public Object getWrappedData() {
        throw new UnsupportedOperationException("Demo code");
    }

    @Override
    public boolean isRowAvailable() {
        int realIndex = index / 2;
        if (realIndex < 0) {
            return false;
        }
        if (realIndex >= values.size()) {
            return false;
        }
        return true;
    }

    @Override
    public void setRowIndex(int idx) {
        this.index = idx;
    }

    @Override
    public void setWrappedData(Object value) {
        throw new UnsupportedOperationException("Demo code");
    }

    class Pair {
        public String key;
        public String value;
    }

}

在此演示代码中,值返回的只是一个要显示的:

In this demo code, the value returned is just the one to be displayed:

    <h:dataTable id="somelist" value="#{twoRowModel}" var="item">
        <h:column>
            <f:facet name="header">
                <h:outputText value="ID" />
            </f:facet>
            <h:outputText value="#{item}" />
        </h:column>
    </h:dataTable>

这篇关于JSF中每条记录多行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆