oncomplete for p:ajax event is always called, even if the listener returns nothing

Viewed 618

I have a dataTable with different rows, when i select the particular rows i don't want to perform any onClick event on the particular row. I want to prevent calling show() method in oncomplete. I have tried different ways but, i am unable to stop it.

Here is my code given below.

<h:form id="availableBooksForm">
        <p:messages />
        <p:panel>
            <p:remoteCommand name="show" action="#{boAction.selectPromotion}" />

            <p:dataTable id="BooksTable" value="#{userProfile.availableBooks}"
                var="res" selectionMode="single" rowKey="#{res.promotionId}"
                rows="#{referenceData.recordsPerPage}" lazy="true"
                resizableColumns="true" paginator="true"
                paginatorTemplate="#{referenceData.paginatorTemplate}"
                paginatorPosition="bottom" paginatorAlwaysVisible="false"
                style="margin-top: 5px; text-overflow: ellipsis;">

                <p:ajax event="rowSelect" listener="#{boAction.onSelectPromotion}"
                    oncomplete="show()" />

                <p:column headerText="#{msg.promotionInfo}">
                    <p:panelGrid columns="2" columnClasses="pct50,"
                        style="font-size: 12px">

                        <h:outputText value="#{msg.promoCode}" />
                        <h:outputText value="#{res.promoCode}">
                            <f:convertNumber pattern="#{userProfile.displayStringFormat}" />
                        </h:outputText>

                        <h:outputText value="#{msg.promoName}" />
                        <h:outputText value="#{res.promoName}">
                            <f:convertNumber pattern="#{userProfile.displayStringFormat}" />
                        </h:outputText>
                    </p:panelGrid>
                </p:column>
            </p:dataTable>
        </p:panel>

here i am calling the onSelectPromotion method to return nothing as shown in the below:

public void onSelectPromotion(SelectEvent event) {

    String selectedType = ((AllBooks) event.getObject()).getPromoType();
    if(selectedType.equals(BookType.getBookType)) {
        return;
    }
}

after this the show() method is calling the boAction.selectPromotion action. I dont want to call this method. How can i prevent this by selecting particular row from the dataTable.

I don't want to execute this method if i select the row from the table.

public String selectPromotion() {
    //some code is executing here
}
2 Answers

Using return in your Ajax listener will not prevent the oncomplete from being called, it will just stop execution of the method. Basically your listener is useless. See:

But you can solve your problem differently. In your p:dataTable use the selection attribute to keep a reference to the currently selected row. For example selection="#{userProfile.selectedBook}" (add selectedBook to your bean and make sure there are getters and setters). Then in the selectPromotion method you can simply get the selectedBook from your bean and decide whether you need to end or continue to process the remaining code.

You can see the selection attribute in action in the showcase.

I have tried with the below code snippet in the xhtml file , i am not allowing to select the row.

<p:dataTable id="BooksTable" value="#{userProfile.availableBooks}"
            var="res" selectionMode="single" rowKey="#{res.promotionId}"
            rows="#{referenceData.recordsPerPage}" lazy="true"
            resizableColumns="true" paginator="true"
            paginatorTemplate="#{referenceData.paginatorTemplate}"
            paginatorPosition="bottom" paginatorAlwaysVisible="false"
            style="margin-top: 5px; text-overflow: ellipsis;" 
            disabledSelection="#{(res.promoName == 'Your String')}">

by adding the disabledSelection with the condition, i am not allowing to select particular row in the dataTable using the code: disabledSelection="#{(res.promoName == 'Your String')}"

if you have integer type of value to compare you can use the below condition to check as disabledSelection="#{(res.promoName == 123456)}"

Related