+ Start a Discussion
Soundar Raj PonpandiSoundar Raj Ponpandi 

Remove Records From wrapper List

Hi Expert,

Now i am displaying selected records to Pageblock2 . Though selected records are don't need to show in PageBlock1. kindly Check the below Image For information.

**************** Apex *******************
 
public class OppDisplaySelectedContrl {

    public List<oppWrapper> listOppWrap{get;set;}
    Public List<Opportunity> listOfSelectedOpp{get;set;}
    Public List<Opportunity> listOfSelectedOpp2{get;set;}
    Set<Integer> st = New Set<Integer>();
    
    
    public OppDisplaySelectedContrl(){
        listoppWrap = new List<oppWrapper>();
        searchrecord();
    }
    
    public void searchrecord(){
        listOppWrap.clear();
        for(Opportunity opp : [Select id,name,stageName,amount, CloseDate From Opportunity Limit 5]){
            listOppWrap.add(New OppWrapper(opp));
             System.debug('Opportunity list' + opp );
        }
       
    }
    
    public void addToGrid(){
        listOfSelectedOpp = New List<Opportunity>();
        listOfSelectedOpp.clear();
        for(OppWrapper  OppWrapObj : listOppWrap){
            if(OppWrapObj.selected == True){
                listOfSelectedOpp.add(OppWrapObj.wrapOpp);
       
            }
        }
        
       
        
    }
    
    public void closedWon(){
        
        for(Opportunity opp : listOfSelectedOpp){
            opp.StageName = 'Closed Won';
        }
        update listOfSelectedOpp;
        searchrecord();
    }
    
    public void closedLost(){
        for(Opportunity opp : listOfSelectedOpp){
            opp.StageName = 'Closed Lost';
        }
        
        update listOfSelectedOpp;
        searchrecord();
    }
    
    public class OppWrapper{
        public boolean selected{get;set;}
        public Opportunity wrapOpp{get;set;}
        public OppWrapper(Opportunity opp){
            selected = false;
            wrapOpp = opp;
        }
    }
}

*********************VF Page ***********************
 
<apex:page controller="OppDisplaySelectedContrl">
    <apex:form >
       <apex:pageBlock title="Opportunity" id="pb1">
          <apex:pageBlockSection title="All Opportunity" columns="1">
             <apex:pageBlockTable value="{!listOppWrap}" var="oppWrap" >
                 <apex:column >
                 <!--<apex:inputCheckbox value="Select"/> -->
                 </apex:column>
                 <apex:column headerValue="Select">
                 	<apex:inputCheckbox value="{!oppWrap.selected}"  />
                 </apex:column>
                 <apex:column value="{!oppWrap.wrapOpp.name}"/>
                 <apex:column value="{!oppWrap.wrapOpp.StageName}"/>
                 <apex:column value="{!oppWrap.wrapOpp.closeDate}"/>
                 <apex:column value="{!oppWrap.wrapOpp.amount}"/> 
              </apex:pageBlockTable>
           </apex:pageBlockSection> 
           
              <apex:pageBlockButtons >
                 <apex:commandButton value="Add To Grid" action="{!addToGrid}"/>
              </apex:pageBlockButtons>
        </apex:pageBlock>
        
        <apex:pageBlock title="Selected opportunity" id="pb2">
            <apex:pageBlockSection columns="1">
              <apex:pageBlockTable value="{!listOfSelectedOpp}" var="selectedOpp" >
                  <apex:column value="{!selectedOpp.name}" headerValue="Opportunity Name"/>
                  <apex:column value="{!selectedOpp.StageName}" headerValue="Stage"/>
                  <apex:column value="{!selectedOpp.closeDate}" headerValue="Closed Date"/>
                  <apex:column value="{!selectedOpp.Amount}" headerValue="Amount"/>

                  
                </apex:pageBlockTable>
            </apex:pageBlockSection>
            
            <apex:pageBlockButtons >
              <apex:commandButton value="Closed Won" action="{!closedWon}"/>
                <apex:commandButton value="Closed Lost" action="{!closedLost}" />
           </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

**********Sample output ***********

User-added image


Special Thanks In Advance !!!!


Regards, 

Soundar 
Best Answer chosen by Soundar Raj Ponpandi
vijayabhaskarareddyvijayabhaskarareddy
HI@ Raj
======
replace this function with below code
public void addToGrid(){
        listOfSelectedOpp = New List<Opportunity>();
        listOfSelectedOpp.clear();
        for ( Integer i = listOppWrap.size() - 1; i >= 0; --i )
          {
            OppWrapper oppWrapObj=listOppWrap[i];
            if (OppWrapObj.selected== True){
                 listOfSelectedOpp.add(OppWrapObj.wrapOpp);
                 listOppWrap.remove(i);
               }
         }
     }

 

All Answers

bob_buzzardbob_buzzard
You need to remove the records that are selected from listOppWrap. If you go through the list in reverse order you can remove them as you process them. Something like:
 
for ( Integer i = listOppWrap.size() - 1; i > 0; --i )
{
    OppWrapper oppWrapObj=listOppWrap[i];
    if (OppWrapObj.selected == True){
        listOfSelectedOpp.add(OppWrapObj.wrapOpp);
    }

    // remove from list
    listOppWrap.remove(i);
}



 
vijayabhaskarareddyvijayabhaskarareddy
HI@ Raj
======
replace this function with below code
public void addToGrid(){
        listOfSelectedOpp = New List<Opportunity>();
        listOfSelectedOpp.clear();
        for ( Integer i = listOppWrap.size() - 1; i >= 0; --i )
          {
            OppWrapper oppWrapObj=listOppWrap[i];
            if (OppWrapObj.selected== True){
                 listOfSelectedOpp.add(OppWrapObj.wrapOpp);
                 listOppWrap.remove(i);
               }
         }
     }

 
This was selected as the best answer
Soundar Raj PonpandiSoundar Raj Ponpandi
Hi vijayabhaskarareddy

Your Code is Working Perfectly what i am expecting , and right now i need some changes in that code ! 

1. Selected record's removed from pageblockTable1.
2. Now it's added in pageblockTable2
3.And again  i Like to select and display from pageblocktable1 to pageblocktable2 . for example 

initially i was selected 1 record and click the addToGrid Button and again i like to select 2 records . now 3 records need to be show in pageblockTable.


Could you please advise me how it's possible ...

Thanks in advance..


Regards,


Soundar.
Soundar Raj PonpandiSoundar Raj Ponpandi
Hi bob_buzzard,

Thanks for your quick response. Can you please advise me for above comment please 



Note

1. Selected record's removed from pageblockTable1.
2. Now it's added in pageblockTable2
3.And again  i Like to select and display from pageblocktable1 to pageblocktable2 . for example 

initially i was selected 1 record and click the addToGrid Button and again i like to select 2 records . now 3 records need to be show in pageblockTable.

Regards.
SOUNDAR
Soundar Raj PonpandiSoundar Raj Ponpandi
Dear Friends,

I solved above comments by myself . Even it's Possible by both of your help (Mr. vijayabhaskarareddy & Mr. bob_buzzard).

I make some changes in the code .
 
public List<Opportunity>  listOfSelectedOpp = new List<Opportunity>();
public List<Opportunity>  getListOfSelectedOpp(){
return listOfSelectedOpp ;

*** instead of  ***

public List<Opportunity>  listOfSelectedOpp{get;set;}

}


public void addToGrid(){
        //listOfSelectedOpp = New List<Opportunity>();      ** removed **
        //listOfSelectedOpp.clear();      ** removed **
        for ( Integer i = listOppWrap.size() - 1; i >= 0; --i )
          {
            OppWrapper oppWrapObj=listOppWrap[i];
            if (OppWrapObj.selected== True){
                 listOfSelectedOpp.add(OppWrapObj.wrapOpp);
                 listOppWrap.remove(i);
               }
         }
     }



Regards,
Soundar.
pradnya mulepradnya mule
I want to keep my first selected list also in selected list.
How i can do this?
 
pradnya mulepradnya mule
User-added image