Petr Mensik
Petr Mensik

Reputation: 27496

Render a form content on Ajax event

How do you solve this common problem?Lets say you need to render few form elements depending on some Ajax event(but not the whole form) and you don't want to break forms layout. Here is sample code

<h:form>
    <h:panelGrid columns="2">
        text1
        <h:inputText value="asasd" />
        text2
        <h:inputText value="asdasd" />
        <p:selectBooleanCheckbox value="#{bean.myBoolean}">
            <p:ajax update="someId" listener="{bean.changeValueOfMyBoolean"} />
        </p:selectBooleanCheckbox>
        <h:outputText value="asdas"rendered="#{bean.myBoolean}" />
        <h:inputText value="bean value" rendered="#{bean.myBoolean"} />
        ...few more some elements

So I see only two options, put them in <h:panelGroup>, give all of them one id and create another <h:panelGrid> inside (which breaks forms layout, because the newly rendered elements will have different indent) or give each element it's own paneGroup with different id a then in <p:ajax>use something like this update=id1, id2, ...but that also doesn't seem to me like a good solution.

So what is your approach, is there a better way?

Upvotes: 2

Views: 227

Answers (1)

mprabhat
mprabhat

Reputation: 20323

Either you give all the ids which you want to be updated in the update or

You can give all the related components id which have something in common, something like

inputText1, inputText2, inputText3 and like

then in your managed bean have one variable which will return the ids of all these elements.

On ajax event, set the values (You can use regular expression to get all the ids in the view).

But I think that's the round about way, since this requires a trip to server, instead I would place the ids in update.

Upvotes: 1

Related Questions