-1

When I use my Wrapper class to delete some records, my Select button doesn't work (without wrapper it works fine). So I even can't check, is my delete-code works or not. What is wrong here?

Apex:

public class thirdinterview { 

public Id objId{get;set;}  //for assignTo of apex:param
public List<WrapperObj> wrapperObjList {get;set;}  
public List<Object__c> selectedObjList{get;set;}

public thirdinterview(){
   selectedObjList = new List<Object__c>();
}

  public List<WrapperObj> getObjs(){
        if(wrapperObjList == null){
            wrapperObjList = new List<WrapperObj>();
            for(Object__c o : [select Id, Name from Object__c limit 25]){
                  wrapperObjList.add(new WrapperObj(o));
            }
         }
       return wrapperObjList;
   }  
  public PageReference deleteSelected(){ //For Delete button   
        for(WrapperObj cCon : getObjs()) {
            if(cCon.selected == true){
            temp.add(cCon.con);
            }
        }
        for(Integer i=0; i<temp.size(); i++){
           selectedObjList.remove(i);  //I try to remove selected records
         } 
         return null;
  }
  public class WrapperObj {
        public Object__c con {get; set;}
        public Boolean selected {get; set;}
        public WrapperObj(Object__c c) {
            con = c;
            selected = false;
        }
   }
public ApexPages.StandardSetController setController {
get {
    if(setController == null) {                
        setController = new ApexPages.StandardSetController(getObjsWithSOQLFilters());
            setController.setPageSize(size);  
            noOfRecords = setController.getResultSize();
        }
        return setController;
    }
    set;
}

public List<Object__c> getObjsList() {
    return (List<Object__c>) setController.getRecords();
}

public void getSelectedObject() {  //For Select button
    List<Object__c> temporaryList = [SELECT Id, Name
                                     FROM Object__c
                                     WHERE Id =: objId];
    selectedJobAdvertisementsList.addAll(temporaryList);
}
}

My Visualforce page for Select and Delete Buttons:

<apex:repeat value="{!ObjsList}" var="ob">
 <div class="div-detailed-info">
      <h2>INFO about {!ob.Name}</h2>
 </div>
 <apex:commandButton value="Select" action="{!getSelectedObject}" reRender="smallPanelId"> 
      <apex:param name="accountId" value="{!ob.Id}" assignTo="{!objId}"/> 
 </apex:commandButton>
</apex:repeat>

<apex:outputPanel id="smallPanelId">
   <apex:repeat value="{!wrapperObjList }" var="ob">
      <div class="div-smallpanel-label">
        <apex:outputText style="font-weight:800" value="{!ob.Name}"/>
         <apex:inputCheckbox value="{!ob.selected}" />
         <apex:commandButton value="✕" action="{!deleteSelected}" reRender="smallPanelId" />
    </div>
   </apex:repeat>
  </apex:outputPanel>
  • 2
    Deleting your post and then adding it back to the exchange verbatim is not going to do you any favors, and is actually likely to get you question banned, preventing you from asking anything in the future. Please instead try to edit and improve your posts. – Adrian Larson Nov 7 '18 at 5:53
2

Keeping two lists synchronized is an awkward approach to this problem. I suggest you eliminate selectedObjList and instead iterate over wrapperObjList and check its selected flag whenever you need to find the selected records.

(The way you are using indexes in deleteSelected looks broken to me.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.