Question

I'm trying to add a global filter for my <p:dataTable> of which I create programmatically from a managed bean. The table works fine and renders correctly, however only the last added component is rendered in the datatable facet. Here is the code I tried:

    //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);
Was it helpful?

Solution

Like in normal XHTML/Facelets code, a <f:facet> can have only one child.

This, as indicated in your code comment,

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

is already invalid. It wouldn't have worked in Facelets either. You need to wrap it in a <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> 

Just do the same in Java code. Remember: there's nothing which can only be done in Java code and not in XHTML, or vice versa. Everything which is possible in Java is also possible using pure XHTML (unlike JSP). Only difference is that XHTML is generally much less verbose and more maintenance friendly in this area.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top