是否可以使用jsf h:datatable来拥有多行

时间:2009-08-22 06:59:13

标签: java jsf

使用h:datatable,我们可以按如下方式显示数据

  1. JEMS
  2. 汤姆
  3. chirs
  4. 哈里
  5. 但是我可以显示如下所示:

    1. Jems 2. tom
    2. chris 4. harry
    3. Ragards, ABHI

3 个答案:

答案 0 :(得分:5)

您可以在模型中进行管理。

例如,将列表拆分成对:

public class PairedList<T> extends AbstractList<Pair<T>> {
  private final List<? extends T> data;
  private final T defaultVal;

  public PairedList(List<? extends T> data, T defaultVal) {
    this.data = data;
    this.defaultVal = defaultVal;
  }

  @Override public int size() {
    return (data.size() / 2) + (data.size() % 2);
  }

  @Override public Pair<T> get(int index) {
    int left = index * 2;
    int right = left + 1;
    return new Pair<T>(data.get(left), right >= data.size() ? defaultVal : data
        .get(right));
  }

  @Override public boolean addAll(Collection<? extends Pair<T>> c) {
    throw new UnsupportedOperationException();
  }
}

对班级:

public class Pair<T> {

  private final T left;
  private final T right;

  public Pair(T left, T right) {
    this.left = left;
    this.right = right;
  }

  public T getRight() { return right; }
  public T getLeft() { return left; }
}

公开列表的托管bean:

public class TwoPerRowBean implements Serializable {
  private final List<String> data = Arrays.asList("Jems", "tom", "chirs",
      "harry", "Barry");

  public List<Pair<String>> getPairedData() {
    return new PairedList<String>(data, "-");
  }
}

表格配置:

<h:dataTable value="#{twoPerRowBean.pairedData}" var="pair">
  <h:column> <h:outputText value="#{pair.left}" /> </h:column>
  <h:column> <h:outputText value="#{pair.right}" /> </h:column>
</h:dataTable>

答案 1 :(得分:1)

您可以使用支持“newspaperColumns”和“newspaperOrientation”属性的t:dataTable component。 newspaperColumns确定表格将被分割的列数和newspaperOrientation报纸表格中报纸列的方向。

在您的示例中,bean:

public class Bean {
    public List<String> getPeople() {
        List<String> people = new ArrayList<String>();
        people.add("Jems");
        people.add("tom");
        people.add("chirs");
        people.add("harry");
        return people;
    }
}

和JSF:

<t:dataTable var="person" value="#{bean.people}" rowIndexVar="index"
             newspaperOrientation="horizontal" newspaperColumns="2">
    <h:column>
        <h:outputText value="#{index + 1}"/>
    </h:column>
    <h:column>
        <h:outputText value="#{person}"/>
    </h:column>
</t:dataTable>

渲染为:

<table>
  <tbody id="_id13:tbody_element">
    <tr><td>1</td><td>Jems</td><td>2</td><td>tom</td></tr>
    <tr><td>3</td><td>chirs</td><td>4</td><td>harry</td></tr>
  </tbody>
</table>

答案 2 :(得分:0)

您可以将List<String[]>传递给<h:datatable value="bean.list">

@ViewScoped
public class NameClass implements Serializable{

   private List<String[]> list;

   public void onPageLoad(){
      list = someMethod that returns the list;
   }

   //...gettters and setters
}

进入.xhtml:

<h:datatable value="nameClass.list" var="l">
   <h:column>#{l[0]}</h:column>
   <h:column>#{l[1]}</h:column>
</h:datatable>