0

In th following code the action support for my input text field is not working as it should work(Line number 69-70 in vf code).I want to navigate to the page number entered by the user.Following is my controller and VF code: Code for controller

public class ContactListViewController {
public ContactlistViewController(){
    system.debug('constructor first called');
    RecordsPerPageslist=10;
    alphabet = new list<string>{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','Others','All'};
    allContactList = new list<wrapper>();
    for(contact c: (list<contact>)stdSetController.getRecords())
            allContactList.add(new wrapper(c));
    System.debug('constructor called');
}
Map<id,Boolean> m = new Map<id,boolean>();       // To store boolean values of checkboxes corrosponding to every contact id
list<contact> con = [SELECT Name,id,Account.name,Title,Phone,Email FROM Contact];
public list<wrapper> allContactList;            //Wrapper class object

public list<wrapper> getWrapperContacts(){      //List of wrapper class to display in table
    System.debug('wrapperContacts called');
    return allContactList;
}

public void SelectedListContacts(){         // Select contacts and save them in a map
   System.debug('SelectedListContacts called');
    for(wrapper wc:allContactList){
        m.put(wc.con.id,wc.isSelected);
    }

    //System.debug('getSelectedListContacts = '+m);

}
public void SelectedAllContacts(){          //To select all contacts in a page
    System.debug('SelectedAllContacts called');
    for(wrapper wc:allContactList){
        m.put(wc.con.id,wc.isSelected=true);
    }
}
public void next(){
    System.debug('next called');
    allContactList.clear();        
    this.stdSetController.next();
    for(contact c: (list<contact>)stdSetController.getRecords())
            allContactList.add(new wrapper(c)); 
    for(wrapper wc:allContactList){
        wc.isSelected=m.get(wc.con.id);
    }
}
public void previous(){
    System.debug('previous called');
    allContactList.clear();        
    this.stdSetController.previous();
    for(contact c: (list<contact>)stdSetController.getRecords())
            allContactList.add(new wrapper(c)); 
    for(wrapper wc:allContactList){
        wc.isSelected=m.get(wc.con.id);
}
}
public void last(){
    System.debug('last called');
    allContactList.clear();        
    this.stdSetController.last();
    for(contact c: (list<contact>)stdSetController.getRecords())
            allContactList.add(new wrapper(c)); 
    for(wrapper wc:allContactList){
        wc.isSelected=m.get(wc.con.id);
}
}
public void first(){
    System.debug('first called');
    allContactList.clear();        
    this.stdSetController.first();
    for(contact c: (list<contact>)stdSetController.getRecords())
            allContactList.add(new wrapper(c)); 
    for(wrapper wc:allContactList){
        wc.isSelected=m.get(wc.con.id);
}
}
public boolean getHasNext(){
    System.debug('getHasNext called');
    return stdSetController.getHasNext();
}
public boolean getHasPrevious(){
    System.debug('getHasPrevious called');
    return stdSetController.getHasPrevious();
}
public list<String> alphabet{get;set;}
public String alphaSearchConct{get;set;}                            // To get commandlink parameter for alphabet selected
public Pagereference alphaSearch(){                             //To update contact list as per the alphabet selected by the user
   System.debug('alphaSearch called');
    allContactList.clear();
    if (alphaSearchConct=='All'){
        con = [SELECT name,Account.name,Title,Phone,Email FROM contact];
    }
    else{
            con = [SELECT name,Account.name,Title,Phone,Email FROM contact WHERE lastName Like:alphaSearchConct+'%'];
    }
    ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(con);
    stdSetController= ssc;
    for(contact c: (list<contact>)stdSetController.getRecords())
            allContactList.add(new wrapper(c));
    system.debug('alphaSearchconct called');
    return null;
}      
public Integer PageNumber{
    get{                                                            //To get current page number
        System.debug('get of pageNumber called '+ PageNumber);
        this.PageNumber=stdSetController.getPageNumber();   
        return this.PageNumber;
    }
    set{  
        System.debug('set of pageNumber called');
        this.pageNumber=value;         
}
    }

public PageReference NavigateByText(){

    System.debug('getNavigateByText ');
    allContactList.clear();
    this.stdSetController.setPageNumber(PageNumber);
     //System.debug('getNavigateByText '+ stdSetController.getPageNumber()+'records are '+ stdSetController.getRecords());
        for(contact c:(list<contact>)stdSetController.getRecords())
            allContactList.add(new wrapper(c));
        //System.debug(stdSetController.getRecords());
       for(wrapper wc:allContactList){
        wc.isSelected=m.get(wc.con.id);
           System.debug(wc.isSelected);


}
    return null;
}
public Integer TotalPages{                                            // Total number of pages as per user selection of Records per page
    get{
         System.debug(TotalPages); 
        if(stdSetController.getResultSize() <=10)
               this.TotalPages=1;
          if(Math.Mod(stdSetController.getResultSize() ,stdSetController.getPageSize()) == 0)
              this.TotalPages =(stdSetController.getResultSize()/stdSetController.getPageSize());
          else
            this.TotalPages = (stdSetController.getResultSize()/stdSetController.getPageSize())+1;
          //System.Debug(this.TotalPages);
            return totalpages;
    }
    set;
}
public list<SelectOption> getRecordsPerPageOptionList(){              //To display a drop down list on vf page  
        list<SelectOption>  options = new list<SelectOption>();
        options.add(new selectOption('10','10'));
        options.add(new selectOption('25','25'));
        options.add(new selectOption('50','50'));
        options.add(new selectOption('100','100'));
        options.add(new selectOption('200','200'));
        return options;
}
public Integer RecordsPerPageslist{ 
    get;
    set{                                                          //To select number of records per page
        if(value!=null){
            this.RecordsPerPagesList=value;
            System.debug('RecordsPerPageList called');
        }
    }       
}
public Pagereference ChangeNumberOfRecordsPerPage(){
   System.debug('ChangeNumberOfRecordsPerPage called');
    allContactList.clear();
    for(contact c: (list<contact>)stdSetController.getRecords())
            allContactList.add(new wrapper(c)); 
     for(wrapper wc:allContactList)
        wc.isSelected=m.get(wc.con.id);
    return null;
} 
public ApexPages.StandardSetController stdSetController{            //Instantiating a standard set controller
    get{
        if(stdSetController==null){
             stdSetController = new ApexPages.StandardSetController(con);
        }
          stdSetController.setPageSize(RecordsPerPageslist);        //Limiting Number of records to be displayed per page 
            System.debug('stdSetController called ');
        return stdSetController;   
    }
    set;
}
public class wrapper{
  public boolean isSelected{get;set;}
  public Contact con{get;set;}

     wrapper(contact con){
        isSelected = false;
        this.con = con;
    }
}
}

Code for VF page:

<apex:page controller="ContactListViewController" sidebar="false">
<apex:form >
    <!-- For alphabetic search-->
    <div align="right">
        <apex:panelGrid >
            <apex:repeat value="{!alphabet}" var="alph">
                <apex:commandLink value="{!alph} | " action="{!getalphaSearch}" reRender="table">
                    <apex:param name="a" value="{!alph}" assignTo="{!alphaSearchConct}"/>
                </apex:commandLink>
            </apex:repeat>
        </apex:panelGrid>
    </div>
    <apex:PageBlock id="table">
        <apex:pageMessages />
        <apex:commandButton action="{!getSelectedAllContacts}" value="Select All" reRender="table"/>
        <apex:PageBlockTable value="{!WrapperContacts}" var="contacts" >
            <!-- To display and select Checkboxes-->
            <apex:column >
                <apex:facet name="header">
                    <apex:inputCheckbox value="{!contacts.isSelected}">
                        <apex:actionSupport event="onclick" action="{!getSelectedAllContacts}" reRender="table"/>
                            <apex:param value="{!contacts.isSelected}" assignTo="{!Allselection}"/>
                    </apex:inputCheckbox>   
                    </apex:facet>
                <apex:inputCheckbox value="{!contacts.isSelected}">
                    <apex:actionSupport event="onclick" action="{!getSelectedListContacts}" reRender="table"/>
                </apex:inputCheckbox>  
            </apex:column>
            <!-- To Edit and Delete a record -->
            <apex:column headerValue="Action">
                <apex:outputLink value="{!URLFOR($Action.Contact.Edit,contacts.con.id)}"> 
                    Edit |
                </apex:outputlink>
                <apex:outputLink value="{!URLFOR($Action.Contact.Delete,contacts.con.id)}"> 
                    Del |
                </apex:outputlink>
            </apex:column>
            <apex:column headerValue="Name">
                <apex:outputLink value="/{!contacts.con.id}">
                    {!contacts.con.name}
                </apex:outputLink>
            </apex:column>
            <apex:column headerValue="Account Name">
                <apex:outputLink value="/{!contacts.con.account.id}">
                    {!contacts.con.account.name}
                </apex:outputLink>
            </apex:column>
            <apex:column value="{!contacts.con.Title}"/>
            <apex:column value="{!contacts.con.Phone}"/>
            <apex:column value="{!contacts.con.email}"/>
            <apex:inlineEditSupport />
        </apex:PageBlockTable>
        </apex:PageBlock> 
    <!-- below code for pagination -->
    <apex:outputPanel id="button"> 
    <div align = "center" >
        <!-- To return to first page of records-->
        <apex:commandButton action="{!first}" value="<<" title="First Page" disabled="{!!HasPrevious}" reRender="table,button"/>
        <!-- To return to Previous page of records-->
        <apex:commandButton action="{!previous}" value="Previous" disabled="{!!HasPrevious}" reRender="table,button"/>
        <!-- To return to next page of records-->
        <apex:commandButton action="{!next}" value="Next >" disabled = "{!!HasNext}" reRender="table,button"/>
        <!-- To return to last page of records-->
        <apex:commandButton action="{!last}" value=">>" title="Last Page" disabled="{!!HasNext}" reRender="table,button"/>
        <!-- InputText to display current page and to navigate to any page number, At righmost side of page-->
        <span style="float:right">
            <apex:outputLabel value="Page ">
            </apex:outputLabel>
             <!-- To navigate to the page--> 
            <apex:InputText value="{!PageNumber}" maxLength="4" size="1">
            <apex:actionSupport event="onchange" action="{!NavigateByText}" reRender="table,button"/>
            </apex:InputText>
             <!-- The above action support is not working-->         
            <apex:outputLabel value=" of {!TotalPages}">
            </apex:outputLabel>
        </span>
        <!-- To display a list for number of records to be selected per page-->
        <span style = "float:left">
            <apex:SelectList value="{!RecordsPerPageslist}" size="1" >
                <apex:selectOptions value="{!RecordsPerPageOptionList}">    
                </apex:selectOptions>
                <apex:actionSupport event="onchange" action="{!getChangeNumberOfRecordsPerPage}" reRender="table,button"/>
            </apex:SelectList>
        </span>
    </div>
</apex:outputPanel>      
</apex:form>

1 Answer 1

3

The apex:actionSupport is working fine. The issue is, in the PageNumber getter setter, you are setting the earlier value in get method. So you are getting 1 whenever you are putting any value and it is showing page 1 result for any input value. Please remove the setter as mentioned below. Also, set the initial value of PageNumber to 1 in the constructor.

Also you have to add this.PageNumber=stdSetController.getPageNumber(); in previous and next method so that the text box will be updated with the latest page number when user clicks on previous & next button.

enter image description here

Code:

public class ContactListViewController {
    public ContactlistViewController(){
        system.debug('constructor first called');
        PageNumber = 1;
        ..........
        ..........
        ..........
    }
    ..............
    ..............
    ..............
public void next(){
    System.debug('next');
    allContactList.clear();        
    this.stdSetController.next();
    this.PageNumber=stdSetController.getPageNumber();  
    for(contact c: (list<contact>)stdSetController.getRecords())
        allContactList.add(new wrapper(c)); 
    for(wrapper wc:allContactList){
        wc.isSelected=m.get(wc.con.id);
    }
}
public void previous(){
    allContactList.clear();        
    this.stdSetController.previous();
    this.PageNumber=stdSetController.getPageNumber();  
    for(contact c: (list<contact>)stdSetController.getRecords())
        allContactList.add(new wrapper(c)); 
    for(wrapper wc:allContactList){
        wc.isSelected=m.get(wc.con.id);
    }
}       
    ..............
    ..............
    ..............
    public Integer PageNumber{
        get{                                                            //To get current page number
            System.debug('get of pageNumber called '+ PageNumber);   
            return this.PageNumber;
        }
        set{  
            System.debug('set of pageNumber called '+ value);
            this.pageNumber=value;         
        }
    }       
    ............
    ............
    ............

Screen Capture

enter image description here

Complete Code:

VF Page

<apex:page controller="ContactListViewController" sidebar="false">
    <apex:form >
        <!-- For alphabetic search-->
        <div align="right">
            <apex:panelGrid >
                <apex:repeat value="{!alphabet}" var="alph">
                    <apex:commandLink value="{!alph} | " action="{!getalphaSearch}" reRender="table">
                        <apex:param name="a" value="{!alph}" assignTo="{!alphaSearchConct}"/>
                    </apex:commandLink>
                </apex:repeat>
            </apex:panelGrid>
        </div>
        <apex:PageBlock id="table">
            <apex:pageMessages />
            <apex:commandButton action="{!getSelectedAllContacts}" value="Select All" reRender="table"/>
            <apex:PageBlockTable value="{!WrapperContacts}" var="contacts" >
                <!-- To display and select Checkboxes-->
                <apex:column >
                    <apex:facet name="header">
                        <apex:inputCheckbox value="{!contacts.isSelected}">
                            <apex:actionSupport event="onclick" action="{!getSelectedAllContacts}" reRender="table"/>
                            <apex:param value="{!contacts.isSelected}" assignTo="{!Allselection}"/>
                        </apex:inputCheckbox>   
                    </apex:facet>
                    <apex:inputCheckbox value="{!contacts.isSelected}">
                        <apex:actionSupport event="onclick" action="{!getSelectedListContacts}" reRender="table"/>
                    </apex:inputCheckbox>  
                </apex:column>
                <!-- To Edit and Delete a record -->
                <apex:column headerValue="Action">
                    <apex:outputLink value="{!URLFOR($Action.Contact.Edit,contacts.con.id)}"> 
                        Edit |
                    </apex:outputlink>
                    <apex:outputLink value="{!URLFOR($Action.Contact.Delete,contacts.con.id)}"> 
                        Del |
                    </apex:outputlink>
                </apex:column>
                <apex:column headerValue="Name">
                    <apex:outputLink value="/{!contacts.con.id}">
                        {!contacts.con.name}
                    </apex:outputLink>
                </apex:column>
                <apex:column headerValue="Account Name">
                    <apex:outputLink value="/{!contacts.con.account.id}">
                        {!contacts.con.account.name}
                    </apex:outputLink>
                </apex:column>
                <apex:column value="{!contacts.con.Title}"/>
                <apex:column value="{!contacts.con.Phone}"/>
                <apex:column value="{!contacts.con.email}"/>
                <apex:inlineEditSupport />
            </apex:PageBlockTable>
        </apex:PageBlock> 
        <!-- below code for pagination -->
        <apex:outputPanel id="button"> 
            <div align = "center" >
                <!-- To return to first page of records-->
                <apex:commandButton action="{!first}" value="<<" title="First Page" disabled="{!!HasPrevious}" reRender="table,button"/>
                <!-- To return to Previous page of records-->
                <apex:commandButton action="{!previous}" value="Previous" disabled="{!!HasPrevious}" reRender="table,button"/>
                <!-- To return to next page of records-->
                <apex:commandButton action="{!next}" value="Next >" disabled = "{!!HasNext}" reRender="table,button"/>
                <!-- To return to last page of records-->
                <apex:commandButton action="{!last}" value=">>" title="Last Page" disabled="{!!HasNext}" reRender="table,button"/>
                <!-- InputText to display current page and to navigate to any page number, At righmost side of page-->
                <span style="float:right">
                    <apex:outputLabel value="Page ">
                    </apex:outputLabel>
                    <!-- To navigate to the page--> 
                    <apex:InputText value="{!PageNumber}" maxLength="4" size="1">
                        <apex:actionSupport event="onchange" action="{!NavigateByText}" reRender="table,button"/>
                    </apex:InputText>
                    <!-- The above action support is not working-->         
                    <apex:outputLabel value=" of {!TotalPages}">
                    </apex:outputLabel>
                </span>
                <!-- To display a list for number of records to be selected per page-->
                <span style = "float:left">
                    <apex:SelectList value="{!RecordsPerPageslist}" size="1" >
                        <apex:selectOptions value="{!RecordsPerPageOptionList}">    
                        </apex:selectOptions>
                        <apex:actionSupport event="onchange" action="{!getChangeNumberOfRecordsPerPage}" reRender="table,button"/>
                    </apex:SelectList>
                </span>
            </div>
        </apex:outputPanel>      
    </apex:form>
</apex:page>

Apex Controller

public class ContactListViewController {
    public ContactlistViewController(){
        system.debug('constructor first called');
        PageNumber = 1;
        RecordsPerPageslist=10;
        allContactList = new list<wrapper>();
        for(contact c: (list<contact>)stdSetController.getRecords())
            allContactList.add(new wrapper(c));
        System.debug('constructor called');
    }
    Map<id,Boolean> m = new Map<id,boolean>();       // To store boolean values of checkboxes corrosponding to every contact id
    list<contact> con = [SELECT Name,id,Account.name,Title,Phone,Email FROM Contact];
    public list<wrapper> allContactList;            //Wrapper class object

    public list<wrapper> getWrapperContacts(){      //List of wrapper class to display in table
        return allContactList;
    }

    public void getSelectedListContacts(){          // Select contacts and save them in a map
        for(wrapper wc:allContactList){
            m.put(wc.con.id,wc.isSelected);
        }

        System.debug('getSelectedListContacts = '+m);

    }
    public void getSelectedAllContacts(){           //To select all contacts in a page
        for(wrapper wc:allContactList){
            m.put(wc.con.id,wc.isSelected=true);
        }
    }
    public void next(){
        System.debug('next');
        allContactList.clear();        
        this.stdSetController.next();
        this.PageNumber=stdSetController.getPageNumber();  
        for(contact c: (list<contact>)stdSetController.getRecords())
            allContactList.add(new wrapper(c)); 
        for(wrapper wc:allContactList){
            wc.isSelected=m.get(wc.con.id);
        }
    }
    public void previous(){
        allContactList.clear();        
        this.stdSetController.previous();
        this.PageNumber=stdSetController.getPageNumber();  
        for(contact c: (list<contact>)stdSetController.getRecords())
            allContactList.add(new wrapper(c)); 
        for(wrapper wc:allContactList){
            wc.isSelected=m.get(wc.con.id);
        }
    }
    public void last(){
        allContactList.clear();        
        this.stdSetController.last();
        for(contact c: (list<contact>)stdSetController.getRecords())
            allContactList.add(new wrapper(c)); 
        for(wrapper wc:allContactList){
            wc.isSelected=m.get(wc.con.id);
        }
    }
    public void first(){
        allContactList.clear();        
        this.stdSetController.first();
        for(contact c: (list<contact>)stdSetController.getRecords())
            allContactList.add(new wrapper(c)); 
        for(wrapper wc:allContactList){
            wc.isSelected=m.get(wc.con.id);
        }
    }
    public boolean getHasNext(){
        return stdSetController.getHasNext();
    }
    public boolean getHasPrevious(){
        return stdSetController.getHasPrevious();
    }
    public list<String> alphabet{
        get{                                                            //To display a list of alphabets on vf page 
            alphabet = new list<string>{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','Others','All'};
                return alphabet;     
        }
        set;
    }
    public String alphaSearchConct{get;set;}                            // To get commandlink parameter for alphabet selected
    public Pagereference getalphaSearch(){                              //To update contact list as per the alphabet selected by the user
        allContactList.clear();
        if (alphaSearchConct=='All'){
            con = [SELECT name,Account.name,Title,Phone,Email FROM contact];
        }
        else{
            con = [SELECT name,Account.name,Title,Phone,Email FROM contact WHERE lastName Like:alphaSearchConct+'%'];
        }
        ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(con);
        stdSetController= ssc;
        for(contact c: (list<contact>)stdSetController.getRecords())
            allContactList.add(new wrapper(c));
        system.debug('alphaSearchconct called');
        return null;
    }      
    public Integer PageNumber{
        get{                                                            //To get current page number
            System.debug('get of pageNumber called '+ PageNumber);
            //this.PageNumber=stdSetController.getPageNumber();   
            return this.PageNumber;
        }
        set{  
            System.debug('set of pageNumber called '+ value);
            this.pageNumber=value;         
        }
    }

    public PageReference NavigateByText(){
        System.debug('NavigateByText '+ stdSetController.getPageNumber());
        System.debug('getNavigateByText '+ stdSetController.getPageNumber());
        allContactList.clear();
        this.stdSetController.setPageNumber(PageNumber);
        System.debug('getNavigateByText '+ stdSetController.getPageNumber()+'records are '+ stdSetController.getRecords());
        for(contact c:(list<contact>)stdSetController.getRecords())
            allContactList.add(new wrapper(c));
        //System.debug(stdSetController.getRecords());
        for(wrapper wc:allContactList){
            wc.isSelected=m.get(wc.con.id);
            System.debug(wc.isSelected);
            System.debug('pageNumber called');

        }
        return null;
    }
    public Integer TotalPages{                                            // Total number of pages as per user selection of Records per page
        get{
            if(stdSetController.getResultSize() <=10)
                this.TotalPages=1;
            if(Math.Mod(stdSetController.getResultSize() ,stdSetController.getPageSize()) == 0)
                this.TotalPages =(stdSetController.getResultSize()/stdSetController.getPageSize());
            else
                this.TotalPages = (stdSetController.getResultSize()/stdSetController.getPageSize())+1;
            //System.Debug(this.TotalPages);
            return totalpages;
        }
        set;
    }
    public Integer MaxNumberOfRecords{                                    //Maximum number of records in a query list
        get{
            return stdSetController.getRecords().size();
        }
        set;
    }
    public list<SelectOption> getRecordsPerPageOptionList(){              //To display a drop down list on vf page  
        list<SelectOption>  options = new list<SelectOption>();
        options.add(new selectOption('10','10'));
        options.add(new selectOption('25','25'));
        options.add(new selectOption('50','50'));
        options.add(new selectOption('100','100'));
        options.add(new selectOption('200','200'));
        return options;
    }
    public Integer RecordsPerPageslist{ 
        get;
        set{                                                          //To select number of records per page
            if(value!=null){
                this.RecordsPerPagesList=value;
                System.debug('RecordsPerPageList called');
            }
        }       
    }
    public Pagereference getChangeNumberOfRecordsPerPage(){
        allContactList.clear();
        for(contact c: (list<contact>)stdSetController.getRecords())
            allContactList.add(new wrapper(c)); 
        for(wrapper wc:allContactList)
            wc.isSelected=m.get(wc.con.id);
        return null;
    } 
    public ApexPages.StandardSetController stdSetController{            //Instantiating a standard set controller
        get{
            if(stdSetController==null){
                stdSetController = new ApexPages.StandardSetController(con);
            }
            stdSetController.setPageSize(RecordsPerPageslist);        //Limiting Number of records to be displayed per page 
            System.debug('stdSetController called '+ stdSetController.getPageNumber());
            return stdSetController;   
        }
        set;
    }
    public class wrapper{
        public boolean isSelected{get;set;}
        public Contact con{get;set;}

        wrapper(contact con){
            isSelected = false;
            this.con = con;
        }
    }
}
8
  • -Bera tried your code but still it is not working Commented Apr 13, 2018 at 10:29
  • How come, for me it is working. Do you want me to post the entire code?
    – Saroj Bera
    Commented Apr 13, 2018 at 10:48
  • The code is working but the page number is not changing when we click next or previous Commented Apr 13, 2018 at 11:34
  • Please check my updated answer, mark this as an answer if this solves your problem.
    – Saroj Bera
    Commented Apr 13, 2018 at 18:43
  • Nope, on trying this the page numbers are changing but when we enter page number manually in the input text field then nothing happens Commented Apr 13, 2018 at 19:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .