Sonntag, 3. Januar 2010

Icefaces data table: attribute type specific ouput rendering

Icefaces

I promised in my last post Spring context configured JSF tables to add some more custom icefaces table features.

Based on this post I want to start with "attribute type specific output rendering".

dataTableTemplate.xhtml:
<ice:dataTable value="#{items}"
    var="item"
     rows="10">
    <ice:columns value="#{tableModel.columnModel}" 
        var="columnConfig">
        <f:facet name="header">
            <ice:panelGroup>
                <ice:commandSortHeader columnName="#{columnConfig.field}" >
                    <ice:outputText value="#{columnConfig.label}"/>
                </ice:commandSortHeader>
            </ice:panelGroup>
        </f:facet>
        <ice:outputText value="#{tableModel.cellValue}"/>
    </ice:columns>
</ice:dataTable>

The <ice:outputText value="#{tableModel.cellValue}"/> renders every column as a simple text. To make the rendering type dependent we have to add the column type to the ColumnConfig.

public class ColumnConfig
{

    private String label;

    private String field;

    private Class< ? > clazz = String.class;

    public boolean isString() {
        return clazz.equals(String.class);
    }
    public boolean isBoolean() {
        return clazz.equals(Boolean.class);
    }
    ...
}

web-context.xml:
<bean id="viewUsersBean" class="com.bamboit.webapp.controller.ViewUsersBean" scope="request">
    <constructor-arg index="0" value="com.bamboit.model.User"/><constructor-arg index="1">
        <list>
            <bean class="com.bamboit.model.ColumnConfig">
                <property name="field" value="firstName"/>
                <property name="label" value="user.firstName"/>
            </bean>
            <bean class="com.bamboit.model.ColumnConfig">
                <property name="field" value="lastName"/>
                <property name="label" value="user.lastName"/>
            </bean>
            <bean class="com.bamboit.model.ColumnConfig">
                <property name="field" value="active"/>
                <property name="label" value="user.active"/>
                <property name="clazz" value="java.lang.Boolean"/>
            </bean>
        </list>
    </constructor-arg>
</bean>

The user gets a further boolean attribute named "active". We want to render this as a checkbox.

<ice:dataTable value="#{items}"
    var="item"
     rows="10">
    <ice:columns value="#{tableModel.columnModel}" 
        var="columnConfig">
        <f:facet name="header">
            <ice:panelGroup>
                <ice:commandSortHeader columnName="#{columnConfig.field}" >
                    <ice:outputText value="#{columnConfig.label}"/>
                </ice:commandSortHeader>
            </ice:panelGroup>
        </f:facet>
        <ice:outputText value="#{tableModel.cellValue}" rendered="{columnConfig.string}"/>
        <ice:panelGroup style="text-align:center;" rendered="#{columnConfig.boolean}">
            <ice:selectBooleanCheckbox value="#{tableModel.cellValue}" />
        </ice:panelGroup>
    </ice:columns>
</ice:dataTable>

With the "rendered" attribute either outputText or selectBooleanCheckbox will be rendered depending on the column type. With this technique the column ouput can be customized for different data types like date/timestamp, numbers or what ever you want.

Keine Kommentare:

Kommentar veröffentlichen

Kommentar veröffentlichen