Jsf 将样式添加到特定的p:行

Jsf 将样式添加到特定的p:行,jsf,primefaces,Jsf,Primefaces,我将JSF与PrimeFaces3.5结合使用。我使用不带columns属性的p:panelGrid,而是使用p:row和p:column显式创建行和列,如showcase中所示 现在我需要在CSS类的帮助下改变一行的样式。但要么我错过了它,要么就是根本没有办法将类添加到p:row?!我甚至可以设置属性styleClass,但在渲染输出中会忽略它 有没有办法通过类别来区分panelGrid中的一行?尝试使用css上的通配符来计算以id结尾的所有td 像这样,选择id以myRowId结尾的所有td

我将JSF与PrimeFaces3.5结合使用。我使用不带columns属性的p:panelGrid,而是使用p:row和p:column显式创建行和列,如showcase中所示

现在我需要在CSS类的帮助下改变一行的样式。但要么我错过了它,要么就是根本没有办法将类添加到p:row?!我甚至可以设置属性styleClass,但在渲染输出中会忽略它


有没有办法通过类别来区分panelGrid中的一行?

尝试使用css上的通配符来计算以id结尾的所有td

像这样,选择id以myRowId结尾的所有td

这里有一个

先前的答案

由于无法在p:row上使用styleClass,因此可以尝试以下方法


为p:row分配一个id,如下所示:如果您在其他行中需要相同的样式,也许您可以根据您对Daniel的响应使用p:column中的列样式。大概是这样的:

并对xhtml文件进行int。。。我不知道为什么至少在PrimeFaces 6.2版之前,styleClass属性在默认情况下会被忽略,但是您可以创建一个自定义呈现程序,将其值附加到HTML输出中。默认PrimeFaces渲染器的简单替换如下所示:

public class PanelGridBodyRowRenderer extends CoreRenderer implements HelperRowRenderer {

    @Override
    public void encode(FacesContext context, Row row) throws IOException {
        ResponseWriter writer = context.getResponseWriter();
        String rowStyleClass = PanelGrid.TABLE_ROW_CLASS;
        String userRowStyleClass = row.getStyleClass();
        if (userRowStyleClass != null) {
            rowStyleClass = rowStyleClass + " " + userRowStyleClass;
        }

        writer.startElement("tr", row);
        writer.writeAttribute("class", rowStyleClass, null);
        writer.writeAttribute("role", "row", null);
        renderChildren(context, row);
        writer.endElement("tr");
    }

}
对于PrimeFaces版本6.2,您只需在WAR中的包org.PrimeFaces.component.row.renderer中创建此渲染器类。然后,类加载器将加载渲染器,而不是PrimFaces JAR中相同的渲染器类


有关自定义组件和渲染器的更多信息,请参阅答案。

只是想知道您是否可以像这样滥用primefaces pass-through属性,我们仍在使用JSF 2.1 JEE 6,因此我们不能利用这个想法,尽管我会注意这一点。对于这一行,我甚至没有唯一的ID。即使我有一个,我也不知道它,所以我不能给它附加样式…我编辑了我的答案,如果你的客户不使用IE,你可以使用它,我想你使用p:panelGrid有什么特别的好处吗?您可以始终使用显式xhtml标记,并且不受任何限制地分配类。这将允许我只分配一行,谢谢。但是如果我在几个不同的p:panelGrids中有几行或相同的逻辑,我会有几十个CSS规则…:-
#myFormId\3A SomeNamingContainer\3A myRowId {
    background-color: red;
}
.stylePerColumn {
    background-color: #F22626;
    color: black;
    border-style: none !important;
}
public class PanelGridBodyRowRenderer extends CoreRenderer implements HelperRowRenderer {

    @Override
    public void encode(FacesContext context, Row row) throws IOException {
        ResponseWriter writer = context.getResponseWriter();
        String rowStyleClass = PanelGrid.TABLE_ROW_CLASS;
        String userRowStyleClass = row.getStyleClass();
        if (userRowStyleClass != null) {
            rowStyleClass = rowStyleClass + " " + userRowStyleClass;
        }

        writer.startElement("tr", row);
        writer.writeAttribute("class", rowStyleClass, null);
        writer.writeAttribute("role", "row", null);
        renderChildren(context, row);
        writer.endElement("tr");
    }

}