[SalesForce] Select Records in pageblocktable using a checkbox

I got a similar page similar to the following link which is a search function uses paginator using Standardset controller, and I want to make the search records selectable using a checkbox in front of each record (as a new column). How can I do this ?

please refer to the following link for better understanding because my class is a bit too long to post here.

http://www.sfdcpoint.com/salesforce/pagination-using-standard-set-controller-salesforce/

following is my search method which uses standardsetcontroller,

    //search variables
    public List<Market_Entity__c> marketEntity {get; private set;}
    public String searchStringName {get; set;}
    public String searchStringPostCode {get; set;}
    public String searchStringSPID {get; set;}
    public boolean showNewSearchPopup {get; private set;}
    public String warningMessage {get; set;}

    //paginator variables
    public ApexPages.StandardSetController setCon {get;set;}
    public Integer noOfRecords {get;set;}
    public Integer size {get;set;}
    public List<SelectOption> paginationSizeOptions {get;set;}

public void stringSearchValue(){

        setCon = null; // Clears the previous search results
        if(setCon == null){

            size=20; // Default size of records to display for every section

            if( searchStringName.length() >=3 || searchStringPostCode.length() >=3 || searchStringSPID.length() >=3 ){

                string searchquery = 'SELECT Name, Id, Postcode__c, SPID__c, OrgId__c FROM Market_Entity__c WHERE (Name LIKE \'%'+String.escapeSingleQuotes(searchStringName)+'%\' AND Postcode__c LIKE \'%'+String.escapeSingleQuotes(searchStringPostCode)+'%\' AND SPID__c LIKE \'%'+String.escapeSingleQuotes(searchStringSPID)+'%\') AND OrgId__c = \'SWBS\'';

                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(searchquery));
                marketEntity = setCon.getRecords();
                setCon.setPageSize(size);
                noOfRecords = setCon.getResultSize();

                warningMessage = Label.No_Result_Found;
                    if (marketEntity.size() == 0) 
                        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, Label.No_Result_Found ));

            }else{
                warningMessage = Label.Search_Text_Characters;
                    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, Label.Search_Text_Characters));
            }

        }
    }

How can I use a wrapper class to the above method ? I can pass the List of marketEntity to a new wrapper method and use wrapper variable in the page right ? how can I do that ?

Best Answer

There is a very good example by Jeff. You can take reference.

Controller Code

public with sharing class PagingController {

    List<categoryWrapper> categories {get;set;}

    // instantiate the StandardSetController from a query locator
    public ApexPages.StandardSetController con {
        get {
            if(con == null) {
                con = new ApexPages.StandardSetController(Database.getQueryLocator([Select Id, Name FROM Cat3__c Order By Name limit 100]));
                // sets the number of records in each page set
                con.setPageSize(5);
            }
            return con;
        }
        set;
    }

    // returns a list of wrapper objects for the sObjects in the current page set
    public List<categoryWrapper> getCategories() {
        categories = new List<categoryWrapper>();
        for (Cat3__c category : (List<cat3__c>)con.getRecords())
            categories.add(new CategoryWrapper(category));

        return categories;
    }

    // displays the selected items
     public PageReference process() {
         for (CategoryWrapper cw : categories) {
             if (cw.checked)
                 ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,cw.cat.name));
         }
         return null;
     }

    // indicates whether there are more records after the current page set.
    public Boolean hasNext {
        get {
            return con.getHasNext();
        }
        set;
    }

    // indicates whether there are more records before the current page set.
    public Boolean hasPrevious {
        get {
            return con.getHasPrevious();
        }
        set;
    }

    // returns the page number of the current page set
    public Integer pageNumber {
        get {
            return con.getPageNumber();
        }
        set;
    }

    // returns the first page of records
     public void first() {
         con.first();
     }

     // returns the last page of records
     public void last() {
         con.last();
     }

     // returns the previous page of records
     public void previous() {
         con.previous();
     }

     // returns the next page of records
     public void next() {
         con.next();
     }

     // returns the PageReference of the original page, if known, or the home page.
     public void cancel() {
         con.cancel();
     }

}

Wrapper Class

public class CategoryWrapper {

    public Boolean checked{ get; set; }
    public Cat3__c cat { get; set;}

    public CategoryWrapper(){
        cat = new Cat3__c();
        checked = false;
    }

    public CategoryWrapper(Cat3__c c){
        cat = c;
        checked = false;
    }

    public static testMethod void testMe() {

        CategoryWrapper cw = new CategoryWrapper();
        System.assertEquals(cw.checked,false);

        CategoryWrapper cw2 = new CategoryWrapper(new Cat3__c(name='Test1'));
        System.assertEquals(cw2.cat.name,'Test1');
        System.assertEquals(cw2.checked,false);

    }

}

If you want to maintain state also then you can take reference from here. http://salesforcehelpinghands.blogspot.in/2012/10/pagination-with-maintaing-state-of.html