【问题标题】:how to add component programmatically/dynamically to a p:dataTable facet如何以编程/动态方式将组件添加到 p:dataTable facet
【发布时间】:2013-11-28 17:04:58
【问题描述】:

我正在尝试为我的 <p:dataTable> 添加一个全局过滤器,我通过托管 bean 以编程方式创建该过滤器。该表工作正常并且可以正确呈现,但是只有最后添加的组件会在 datatable 方面呈现。这是我尝试过的代码:

    //config
    FacesContext fc = FacesContext.getCurrentInstance();
    Application application = fc.getApplication();
    ExpressionFactory ef = application.getExpressionFactory();
    ELContext elc = fc.getELContext();

    //Table
    table = (DataTable) application.createComponent(DataTable.COMPONENT_TYPE);
    table.setId("tabexam");
    table.setValue(listexam);
    table.setVar("exam");
    table.setWidgetVar("examTable");
    table.setEmptyMessage("aucun résultat trouvé pour votre recherche");
    table.setFilteredValue(filteredexams);
    table.setPaginator(true);
    table.setPaginatorPosition("bottom");
    table.setRows(25);
    table.setPaginatorTemplate("{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}");
    table.setRowsPerPageTemplate("10,25,50,100");


    /////////this is the part that regards this question///////////
    /*
     this is the HTML code that i want to translate to java code :
     <f:facet name="header" >  
     <h:outputText value="Rechercher:  "/>  
     <p:inputText id="globalFilter" onkeyup="examTable.filter();" style="width:200px" /> 
     </f:facet> 
    */
    UIOutput tableTitle = (UIOutput) application.createComponent(UIOutput.COMPONENT_TYPE);

    tableTitle.setValue("Rechercher :");

    HtmlInputText globalfilterinput = (HtmlInputText) application.createComponent(HtmlInputText.COMPONENT_TYPE);
    globalfilterinput.setId("globalFilter");
    ValueExpression globalfilterJSaction = ef.createValueExpression(elc, "examTable.filter()", Object.class);
    globalfilterinput.setValueExpression("onkeyup", globalfilterJSaction);


    Map comps = new HashMap();

    comps.put("header", tableTitle);
    comps.put("header", globalfilterinput);

    table.getFacets().putAll(comps);

    /////////////////////////////////////////////////// 

    //Index
    Column indexColumn = (Column) application.createComponent(Column.COMPONENT_TYPE);
    UIOutput indexColumnTitle = (UIOutput) application.createComponent(UIOutput.COMPONENT_TYPE);
    indexColumnTitle.setValue("Index");
    indexColumn.getFacets().put("header", indexColumnTitle);
    //table.getColumns().add(column);
    table.getChildren().add(indexColumn);

    ValueExpression indexValueExp = ef.createValueExpression(elc, "#{exam.examen.studyPatientState}", Object.class);
    HtmlOutputText indexOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
    indexOutput.setValueExpression("value", indexValueExp);
    indexColumn.getChildren().add(indexOutput);

    //Name Column
    Column nameColumn = (Column) application.createComponent(Column.COMPONENT_TYPE);
    UIOutput nameColumnTitle = (UIOutput) application.createComponent(UIOutput.COMPONENT_TYPE);
    nameColumnTitle.setValue("Name");
    nameColumn.getFacets().put("header", nameColumnTitle);
    table.getChildren().add(nameColumn);

    ValueExpression nameValueExp = ef.createValueExpression(elc, "#{exam.examen.rapport.rapportOraleState}", Object.class);
    HtmlOutputText nameOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
    nameOutput.setValueExpression("value", nameValueExp);
    nameColumn.getChildren().add(nameOutput);

【问题讨论】:

    标签: jsf primefaces datatable dynamically-generated facet


    【解决方案1】:

    与普通的 XHTML/Facelets 代码一样,&lt;f:facet&gt; 只能有 一个 子级。

    正如您的代码注释中所指出的那样,

    <f:facet name="header" >  
        <h:outputText value="Rechercher:  "/>  
        <p:inputText id="globalFilter" onkeyup="examTable.filter();" style="width:200px" /> 
    </f:facet> 
    

    已经无效。它也不会在 Facelets 中工作。您需要将其包装在 &lt;h:panelGroup&gt; 中。

    <f:facet name="header" >  
        <h:panelGroup>
            <h:outputText value="Rechercher:  "/>  
            <p:inputText id="globalFilter" onkeyup="examTable.filter();" style="width:200px" /> 
        </h:panelGroup>
    </f:facet> 
    

    只需在 Java 代码中执行相同操作即可。请记住:没有什么可以在Java代码中完成,而不能在XHTML中完成,反之亦然。使用纯 XHTML(与 JSP 不同)也可以在 Java 中实现一切。唯一的区别是 XHTML 在这方面通常不那么冗长,并且更易于维护。

    【讨论】:

    • 我能说什么,伙计,你真是个天才,谢谢,我也遇到了 column.setFilterBy() 和 column.setSortBy() 的问题,或者我应该为此问一个单独的问题.
    • 不客气。是的,问一个单独的问题,这与当前问题中提出的以编程方式创建构面无关。
    猜你喜欢
    • 2018-09-24
    • 1970-01-01
    • 2011-10-31
    • 2020-04-06
    • 2013-04-10
    • 1970-01-01
    • 2021-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多