jsfdatatablemyfacesfacet

how to add ID attribute to header of h:datatable


here is a code snippet of datatable in myfaces.

<h:dataTable class="pretty" value="#{someValue}" >
    <h:column>
        <f:facet name="header">
            <h:outputText value="Name"/>
         </f:facet>
    </h:column>
    <h:column>
        <f:facet name="header">
             <h:outputText value="Outstanding"/>
        </f:facet>
    </h:column>       
</h:dataTable> 

This would produce html with

<th></th> 

for the table. I am trying to set the id and header as below to the th tag:

<th id="value" headers="header value">

but not sure how to do it in myfaces f:facet as it only provides name to be set along with some other values. Any help to set the id and headers to the th tag would be highly appreciated.

Thanks, Madhu


Solution

  • Fixed the issue by creating a custom renderer which extends the:

    org.apache.myfaces.renderkit.html.HtmlTableRenderer
    

    Modified the function to add the extra bit of attributes:

    protected void renderColumnHeaderCell(FacesContext facesContext, 
            ResponseWriter writer, UIComponent uiComponent, UIComponent facet,
            String headerStyleClass, int colspan) throws IOException
    

    And then adding the config in the faces-config.xml to point to our class:

    <render-kit>
        <renderer>
            <component-family>javax.faces.Data</component-family>
            <renderer-type>javax.faces.Table</renderer-type>
            <renderer-class>OurClass</renderer-class>
        </renderer>
    </render-kit>