如何为 Vaadin 8 中的 checkboxGroup 中的特定复选框添加 CSS 样式。

huangapple go评论71阅读模式
英文:

how to put css styling for one specific checkbox in checkboxGroup of vaadin 8

问题

以下是您要翻译的内容:

我有一个包含多个复选框的复选框组。我有一些复选框需要看起来不同,可以通过加粗文本或着色文本来实现,而这些样式不会因选择/取消选择而改变。
我有以下代码来构建复选框组。但是我无法为一个特定的复选框添加样式,因为我无法访问它。我该如何做呢?

CheckBoxGroup<ReferenceScreenResultAnswer> answersOptionGroup = new CheckBoxGroup<>(question.getText());
List<ReferenceScreenResultAnswer> checkBoxItems = new ArrayList<>();
answersOptionGroup.setItems(checkBoxItems);
.......
// 这是我想要为特定复选框/值添加样式的地方
for (Answer answer : preSelectedAnswer)
{
	ReferenceScreenResultAnswer rsra = new ReferenceScreenResultAnswer();
	rsra.setAnswer(answer);
	rsra.setReferenceScreenResultQuestion(rsrq);
	answersOptionGroup.select(rsra);
}

我可以单独处理复选框,如下所示

CheckBox cb = new CheckBox();
cb.setCaptionAsHtml(true);
cb.setCaption("<b> 你好 </b> 在那里");

但是我无法从CheckBoxGroup中访问单独的复选框。您有任何关于如何访问它们的想法吗?

英文:

I have a checkboxgroup which has multiple checkboxes. I have certain checkboxes which needs to look different, either by bold text or colored text ,which wont change irrespective of selection/unselection.
I have following code to build checkboxgroup. But I am not able to put style specific to one checkbox, because I dont have access to it. How can I do that

CheckBoxGroup<ReferenceScreenResultAnswer> answersOptionGroup = new CheckBoxGroup<>(question.getText());
List<ReferenceScreenResultAnswer> checkBoxItems = new ArrayList<>();
answersOptionGroup.setItems(checkBoxItems);
.......
// this is where i want to put CSS to specific checkbox/values
for (Answer answer : preSelectedAnswer)
{
	ReferenceScreenResultAnswer rsra = new ReferenceScreenResultAnswer();
	rsra.setAnswer(answer);
	rsra.setReferenceScreenResultQuestion(rsrq);
	answersOptionGroup.select(rsra);
}

I can do invidiual checkboxes like

CheckBox cb = new CheckBox();
cb.setCaptionAsHtml(true);
cb.setCaption("<b> hello </b> there");

But I am not able to access individual checkboxes from CheckBoxGroup. Any idea how to access them

答案1

得分: 1

我找到了答案:

// 为预选答案添加CSS样式,以使它们在选择方面看起来不同
answersOptionGroup.setItemCaptionGenerator(new ItemCaptionGenerator<ReferenceScreenResultAnswer>() {
    @Override
    public String apply(ReferenceScreenResultAnswer item) {
        if (preSelectedAnswer.contains(item.getAnswer()))
            return "<strong>" + item.getAnswer().toString() + "</strong>";
        else
            return item.getAnswer().toString();
    }
});
英文:

i found the answer:

                    // css style the pre selected answer, so they look different irrespective
					// of their selection
					answersOptionGroup.setItemCaptionGenerator(new ItemCaptionGenerator&lt;ReferenceScreenResultAnswer&gt;()
					{
						@Override
						public String apply(ReferenceScreenResultAnswer item)
						{		
							if (preSelectedAnswer.contains(item.getAnswer()))
								return &quot;&lt;strong&gt;&quot; + item.getAnswer().toString() + &quot;&lt;/strong&gt;&quot;;
							else
								return item.getAnswer().toString();
						}
					});

huangapple
  • 本文由 发表于 2020年10月18日 12:03:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64409620.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定