简体   繁体   English

在JSF中,在ah:dataTable中包含ui:repeater是否有效?

[英]In JSF is it valid to have a ui:repeater inside a h:dataTable

Is it valid to have a JSF ui:repeater inside ah:dataTable for dynamically defining the columns? 在ah:dataTable中具有JSF ui:repeater以动态定义列是否有效? The method getFieldNames(...) is never called even though it is referenced from the html. 即使从html引用,也不会调用getFieldNames(...)方法。 The method getEntities is called. 调用方法getEntities。 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>

No, it's not possible to use ui:repeat for having dynamic columns inside a data table. 不,不能使用ui:repeat在数据表中包含动态列。 You can try a component library such as Primefaces or Richfaces, which allows you to have dynamic columns easily. 您可以尝试使用诸如Primefaces或Richfaces之类的组件库,它使您轻松拥有动态列。 In this link you can see Primefaces' dynamic columns feature in action. 链接中,您可以看到Primefaces的动态列功能的使用。 Also, you may try to create the table model dynamically in the backing bean.This BalusC's tutorial explains how to do it. 另外,您可能会尝试在支持Bean中动态创建表模型。此BalusC的教程介绍了如何做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM