Thursday, May 24, 2012

Dynamic View On Visualforce:

Dynamic View On Visualforce:
How to limit page view by sending select option values in controller .In this example you can find a drop down list contains one of the standard field on account object (Type).If you would like to control your page display as of selected drop down list code looks like this




public class myPageController {
private Account acc = new Account();
private List<Account> accs;
private string selectedAccount;
public Account getAccount()
{
return acc;
}
public void setAccount(Account a)
{
acc = a;
}
public List<Account> getAccounts()
{
return accs;
}
public string getSelectedAccount()
{
return selectedAccount;
}
public void setSelectedAccount(string value)
{
selectedAccount = value;
}
public PageReference ViewAccounts()
{
if (acc.Type == null)
{
//view a message because validation has failed.
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Validation Failed: Select an item fron the dropdown list.'));
return null;
}
string AccountType = acc.Type;
accs = [Select id, name, type from Account where type= :AccountType limit 20];
if (accs == null || accs.size() == 0)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'No data found to be shown.'));
}
return null;
}
public PageReference ShowSelected()
{
if (selectedAccount != null && selectedAccount != '')
{
for(Account a: accs)
{
if (a.Id == selectedAccount)
{
string Msg = a.Name + ' Account is selected.';
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,Msg ));
}
}
}
return null;
}
}








<apex:page controller="myPageController" tabStyle="Account">
<apex:form >
<apex:sectionHeader title="My Page" />
<apex:pageBlock title="" id="pageBlock">
<apex:pageBlockButtons location="top">
<apex:inputField id="accountType" value="{!Account.Type}" />
<apex:commandButton value="View" action="{!ViewAccounts}" id="theButton" rerender="pageBlock"></apex:commandButton>
</apex:pageBlockButtons>
<apex:pageMessages ></apex:pageMessages>
<!-- defines a javascript function which calls a Controller's method -->
<apex:actionFunction action="{!ShowSelected}" name="RowMethodCall" rerender="pageBlock" status="status">
<apex:param name="selectedAccount" assignTo="{!selectedAccount}" value=""/>
</apex:actionFunction>
<!-- define a start facet within the actionStatus that has a div with defined id visible to the whole HTML document -->
<apex:actionStatus id="status">
<apex:facet name="start">
<div id="statusTemplate">Working...</div>
</apex:facet>
</apex:actionStatus>


<apex:pageBlockTable value="{!accounts}" var="a" rendered="{!NOT(ISNULL(accounts))}">
<apex:column >
<apex:facet name="header">Account Name</apex:facet>
<apex:outputLink value="/{!a.Id}" target="_blank">{!a.Name}</apex:outputLink>
</apex:column>
<apex:column value="{!a.type}"></apex:column>
<apex:column >
<apex:facet name="header">Action</apex:facet>
<a href="javascript:DoTask('{!a.Id}');">Execute</a>
<div style="display:none" id="{!a.Id}">
Processing {!a.Name} ...
</div>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
<script type="text/javascript">
function DoTask(accountId)
{


var accountStatus = document.getElementById(accountId);
if (accountStatus && accountStatus.innerHTML)
{
document.getElementById('statusTemplate').innerHTML = accountStatus.innerHTML;
}


RowMethodCall(accountId);
}
</script>
</apex:form>
</apex:page>



No comments:

Post a Comment