This code explains how to build a simple page that allows you to edit several records at the same time. The code builds this table:
You can edit any email, but only those marked with the checkbox will be saved on the database once the “Save” button is clicked, additionally there is a link on each row that will navigate to the edit page and fully edit the record. This is not a very good user interface, but allows me to demonstrate some techniques.
VisualForce Page:
<apex:page controller="MultiRow" >
<apex:form >
<apex:pageblock >
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!Save}" />
</apex:pageBlockButtons>
<apex:pageBlockSection columns="1">
<apex:dataTable value="{!Contacts}" var="c" border="1" rowClasses="odd,even" styleClass="tableClass" cellpadding="3">
<apex:column >
<apex:facet name="header">Actions</apex:facet>
<apex:inputCheckbox value="{!c.Checked}"/>
<apex:commandLink value="Full Edit" action="{!URLFOR($action.Contact.Edit, c.ID)}" target="_blank" />
</apex:column>
<apex:column >
<apex:facet name="header">Name</apex:facet>
<apex:outputText value="{!c.Name}"/>
</apex:column>
<apex:column >
<apex:facet name="header">Email</apex:facet>
<apex:inputText value="{!c.Email}" />
</apex:column>
</apex:dataTable>
</apex:pageBlockSection>
</apex:pageblock>
</apex:form>
</apex:page>
Controller:
public class MultiRow {
public List<multiRowContact> Contacts { get; set; }
public MultiRow() {
LoadData();
}
public PageReference Save() {
for (multiRowContact MRC : Contacts) {
MRC.Save();
}
LoadData();
return null;
}
private void LoadData() {
Contacts = new List<multiRowContact>();
for (List<Contact> cs : [SELECT c.ID, c.Name, c.Email FROM Contact c WHERE c.AccountID = '0018000000OdcOt']) {
for (Contact c : cs) {
multiRowContact MRC = new multiRowContact();
MRC.ID = c.ID;
MRC.Checked = false;
MRC.Name = c.Name;
MRC.Email = c.Email;
Contacts.add(MRC);
}
}
}
private class multiRowContact {
public String ID { get; set; }
public String Name { get; set; }
public String Email { get; set; }
public Boolean Checked { get; set; }
public void Save() {
if (Checked) {
System.debug('Saving...ID: ' + ID);
Contact c = [SELECT c.Email FROM Contact c WHERE c.ID = :ID LIMIT 1];
c.Email = Email;
update c;
}
}
}
}
No comments:
Post a Comment