0

Is it valid to have a JSF ui:repeater inside a h:dataTable for dynamically defining the columns? The method getFieldNames(...) is never called even though it is referenced from the html. The method getEntities is called. Any ideas?

@Named(value = "data")
@RequestScoped
public class MyBean {

    List<Map<String, String>> data = new ArrayList<Map<String, String>>();

    public CustomBean() {
        final AtomicInteger i = new AtomicInteger(0);
        for (final String fieldName : new String[]
            {"ColumnOne", "ColumnTwo", "ColumnThree"}) {
            data.add(new HashMap<String, String>() {

                {
                    put(fieldName, fieldName + "_" + i.getAndDecrement());
                }
            });
        }
    }

    public List<String> getFieldNames() {
        // THIS METHOD DOES NOT GET CALLED
        return new ArrayList<String>(data.get(0).keySet());
    }

    public List<Map<String, String>> getEntities() {
        return data;
    }
}



<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets">
    <h:head>
        <title>Title</title>
    </h:head>
    <h:body>
        <h:dataTable value="#{data.entities}" var="entity">
            <ui:repeat var="fieldName" value="#{data.fieldNames}">
                <h:column>
                    <f:facet name="header">#{fieldName}</f:facet>
                    #{entity.get(fieldName)}
                </h:column>
            </ui:repeat>
        </h:dataTable>
    </h:body>
</html>
auser
  • 6,307
  • 13
  • 41
  • 63
  • Following Q/As may sched some light on your topic: http://stackoverflow.com/questions/8945544/uirepeat-and-hpanelgrid, http://stackoverflow.com/questions/3342984/jstl-in-jsf2-facelets-makes-sense – Yuriy Nakonechnyy Jun 25 '12 at 13:15

1 Answers1

1

No, it's not possible to use ui:repeat for having dynamic columns inside a data table. You can try a component library such as Primefaces or Richfaces, which allows you to have dynamic columns easily. In this link you can see Primefaces' dynamic columns feature in action. Also, you may try to create the table model dynamically in the backing bean.This BalusC's tutorial explains how to do it.

damian
  • 4,024
  • 5
  • 35
  • 53