0

I Have two visualforce page calling same controller.

Visualforce 1 -

 <apex:page controller="Contact_Checkbox_Class2" >

   <apex:form >
    <apex:pageBlock title="Search" >
        <apex:pageBlockSection columns="1">
            <apex:pageBlockSectionItem >
            <apex:outputLabel value="Enter Contact Name"/>
            <apex:inputText value="{!cname2}"/>
            </apex:pageBlockSectionItem>

          <apex:pageBlockSectionItem > 
            <apex:commandButton value="Search" action="{!getdetails2}" />
          </apex:pageBlockSectionItem>
        </apex:pageBlockSection>

      </apex:pageBlock>
    </apex:form>

    <apex:include pageName="MultiselectContact2" rendered="{!Rendered2}" />
</apex:page>

Visualforce 2-

<apex:page controller="Contact_Checkbox_Class2" tabStyle="Contact" >
<apex:form >
    <apex:pageBlock >  


        <apex:pageBlockSection Title="List of Available Contact" >
            <apex:dataTable value="{!Contact2}" var="a" columnswidth="50px,50px" cellpadding="6" border="1">
                <apex:column >
                    <apex:facet name="header">
                        <apex:outputLabel >Name </apex:outputLabel>
                        <apex:inputCheckbox >
                        <apex:actionSupport event="onclick" action="{!GetSelected2}" onsubmit="checkAll(this)" rerender="Selected_PBS"/>
                        </apex:inputCheckbox>
                    </apex:facet>
                    <apex:inputCheckbox value="{!a.selected2}" id="checkedone">
                    <apex:actionSupport event="onclick" action="{!GetSelected2}" rerender="Selected_PBS"/></apex:inputCheckbox>
                </apex:column>
                <apex:column headervalue="Contact Name" value="{!a.acc2.Name}" />
                <apex:column headervalue="Email" value="{!a.acc2.Email}" />
                <apex:column headervalue="Phone" value="{!a.acc2.Phone}" />
            </apex:dataTable>
        </apex:pageBlockSection>

        <apex:pageBlockSection Title="Selected Contact" id="Selected_PBS">
            <apex:dataTable value="{!SelectedContact2}" var="s" columnswidth="50px,50px" cellpadding="4" border="1">
                <apex:column headervalue="Contact Name" value="{!s.Name}" />
                <apex:column headervalue="Email" value="{!s.Email}" />
                <apex:column headervalue="Phone" value="{!s.Phone}" />
            </apex:dataTable>
        </apex:pageBlockSection>
        <apex:pageBlockSection >
    <apex:pageBlockSectionItem > 
        <apex:commandButton value="Email Wizard" action="{!SendTestEmailRedirect2}" />
      </apex:pageBlockSectionItem>    
      </apex:pageBlockSection>
    </apex:pageBlock>

</apex:form>
<script>
    function checkAll(cb)
    {
        var inputElem = document.getElementsByTagName("input");
        for(var i=0; i<inputElem.length; i++)
        {
            if(inputElem[i].id.indexOf("checkedone")!=-1)
            inputElem[i].checked = cb.checked;
        }
    }
</script>

Apex controller -

public class Contact_Checkbox_Class2 {

public PageReference SendTestEmailRedirect2() {
    return null;
}


public String cname2{set;get;}
public list<Contact> result2{set;get;}  


public  Integer t2=0; 


List<contactwrapper2> contactList2 = new List<contactwrapper2>();
List<contact> selectedcontact2 = new List<Contact>();
Boolean render2=false;


public PageReference getdetails2(){
   // selectedcontact.clear();
   contactList2.clear();
   String s3 ='SELECT Id,Name,Email,Phone FROM contact WHERE Name LIKE \'%' + cname2 + '%\'';
   result2 = database.query(s3);
   if(render2==false){
           render2=true;
       }          
   return null;          
}

public boolean getRendered2(){return render2;}

public List<contactwrapper2> getcontact2(){
    for(contact a : result2)
    contactList2.add(new contactwrapper2(a));
    return contactList2;
}


public PageReference getSelected2()
{

    for(contactwrapper2 accwrapper : contactList2)
    {
        for(integer i=0;i<selectedcontact2.size();i++)
        {
        if (selectedcontact2[i]==accwrapper.acc2){
        t2=1;   

        }
        }     

        if(accwrapper.selected2 == true && t2!=1){
         selectedcontact2.add(accwrapper.acc2);
        }


        t2=0;
    }  
   t2=0;


   return null;
}

public List<contact> GetSelectedcontact2()
{
    if(selectedcontact2.size()>0)
    return selectedcontact2;
    else{

    return null;
    } 
}   

public class contactwrapper2
{
    public contact acc2{get; set;}
    public Boolean selected2 {get; set;}
    public contactwrapper2(contact a)
    {
        acc2 = a;
        selected2 = false;
    }
}
}

I want to implemented when the selected contact in the contactlist get unchecked it should get removed from selectedlist.

PLease help.. all the code i tried not working. Thanks in advance

1 Answer 1

1

I suggest this change where the ID values to remove are found and then those are used to remove the items from the other list:

public PageReference getSelected2()
{
    Set<Id> idsToRemove = new Set<Id>();
    for (contactwrapper2 sc : contactList2)
    {
        if (!sc.selected2) ids.add(idsToRemove.acc2.Id);
    }
    for (Integer i = selectedcontact2.size() - 1; i >= 0; i--)
    {
        if (idsToRemove.contains(selectedcontact2[i].Id)) selectedcontact2.remove(i);
    }
    return null;
}

Before going much further you should also clean up the names used in the code as they add confusion in the code.

PS

Code above made some bad assumptions about the rest of the controller. If I was writing the controller it would look like this:

public class MyController {

    public class ContactWrapper {
        public Contact sob {get; set;}
        public Boolean selected {get; set;}
        public ContactWrapper(Contact sob) {
            this.sob = sob;
            this.selected = false;
        }
    }

    public String contactName {set; get;}
    public List<ContactWrapper> selectableContacts {get; private set;}
    public List<Contact> selectedContacts {get; private set;}

    public MyController() {
        selectableContacts = new List<ContactWrapper>();
        selectedContacts = new List<Contact>();
    }

    // Call from apex:commandButton
    public PageReference search() {
        selectableContacts.clear();
        String likeTerm = '%' + contactName + '%';
        for (Contact c : [
                select Id, Name, Email, Phone
                from Contact
                where Name like :likeTerm
                order by Name
                ]) {
            selectableContacts.add(new ContactWrapper(c));
        }
        if (selectableContacts.size() > 0) {
            return Page.SecondPageName;
        } else {
            ApexPages.addMessage(new ApexPages.Message(
                    ApexPages.Severity.INFO,
                    'No matches, try again'
                    ));
            return null;
        }
    }

    // Call from apex:actionSupport
    public PageReference selectionChanged() {
        selectedContacts.clear();
        for (ContactWrapper cw : selectableContacts) {
            if (cw.selected) selectedContacts.add(cw.sob);
        }
        return null;
    }
}
3
  • Not working. . it is removing selected contacts when they are selected too.
    – Rachit
    Commented Mar 17, 2016 at 14:42
  • @Rachit I hadn't followed your code through enough; see the solution posted in the PS.
    – Keith C
    Commented Mar 17, 2016 at 15:49
  • Thanks keith.. for your reply. But i already solved the issue. I will take a note of your solution and test it along with my reset of code. Anyway thanks.
    – Rachit
    Commented Mar 18, 2016 at 7:11

You must log in to answer this question.

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