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>自定义布局,而不是<p:checkbox>类似的。无论如何,我怎样才能实现它?如何显示<p:selectManyCheckbox>带有图像的项目?

4

1 回答 1

2

这是不可能的<p:selectManyCheckbox>。您最好的选择是只使用一堆<p:selectBooleanCheckbox>组件并将模型更改为Map<Entity, Boolean>而不是List<Entity>. 您可以使用<ui:repeat>.

例如(普通的 XHTML 变体;我不会提倡 JavacreateComponent()等价物):

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