4

Primefaces で JSF 2.1 を使用する

class FOO{
String name;
String value;
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void setValue(String value){
this.value=value;
}
public String getValue(){
return this.value;
}

}

を持っていますMap<String, List<FOO>>

ヘッダー名はマップのキーにする必要があります。複数の列 (マップのサイズ) を作成する必要があり、各列には FOO.Name を行に表示するための FOO のリストが必要です。

例: マップのサイズが 4 の場合

列-----キー1

1 列目の ROW - List<FOO>Key1 に対して

列-----Key2

1 列目の ROW - List<FOO>Key2 に対して

列-----Key3

1 列目の ROW - List<FOO>Key3 に対して

列-----Key4

1 列目の ROW - List<FOO>Key4 に対して

このタイプの出力を xhtml ページに表示するために使用するコンポーネントを誰か教えてもらえますか? 動的データテーブル作成を使用しようとしましたが、これを表示できませんでした。

4

2 に答える 2

4

間違ったデータ構造があります。正しいデータ構造に変更してください。最も簡単なのは、プロパティList<Map<String, Object>>を表す でデータを収集することです。rowsMap、列名でキー付けされた列を表します。List<String>これらの列名を、プロパティを表す個別に収集しcolumnsます。最後に、次のように表示します<p:columns>

<p:dataTable value="#{bean.rows}" var="row">
    <p:columns value="#{bean.columns}" var="column">
        #{row[column]}
    </p:columns>
</p:dataTable>

必要に応じて、奇妙なデータ構造を適切なデータ構造に変換する方法を次に示します (それぞれList<FOO>が同じサイズであると仮定します。それ以外の場合、データ構造全体はあまり意味がありません)。

Map<String, List<FOO>> wrongDataStructure = createItSomehow();
List<String> columns = new ArrayList<String>(wrongDataStructure.keySet()); // Note I expect LinkedHashMap ordering here.
List<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();
int size = wrongDataStructure.values().iterator().next().size();

for (int i = 0; i < size; i++) {
    Map<String, Object> row = new HashMap<String, Object>();

    for (String column : columns) {
        row.put(column, wrongDataStructure.get(column).get(i).getName());
    }

    rows.add(row);
}

// Now use "columns" and "rows".
于 2013-01-30T20:59:23.673 に答える
0

I couldn't figure out a way to do this without using a trick: Since the size of your lists in the map may differ for each key set, you need to know the size of the list with largest item set. By knowing that you can come up with this bizarre solution: The idea is to have a for loop in a for loop.

private Map<String, List<Foo>> fooMap = Maps.newHashMap();
private List<Foo> highestNumberOfFoos;

private init()
{
    highestNumberOfFoos = Lists.newArrayList(new Foo("a", "b"), new Foo("c", "d"), new Foo("e", "f")); // We know that there won't be any list who has more items than this item.
    fooMap.put("name1", highestNumberOfFoos);
    fooMap.put("name2", Lists.newArrayList(new Foo("g", "h"), new Foo("i", "j")));
}

...and in the view:

<table>
      <thead>
              <tr>
                  <ui:repeat value="#{bean.fooMap.keySet().toArray()}" var="key">
                         <td>#{key}</td>
                   </ui:repeat>
              </tr>
       </thead>
       <tbody>
             <ui:repeat value="#{bean.highestNumberOfFoos}" var="dummyFoo" varStatus="vs">
             <tr>
                 <ui:repeat value="#{bean.fooMap.entrySet().toArray()}" var="innerEntry">
                     <td>#{innerEntry.value[vs.index].name}</td>
                  </ui:repeat>
             </tr>
                  </ui:repeat>
       </tbody>
 </table>
于 2013-01-30T20:20:54.643 に答える