简体   繁体   中英

Adding / removing components dynamically in JSF2 / Primefaces

I have found similar questions while doing some research, but none of them really addressed my problem.

I have a form with some input criteria and a submit button. When clicking submit, a primefaces datatable is populated below the form with some results. What I need is to show different tables depending on the data entered without refreshing the whole page. Eg if a user enters Person as a value in the form, results for Person table are shown. However, if the user selects Company, results for Company are shown with its corresponding columns.

Just make use of update attribute to update the datatable. Further you can use <p:columns> to generate columns dynamically.

<p:inputText value="#{bean.input}" />
<p:commandButton value="submit" action="#{bean.submit}" update="table" />

<p:dataTable id="table" value="#{bean.model}" var="item">
    <p:columns value="#{bean.columns}" var="column">
        <h:outputText value="#{item[column]}" />
    </p:columns>
</p:dataTable>

with something like:

public void submit() {
    model = populateModelBasedOn(input);
    columns = populateColumnsBasedOn(input);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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