如何将自定义 CSS 类分配给 h:dataTable 的任意任意行? [英] How to assign custom CSS class to arbitrary arbitrary rows of h:dataTable?

查看:22
本文介绍了如何将自定义 CSS 类分配给 h:dataTable 的任意任意行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将特定的 CSS 类分配给我的 的特定行.有什么方法可以访问和自定义生成的表行吗?

I'm trying to assign a specific CSS class to specific rows of my <h:dataTable>. Is there some way to access and cutomize the resulting table rows?

推荐答案

rowClasses 属性绑定到 bean 属性,该属性返回所需的 CSS 类字符串.

Bind the rowClasses attribute to a bean property which returns the desired string of CSS classes.

<h:dataTable value="#{bean.list}" rowClasses="#{bean.rowClasses}">

例如

public String getRowClasses() {
    StringBuilder rowClasses = new StringBuilder();
    for (Item item : list) {
        if (rowClasses.length() > 0) rowClasses.append(",");
        rowClasses.append(item.getRowClass());
    }
    return rowClasses.toString();
}

<小时>

更新 澄清一下,这样您就可以完全编程控制 rowClasses 字符串.请注意,以上只是一个启动示例,不一定需要通过 Item#getRowClass() 左右获取.您甚至可以在带有计数器的简单 for 循环中执行此操作.


Update to clarify, this way you have full programmatic control over the rowClasses string. Note that the above is just a kickoff example, it doesn't necessarily need to be obtained by Item#getRowClass() or so. You can even do it in a simple for loop with a counter.

例如

public String getRowClasses() {
    StringBuilder rowClasses = new StringBuilder();
    for (int i = 0; i < list.size(); i++) {
        if (rowClasses.length() > 0) rowClasses.append(",");
        rowClasses.append(selected.contains(i) ? "selected" : "none");
    }
    return rowClasses.toString();
}

其中 selectedList.如果它包含 1、2 和 5,那么对于 10 个项目的列表,返回的字符串将如下所示:

where selected is a List<Integer>. If it contains 1, 2 and 5, then the returned string will look like as follows for a list of 10 items:

none,selected,selected,none,none,selected,none,none,none,none

这篇关于如何将自定义 CSS 类分配给 h:dataTable 的任意任意行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆