Diganta
Diganta

Reputation: 691

How I conditionally stop execution <p:dataTable>'s page event during page change

In my application I have a <p:dataTable> with pagination using lazy datamodel.

I have a requirement when I click another page button from a page, a confirmation popup should generate, if I click yes then I can go to another page and if I click 'no' then I can't go to another page and I will stay in that current page.

.xhtml code are given below:

<h:form id="userListForm">
    <p:dataTable var="user" id="userTable" value="#{userListController.userDataModel}" lazy="true" paginator="true" rows="25" paginatorPosition="bottom"
            paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
            rowsPerPageTemplate="25,50,100" widgetVar="userDataTable" styleClass="userTable" selectionMode="single" >
       <p:ajax event="page" />

       <p:column id="nameColumn" headerText="#{adbBundle['name']}"
           sortBy="#{user.fullName}" filterBy="#{user.fullName}" styleClass="userName">
           <h:outputText value="#{user.fullName}" />
       </p:column>

       <!-- other columns -->

    <:/p:dataTable>
</h:form>

I am using primefaces 3.4, jsf 2.0.

Upvotes: 0

Views: 984

Answers (2)

Alexandre Jacob
Alexandre Jacob

Reputation: 3041

Inspired from RongNK's answers, you may simply do this,

add this script block :

<script type="text/javascript">
    function onPageStart() {
        return confirm('Are you sure?');
    }
</script>

and put this in your datatable :

<p:ajax event="page" onstart="return onPageStart()" />

You could also inline it in the <p:ajax this way :

<p:ajax event="page" onstart="return confirm('Are you sure?');" />

Tested and working with PrimeFaces 3.4.2

Upvotes: 1

Rong Nguyen
Rong Nguyen

Reputation: 4189

Did you try:

<p:ajax event="page" onstart="return tests()"/>

<script type="text/javascript">
                var vpage = 1;
                var prevvalue = vpage;
                function tests(){
                    if(confirm('Are you sure?')){
                        var p = userDataTable.getPaginator();
                        vpage = p.getCurrentPage();
                        prevvalue = vpage;
                        return true;}
                    else {
                        var p = userDataTable.getPaginator();
                        p.setPage(prevvalue,true);
                        return false;}
                }
            </script>

Upvotes: 0

Related Questions