我使用这个http://www.primefaces.org/showcase-labs/ui/selectManyCheckbox.jsf primefaces组件来获得动态booleanCheckboxes值
<p:selectManyCheckbox value="#{bean.selectedMovies}"
layout="pageDirection">
<f:selectItems value="#{bean.movies}" />
</p:selectManyCheckbox>
我需要将不同的颜色应用于所有布尔复选框,如下图所示
如果是id=1,那么颜色将是红色,如果是id=2,那么颜色将是橙色等等。
正如我们所知道的,这些是电影中的动态值,那么我如何从bean中为这些动态复选框设置背景颜色呢?
我尝试将样式应用于selectManyCheckbox,但它作为背景色应用于整个selectManyCheckbox面板。
那么,我应该如何动态地将支持bean的颜色设置为selectManyCheckbox值呢?
发布于 2012-09-23 10:04:53
可以用纯CSS3完成
取决于你的实际需求。假设最终复选框的html结构与链接到的示例中的默认结构相同,那么以下是实现该结构的纯css3方式。
请注意,此方法并不具体地将颜色绑定到特定的电影/id,它总是将第一部电影设置为红色,第二部设置为橙色,等等。它还有一个实际的限制。如果您计划列出100部电影,每一部都有一个独特的颜色,那么这是而不是的方法。
但是,如果你列出一个小集(10max?),或者你不介意,如果颜色在一个小样本后重复,那么这很可能是你最好的选择。
Here's a fiddle showing both of the following
小集
.ui-selectmanycheckbox tr:nth-of-type(1) .ui-state-default {
background-color: red;
}
.ui-selectmanycheckbox tr:nth-of-type(2) .ui-state-default {
background-color: orange;
}
.ui-selectmanycheckbox tr:nth-of-type(3) .ui-state-default {
background-color: red;
}
.ui-selectmanycheckbox tr:nth-of-type(4) .ui-state-default {
background-color: orange;
}
重复四色
.ui-selectmanycheckbox tr:nth-of-type(4n+1) .ui-state-default {
background-color: red;
}
.ui-selectmanycheckbox tr:nth-of-type(4n+2) .ui-state-default {
background-color: orange;
}
.ui-selectmanycheckbox tr:nth-of-type(4n+3) .ui-state-default {
background-color: green;
}
.ui-selectmanycheckbox tr:nth-of-type(4n+4) .ui-state-default {
background-color: blue;
}
发布于 2012-09-17 05:36:42
这在标准的<p:selectManyCheckbox>
中是不可能的,至少在这种动态环境下是不可能的。如果是静态值,则只需使用CSS3 nth-child()
选择器即可。对于<p:selectManyCheckbox>
中的动态值,您基本上需要覆盖它的呈现器(这不是一项简单的工作),或者发布一个特性请求(这可能需要比您想要的更长的时间)。
您的最佳选择是使用<ui:repeat>
或<h:dataTable>
,其中您在每一行的基础上呈现单个<p:selectBooleanCheckbox>
。这允许您在每个复选框的基础上指定一个styleClass
。这还需要对selectedMovies
属性的填充方式进行技术上的更改。
下面是<h:dataTable>
的一个具体的启动示例(注意:<p:selectManyCheckbox>
本身也生成一个<table>
,所以布局--从技术上讲,没有太大的区别,如果表在语义上困扰您,您可以选择<ui:repeat>
):
<h:dataTable value="#{bean.movies}" var="movie" styleClass="ui-selectmanycheckbox ui-widget">
<h:column>
<p:selectBooleanCheckbox id="checkbox" converter="javax.faces.Boolean"
value="#{bean.checkedMovieIds[movie.id]}" styleClass="#{movie.type}" />
</h:column>
<h:column>
<p:outputLabel for="checkbox" value="#{movie.title}" />
</h:column>
</h:dataTable>
<p:commandButton value="Submit" actionListener="#{bean.collectMovies}" action="#{bean.submit}" />
备注
<h:dataTable styleClass>
使用与<p:selectManyCheckbox>
完全相同的CSS,因此复选框表看起来‘n’感觉完全相同。converter="javax.faces.Boolean"
是强制性的(实际上,让我感到惊讶),因为否则它会将选中的值true
和false
设置为String
而不是Boolean
;在<h:selectBooleanCheckbox>
中不存在这个问题,可能是PF错误吗?styleClass="#{movie.type}"
比styleClass="#{movie.id}"
更有意义,因为为每个单独的"id“值指定一个单独的样式类似乎是一项荒谬的任务,它通常代表一个唯一的自动分配的DB标识符;如果需要,可以在实际代码中对其进行更改。actionListener
在实际的action
方法之前完成收集selectedMovies
的工作。当然,您也可以在实际的action
方法中完成这项工作,但是这样就不再那么清晰地分开了。后台bean如下所示( checkedMovieIds
假设Movie
有一个Long id
属性):
private List<Movie> movies;
private Map<Long, Boolean> checkedMovieIds;
private List<Movie> selectedMovies;
@PostConstruct
public void init() {
movies = populateItSomehow();
checkedMovieIds = new HashMap<Long, Boolean>();
}
public void collectMovies(ActionEvent event) {
selectedMovies = new ArrayList<Movie>();
for (Movie movie : movies) {
if (checkedMovieIds.get(movie.getId())) {
selectedMovies.add(movie);
}
}
}
public void submit() {
System.out.println(selectedMovies);
// ...
}
// +getters (note: no setters necessary for all those three properties)
假设#{movie.type}
可以具有action
、comedy
、fantasy
和horror
值(顺便说一句,它是一个完美的enum
类型候选),那么您可以将单个复选框的样式设置为:
.action .ui-chkbox-box {
background-color: red;
}
.comedy .ui-chkbox-box {
background-color: orange;
}
.fantasy .ui-chkbox-box {
background-color: green;
}
.horror .ui-chkbox-box {
background-color: blue;
}
发布于 2012-09-17 12:06:34
我知道这个解决方案有点麻烦,但它有效:)
Xhtml文件:
<p:selectManyCheckbox binding="#{requestScope.movieSelect}" layout="pageDirection">
<f:selectItems value="#{bean.movies}" />
</p:selectManyCheckbox>
<script>
(function() {
var selectName = '#{requestScope.movieSelect.clientId}';
var kids = jQuery("input[id*="+selectName+"]");
var index = 0;
<ui:repeat value="#{bean.movies}" var="movie">
jQuery(kids[index++]).parent().next().css('background-color', '#{movie.description}');
</ui:repeat>
}());
</script>
支持豆:
public class Bean {
private List<SelectItem> movies = Arrays.asList(
new SelectItem("Scarface", "Scarface", "green"),
new SelectItem("Goodfellas", "Goodfellas", "red"),
new SelectItem("Godfather", "Godfather", "blue"),
new SelectItem("Carlito's Way", "Carlito's Way", "yellow"));
public List<SelectItem> getMovies() {
return movies;
}
}
Css文件:
.ui-chkbox-box {
background-image : none;
}
附注:css文件可能不是必需的。
https://stackoverflow.com/questions/12420004
复制相似问题