+ Start a Discussion
rohitash yadav 8rohitash yadav 8 

Add only currently checked record to another table in visualforce page

Hi,

I have some records with checkboxes in visualforces page. I am using wrapper class. I need to add the selected record(checkbox) to another table. When I click on checkbox then all records are getting added to the table again and again rather than only the one whick I check.

Visualforce Page:
 
<apex:page controller="OpportunityLineItems" >
    <apex:form >
        <apex:pageBlock title="Selected Packages">
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
                <apex:commandButton value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>
                <apex:pageblockTable value="{!checkedPackages}" var="pck" id="Selected_PBS"  >
                    <apex:column headerValue="">
                        Remove
                    </apex:column>
                    <apex:column headerValue="Product Name" value="{!pck.Name}" width="50" />
                    <apex:column headerValue="Product Category" value="{!pck.Product_Category__c}" width="50" />
                    <apex:column headerValue="Activity" width="50">
                        <apex:inputField value="{!pck.Activity__c}"/>
                    </apex:column>
                    <apex:column headerValue="Price" value="{!pck.Cost__c}" width="50"/>
                    <apex:column headerValue="Final Price" width="50">
                        <apex:inputField value="{!pck.Final_Price__c}"/>
                    </apex:column>
                    <apex:column headerValue="No of Days" width="50">
                        <apex:inputField value="{!pck.No_of_Days__c}"/>
                    </apex:column>
                    <apex:column headerValue="No Of People" width="50">
                        <apex:inputField value="{!pck.No_Of_People__c}"/>
                    </apex:column>
                    <apex:column headerValue="No Of Male" width="50">
                        <apex:inputField value="{!pck.No_of_Male__c}"/>
                    </apex:column>
                    <apex:column headerValue="No Of Female" width="50">
                        <apex:inputField value="{!pck.No_of_Female__c}"/>
                    </apex:column>
                    <apex:column headerValue="Total Price" width="50">
                        <apex:inputField value="{!pck.Total_Price__c}"/>
                    </apex:column>
                    <apex:column headerValue="Comments" width="50">
                        <apex:inputField value="{!pck.Comments__c}"/>
                    </apex:column>
                </apex:pageblockTable>
        </apex:pageBlock>
        <apex:pageBlock title="Search for Packages">
            <apex:outputLabel >Packages Category</apex:outputLabel>
            <apex:inputField value="{!prdcat.Product_Category__c}" >
                <apex:actionSupport event="onchange" rerender="packagesList" action="{!filterPackagess}"/>
            </apex:inputField>
                <apex:pageblockTable value="{!packages}" var="a" id="packagesList" >
                    <apex:column headerValue="Select" width="60">
                        <apex:inputCheckbox value="{!a.selected}" id="checkedone" >
                            <apex:actionSupport event="onclick" action="{!getSelectedPackages}" rerender="Selected_PBS"/>
                        </apex:inputCheckbox>
                    </apex:column>
                    <apex:column value="{!a.pkgs.Name}"/>
                    <apex:column value="{!a.pkgs.Product_Category__c}"/>
                    <apex:column value="{!a.pkgs.Cost__c}"/>
                    <apex:column value="{!a.pkgs.Stay__c}"/>
                    <apex:column value="{!a.pkgs.Activity__c}"/>
                    <apex:column value="{!a.pkgs.Description__c}"/>
                </apex:pageblocktable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Wrapper class:
public class OpportunityLineItems {

    List<Package__c> selectedPackages = new List<Package__c>();
    public Package__c prdcat{get;set;}
	List<packagewrapper1> packagesListNew = new List<packagewrapper1>();
    public OpportunityLineItems(){
        prdcat = new Package__c();
    }

    public  List<packagewrapper1> getPackages(){
        packagesListNew.clear();
        filteredList = new List<Package__c>();
        if( prdcat.Product_Category__c != null ){
            filteredList = [SELECT Id,Name,Product_Category__c,Cost__c,Stay__c,Activity__c,Description__c,Final_Price__c,No_of_Days__c,No_Of_People__c,No_of_Male__c,No_of_Female__c,Total_Price__c,Comments__c FROM Package__c 
               where Product_Category__c=:prdcat.Product_Category__c ];
        } else {
            filteredList = [SELECT Id,Name,Product_Category__c,Cost__c,Stay__c,Activity__c,Description__c,Final_Price__c,No_of_Days__c,No_Of_People__c,No_of_Male__c,No_of_Female__c,Total_Price__c,Comments__c FROM Package__c ];
        }
        for(Package__c a : filteredList ){
            packagesListNew.add(new packagewrapper1(a));
        }
        return packagesListNew;
    }
    
    List<Package__c> filteredList = new List<Package__c>();
    List<packagewrapper1> packages = new List<packagewrapper1>();
    public  PageReference  filterPackagess(){
        packages.clear();
        return null;
    }

    public Package__c getPrdcat() {
        prdcat = new Package__c();
        return null;
    }

    public List<Package__c> GetCheckedPackages() {
        if(selectedPackages.size()>0)
        return selectedPackages;
        else
        return null;
    }

    public PageReference getSelectedPackages() {
        selectedPackages.clear();
        for(packagewrapper1 pkgwrapper : packagesListNew)
        if(pkgwrapper.selected == true)
        selectedPackages.add(pkgwrapper.pkgs);
		return null;
    }

    public PageReference cancel() {
        return null;
    }

    public PageReference save() {
        return null;
    }

    public class packagewrapper1 {
        public Package__c pkgs{get; set;}
        public Boolean selected {get; set;}
        public packagewrapper1(Package__c p)
        {
            pkgs = p;
            selected = false;
        }
    }
}


Can anyone help me.

Thanks,
Rohitash
 
RatanRatan
you code correct to me. only issue here looks to me is your method name starts with get

getMethod always call when you call a method in controller side

SO I suggest instead of using getSelectedPackages() method name instead use like fetchSelectedPackages()

and in your action support call this method
 
<apex:actionSupport event="onclick" action="{!fetchSelectedPackages}" rerender="Selected_PBS"/>

 
rohitash yadav 8rohitash yadav 8
Hi Ratan,

Thanks for your reply but my issue is not solved yet.

Thanks,
Rohitash