Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 views
in Technique[技术] by (71.8m points)

java - Is it possible to have multiple rows with jsf h:datatable

With h:datatable we can display data as follows

  1. Jems
  2. tom
  3. chirs
  4. harry

but can I display the same as shown below:

  1. Jems 2. tom
  2. chris 4. harry

Ragards, Abhi

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can manage this in the model.

For example, split a list into pairs:

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();
  }
}

The pair class:

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; }
}

The managed bean that exposes the list:

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, "-");
  }
}

The table configuration:

<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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...