[SalesForce] Wrapper class, controller and visualforce

I have a visualforce page where I am showing all the attachments related to a case with a checkbox to know if the user has marked it. This is the related part of the VF page:

<apex:pageBlock title="Existing Case Attachments">
    <apex:pageBlockTable value="{!listAttachWrapper}" var="item">
        <apex:column title="Select" headerValue="Add Attachment" width="50px,50px">
            <apex:inputCheckbox value="{!item.IsChecked}">
                </apex:inputCheckbox>
        </apex:column>
        <apex:column headerValue="File">
            <apex:outputLink value="{!URLFOR($Action.Attachment.Download,item.att.Id)}"
                target="_blank">{!item.att.Name}</apex:outputLink>
        </apex:column>
        <apex:column value="{!item.att.Description}" />
    </apex:pageBlockTable>
</apex:pageBlock>

I also have my controller with the definition of the wrapper class:

public List<attWrapper> listAttachWrapper {get;set;}
public List <Attachment> selectedmems{get;set;}
public class attWrapper{
    public Attachment att {get; set;}
    public Boolean isChecked{get; set;}
    public attWrapper(Attachment att){
        this.att=att;
        if (isChecked==null){
            isChecked=false;
        }
    }
}

And in the constructor of the method, I get the elements and mark the ischecked field to false:

listAttachWrapper=new List<attWrapper>();
for (Attachment a:[SELECT Name,Description FROM Attachment WHERE ParentId=:case1.Id])
        listAttachWrapper.add(new attWrapper(a));

Finally I have the method that is called from the VF page, where I try to get which attachments have been market to true:

selectedmems=new List<Attachment>();
system.debug('List of members: '+listAttachWrapper);
for (attWrapper att: listAttachWrapper){
    if (att.IsChecked==true){
        selectedmems.add(att.att);
    }
}
system.debug('The list is:' + selectedmems);

I call the above code from the button:

<apex:pageBlockButtons > <apex:commandButton value="Send communication" action="{!communicateClient}" /> <apex:commandButton value="Cancel" action="{!cancel}" />         </apex:PageBlockButtons> 

The problem is that I get all the elements with the IsChecked field to false, even though I am selecting them in the VF page.

I have been for two days already with this, can anyone help please?

Thanks a lot!

Antonio


Sorry I forgot to paste the code of the button:

<apex:pageBlockButtons > <apex:commandButton value="Send communication" action="          {!communicateClient}" /> <apex:commandButton value="Cancel" action="{!cancel}" />     </apex:PageBlockButtons> 

That calls the method that tries to get the values of the list:

selectedmems=new List<Attachment>();
system.debug('List of members: '+listAttachWrapper);
for (attWrapper att: listAttachWrapper){
    if (att.IsChecked==true){
        selectedmems.add(att.att);
    }
}
system.debug('The list is:' + selectedmems);

Any help is appreciated.Thanks,

Antonio

Best Answer

You need to add an <apex:commandButton> or an <apex:commandLink> to your vf-page to submit the page. Also you have to bind the button or link to an action function in the controller.

An other way would be to use ajax, but I recommend to start with the method above.

Related Topic