Friday, June 13, 2014
Modals are part of the Visualstrap from very beginning but I never talked much about them. Modals can be really help to enhance the user experience. They can let you do many things without going to and fro between pages.
Controller : This method sets the isSuccess flag to true only when the records is saved successfully.
Page : The below section only renders when isSuccess is true ( and lets the page to close only when the save was success )
Below is the live demo of the page, you are welcome to try creating some cases for me :)
In Salesforce, we are used to this type of arrangement. For example have a look at the "New" / "Edit" buttons, they will take you to a another page to accomplish the task. It feels a bit clunky.
To make the experience fluid and fast these pages can be replaced with modals.
To make the experience fluid and fast these pages can be replaced with modals.
Scenario
Lets say we want a page where
- See all the cases
- Create new cases
The page can be extended to close and update the cases as well
Prerequisite
The Page & Controller
The page and controller are pretty simple.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<apex:page controller="VSModals_Con"> | |
<vs:importvisualstrap /> | |
<apex:form > | |
<vs:visualstrapblock > | |
<vs:pageheader title="Cases" subtitle="Cases List" icon="file"/> | |
<vs:panel title="All Cases" type="primary" id="allCases"> | |
<vs:well styleclass="well-sm" style="text-align:center"> | |
<a class="btn btn-success btn-md" onclick="$('#newCase').modal('show');return false;">New Case</a> | |
</vs:well> | |
<apex:dataTable value="{!cases}" var="case" styleClass="table table-bordered table-hover"> | |
<apex:column value="{!case.CaseNumber}" headerValue="CaseNumber" styleClass="{!IF(case.isClosed,'','warning')}"/> | |
<apex:column value="{!case.Subject}" headerValue="Subject" styleClass="{!IF(case.isClosed,'','warning')}"/> | |
<apex:column value="{!case.Status}" headerValue="Status" styleClass="{!IF(case.isClosed,'','warning')}"/> | |
<apex:column value="{!case.Priority}" headerValue="Priority" styleClass="{!IF(case.isClosed,'','warning')}"/> | |
</apex:dataTable> | |
</vs:panel> | |
<vs:modal id="newCase" title="New Case"> | |
<apex:outputPanel layout="block" id="newCasePanel"> | |
<apex:pageBlock mode="maindetail"> | |
<apex:pageBlockSection > | |
<apex:inputField value="{!caseObj.Subject}"/> | |
<apex:inputField value="{!caseObj.Status}"/> | |
<apex:inputField value="{!caseObj.Description}"/> | |
<apex:inputField value="{!caseObj.Priority}"/> | |
</apex:pageBlockSection> | |
</apex:pageBlock> | |
<apex:outputPanel layout="block" styleClass="modal-footer"> | |
<apex:commandButton value="Cancel" styleClass="btn-warning" html-data-dismiss="modal"/> | |
<apex:commandButton value="Save" styleClass="btn-success" onclick="$(this).button('loading')" html-data-loading-text="Saving..." action="{!saveCase}" reRender="newCasePanel,allCases"/> | |
</apex:outputPanel> | |
<apex:outputPanel rendered="{!isSuccess}"> | |
<script> | |
$('#newCase').modal('hide'); | |
</script> | |
</apex:outputPanel> | |
</apex:outputPanel> | |
</vs:modal> | |
</vs:visualstrapblock> | |
</apex:form> | |
</apex:page> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class VSModals_Con { | |
public Boolean isSuccess{get;set;} | |
public Case caseObj{get;set;} | |
public VSModals_Con() { | |
init(); | |
} | |
//method to save the records | |
public void saveCase(){ | |
try{ | |
insert caseObj; | |
init(); | |
isSuccess = true; | |
} | |
catch(Exception ex){ | |
Apexpages.addMessages(ex); | |
isSuccess = false; | |
} | |
} | |
//fetch the cases | |
public List<Case> getCases(){ | |
return [SELECT Id,CaseNumber,Status,Priority,Type,Subject,isClosed FROM Case ORDER BY CreatedDate DESC]; | |
} | |
private void init(){ | |
caseObj = new Case(); | |
isSuccess = false; | |
} | |
} |
The Catch
- Opening Modal : Modal can be opened using JS or html attribute. In this demo we are using JS
<a class="btn btn-success btn-md" onclick="$('#newCase').modal('show');return false;">New Case</a>
Controller : This method sets the isSuccess flag to true only when the records is saved successfully.
public void saveCase(){ try{ insert caseObj; init(); isSuccess = true; } catch(Exception ex){ Apexpages.addMessages(ex); isSuccess = false; } }
Page : The below section only renders when isSuccess is true ( and lets the page to close only when the save was success )
<apex:outputPanel rendered="{!isSuccess}"> <script> $('#newCase').modal('hide'); </script> </apex:outputPanel>
![]() |
VSModal in action |
Below is the live demo of the page, you are welcome to try creating some cases for me :)