简体   繁体   English

如何在p:selectManyCheckbox中显示带有图像的项目

[英]How to display items with image in p:selectManyCheckbox

I need to display <p:selectManyCheckbox> items with images. 我需要显示带有图像的<p:selectManyCheckbox>项目。 I tried to display images with in <p:selectOneRadio> . 我试图在<p:selectOneRadio>显示图像。 It works fine. 工作正常。 I am programmatically adding components on UI. 我正在以编程方式在UI上添加组件。 This is my code. 这是我的代码。

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

That's <p:selectOneRadio> with images. 这是<p:selectOneRadio>与图像。

I'd like to use <p:selectManyCheckbox> in same way. 我想以相同的方式使用<p:selectManyCheckbox> But PrimeFaces has only a <p:radioButton> for custom layoue and not a <p:checkbox> like that. 但是PrimeFaces只有一个<p:radioButton>用于自定义layoue,而没有这样的<p:checkbox> How can I achieve it anyway? 无论如何我如何实现呢? How can I display <p:selectManyCheckbox> items with images ? 如何显示带有图像的<p:selectManyCheckbox>项目?

That's not possible with <p:selectManyCheckbox> . 使用<p:selectManyCheckbox>不可能的。 Your best bet is to just use a bunch of <p:selectBooleanCheckbox> components instead and change the model to be Map<Entity, Boolean> instead of List<Entity> . 最好的选择是只使用一堆<p:selectBooleanCheckbox>组件,并将模型更改为Map<Entity, Boolean>而不是List<Entity> You can loop over it using <ui:repeat> . 您可以使用<ui:repeat>对其进行循环。

Eg (normal XHTML variant; I am not going to advocate the Java createComponent() equivalent): 例如(正常的XHTML变体;我将不提倡使用Java createComponent()等效项):

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

with

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

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

To check which ones are selected, loop over the map in action method: 要检查选择了哪些地图,请使用action方法遍历地图:

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

for (Entry<Entity, Boolean> entry : selection.entrySet()) {
    if (entry.getValue()) {
        selectedEntities.add(entry.getKey());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM