2013-11-15 42 views
1

我正在尝试为我的<p:dataTable>添加一个全局筛选器,其中我通过托管bean以编程方式创建了该筛选器。该表正常工作并呈现正确,但只有最后添加的组件呈现在datatable方面。这里是我试过的代码:如何以编程方式/动态地将组件添加到p: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); 

回答

2

像正常XHTML/Facelets的代码,<f:facet>只能有一个孩子

这一点,因为在你的代码注释指出,

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

已经是无效的。它在Facelets中也不会有效。您需要将其包装在<h:panelGroup>中。

<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在这方面通常要少得多并且维护更友好。

+0

我能说什么男人,你只是天才,谢谢你,我也有column.setFilterBy()和column.setSortBy()问题,或者我应该问一个单独的问题。 –

+0

不客气。是的,问一个单独的问题,这与当前问题中提到的以编程方式创建方面无关。 – BalusC

+0

好的,再次感谢你。 –

相关问题