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. E.g. 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.

Upvotes: 0

Views: 1428

Answers (1)

BalusC
BalusC

Reputation: 1108742

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);
}

Upvotes: 2

Related Questions