vojtak
vojtak

Reputation: 55

How to detect update of PrimeFaces dataTable

I have quite simple and straightforward question. Is there any method how to detect change in dataTable?

Let's say, I have a dataTable using pagination. And now, when I go from page 1 to page 2 I need to detect this event and update gMap component on the same page (I need the gMap to refresh its dataModel from backing bean). I tried to use update parameter, but as PF documentation mentions, it's deprecated and didn't work.

I'm able to update gMap by manually clicking on p:button, but I need to do it automatically after dataTable change.

Maybe there is a solution by using javascript and binding datatable widget to some action, but I don't really know :(

Thanks in advance for any help


UPDATE

My method for initializing LazyDataMethod is:

private LazyDataModel<Poi> lazyModel;

private void initializeLazyModel() {
    lazyModel = new LazyDataModel<Poi>() {

        @Override
        public List<Poi> load(int first, int pageSize, String sortField, boolean sortOrder, Map<String, String> filters) {                
            List<Poi> data = service.getPoisLazily(first, pageSize, sortField, sortOrder, filters);
            pois = data;
            createMapForPOIs(pois);
            return data;
        }
    };

    lazyModel.setRowCount(getNumberOfRows());
}

Next there are only getter/setter for LazyDataModel:

public LazyDataModel<Poi> getLazyModel() {
    return lazyModel;
}

public void setLazyModel(LazyDataModel<Poi> lazyModel) {
    this.lazyModel = lazyModel;
}

In pois.xhtml I have datatable declaration as follows:

<p:dataTable update="formMap:map" id="tablePois" var="poi" value="#{poiController.lazyModel}"  lazy="true"
                         paginator="true" rows="10" paginatorPosition="bottom"
                         selection="#{poiController.poi}" rowSelectListener="#{poiController.showPoi}" selectionMode="single"
                         paginatorTemplate="{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"  
                         rowsPerPageTemplate="5,10,15,30,50">                                                       

                <p:column>  
                    <h:outputText value="#{poi.name}" />
                </p:column>
</p:dataTable>

So at this point I'd like to use

RequestContext context = RequestContext.getCurrentInstance(); 
context.addPartialUpdateTarget("form:gMapId");

as suggested by @spauny. But don't know where in the backing bean to put it.

Upvotes: 0

Views: 6459

Answers (1)

spauny
spauny

Reputation: 5096

Hoping you use Primefaces 3.0.M4...

There may be two solutions: the first involves only Primefaces, and to be more specific, the RequestContext component. Because of the power of Primefaces you can get(server side) a RequestContext instance and use a method named addPartialUpdateTarget. After the user requests another page, the method returning the list is called again; this is the best time for you to update the gMap(server-side):

RequestContext context = RequestContext.getCurrentInstance(); 
context.addPartialUpdateTarget("form:gMapId");

You can find info here: http://www.primefaces.org/showcase-labs/ui/requestContext.jsf and of course you can read the Primefaces User Guide for 3.0.M4.

And there is also another solution which of course it's far worse that the first(in this context), but could help you in another situations: the dataTable pagination it's lazy(that means an ajax request is made every time the user requests for another page of results), so you can intercept the ajax request by using ajaxStatus component ( http://www.primefaces.org/showcase-labs/ui/ajaxStatusHome.jsf - see both declarative and programmatic ) and onstart you can call a javascript function (that could have ajax capabilities) - of course the downsides are that you would have to implement that function but the worst is that you can implement just one ajax status per page(that means every ajax request would call you javascript function with ajax capabilities).

So you better use RequestContext, but ajaxStatus it's a very nice and important component: you can show a loading gif for every ajax request, or call a javascript function that shows the user some nice/interesting things(in page).

Upvotes: 1

Related Questions