2

<p:selectManyCheckbox>画像付きのアイテムを表示する必要があります。in で画像を表示してみました<p:selectOneRadio>。それは正常に動作します。プログラムで UI にコンポーネントを追加しています。これは私のコードです。

answerRadio.setLayout("custom"); //answerRadio is SelectOneRadio
customPnl = (PanelGrid) app.createComponent(PanelGrid.COMPONENT_TYPE);
            customPnl.setId("pnl"+qstnCnt);
            customPnl.setColumns(3);
radioBtn = (RadioButton) app.createComponent(RadioButton.COMPONENT_TYPE);
                        radioBtn.setId("opt"+qstnAnsIndx);
                        radioBtn.setFor("ID of answerRadio");
                        radioBtn.setItemIndex(ansIndx);
                        customPnl.getChildren().add(radioBtn);

outPnl.getChildren().add(answerRadio); //outPnl is OutputPanel that include answerRadio
outPnl.getChildren().add(customPnl);

それ<p:selectOneRadio>は画像付きです。

同じように使いたい<p:selectManyCheckbox>です。しかし、PrimeFaces には<p:radioButton>カスタム layoueのみがあり、その<p:checkbox>ようなものはありません。とにかくどうすればそれを達成できますか?<p:selectManyCheckbox>画像付きのアイテムを表示するにはどうすればよいですか?

4

1 に答える 1

2

では不可能<p:selectManyCheckbox>です。あなたの最善の策は、代わりに一連の<p:selectBooleanCheckbox>コンポーネントを使用し、モデルを の代わりに変更することMap<Entity, Boolean>ですList<Entity>。を使用してループできます<ui:repeat>

createComponent()例 (通常の XHTML バリアント。Javaと同等のものを推奨するつもりはありません):

<ui:repeat value="#{bean.entities}" var="entity">
    <p:selectBooleanCheckbox value="#{bean.selection[entity]}" />
    ... (you can put here image, label, anything)
</ui:repeat>

private List<Entity> entites; 
private Map<Entity, Boolean> selection;

@PostConstruct
public void init() {
    entities = service.list();
    selection = new HashMap<>(); // No need to prefill it!
}

どれが選択されているかを確認するには、アクション メソッドでマップをループします。

List<Entity> selectedEntities = new ArrayList<>();

for (Entry<Entity, Boolean> entry : selection.entrySet()) {
    if (entry.getValue()) {
        selectedEntities.add(entry.getKey());
    }
}
于 2015-01-06T08:18:28.340 に答える