Jim
Jim

Reputation: 117

how to reload p:dataGrid programmatically?

I have a datagrid with subscribe and unsubscribe option so the user can subscribe and unsubscribe and vise verse. now I want to reload the datagrid after subscription or unsubscription

I load the data of the grid like that

@PostConstruct
    public void init() {

        packages = packagehelper.getAllPackages();

        getCurrentUserSubscritions();
    }

and here is the xhtml file

<h:form id="form">
            <p:growl id="growl" showDetail="true" sticky="false" life="8000" />  
            <p:dataGrid  var="package" value="#{packageView.packages}" columns="3" 


                         >
                <p:column>
                    <f:facet name="header">
                        Cars for Sale
                    </f:facet>

                    <p:panel  header="#{package.id}" style="text-align:center">
                        <h:panelGrid  columns="2" style="width:100%">
                            <p:graphicImage width="100px" name="images/#{package.imageurl}"/> 
                            <h:outputText value="#{package.name}" />
                            <h:outputText value="#{package.value} EGP for #{package.duration} Days " />
                            <h:outputText value="#{package.description}" />

                            <p:commandButton ajax="true" update=":pack" value="Subscribe" rendered="#{!packageView.IsPackageActive(package.id)}"  action="#{packageView.Subscribe()}"  >
                                <f:setPropertyActionListener value="#{package.id}" target="#{packageView.packageID}" />
                                <f:setPropertyActionListener value="#{package.duration}" target="#{packageView.packageDuration}" />
                            </p:commandButton>


                            <p:commandButton action="#{packageView.Unsubscribe}" update=":pack" ajax="true" value="Cancel" rendered="#{packageView.UserHasPackage(package.id) and packageView.IsPackageActive(package.id)  }">

                               <f:setPropertyActionListener value="#{package.id}" target="#{packageView.packageID}" />
                                <f:setPropertyActionListener value="#{package.duration}" target="#{packageView.packageDuration}" />
                            </p:commandButton>


                        </h:panelGrid>
                    </p:panel>
                </p:column>
            </p:dataGrid>
        </h:form>

Upvotes: 1

Views: 1244

Answers (1)

Cristian Arteaga
Cristian Arteaga

Reputation: 550

When an suscribe or unsuscribe action is completed, you should actualize the DataGrid trough its ID, it is, set an ID to the DataGrid and put such id in the update property of the buttons. For example:

<p:dataGrid id="datalist" .../>

<p:commandButton action="#{packageView.Unsubscribe}" update="datalist" .../>

And when the action of the button is submited set the packages in null, so when the update action of the DataGrid update the packages they should be called again of the persistence or data base.

It is important to consider how to reference the elements in update property. You can see this answer

Upvotes: 2

Related Questions