Newer Version Available
Step 2: Create A Component for User Input
- Click .
- Enter form for the Name field in the New Lightning Bundle popup window. This creates a new component, form.cmp.
-
In the source code editor, enter this code.
1<aura:component implements="force:appHostable"> 2 <ltng:require styles="{!$Resource.SLDSv2 + 3 '/assets/styles/salesforce-lightning-design-system-ltng.css'}"/> 4 <aura:attribute name="expenses" type="Expense__c[]"/> 5 <aura:attribute name="newExpense" type="Expense__c" 6 default="{ 'sobjectType': 'Expense__c', 7 'Name': '', 8 'Amount__c': 0, 9 'Client__c': '', 10 'Date__c': '', 11 'Reimbursed__c': false 12 }"/> 13 <!-- If you registered a namespace, replace the previous aura:attribute tags with the following --> 14 <!-- <aura:attribute name="expenses" type="myNamespace.Expense__c[]"/> 15 <aura:attribute name="newExpense" type="myNamespace__Expense__c" 16 default="{ 'sobjectType': 'myNamespace__Expense__c', 17 'Name': '', 18 'myNamespace__Amount__c': 0, 19 'myNamespace__Client__c': '', 20 'myNamespace__Date__c': '', 21 'myNamespace__Reimbursed__c': false 22 }"/> --> 23 <!-- Attributes for Expense Counters --> 24 <aura:attribute name="total" type="Double" default="0.00" /> 25 <aura:attribute name="exp" type="Double" default="0" /> 26 27 <!-- Input Form using components --> 28 <div class="container"> 29 <form class="slds-form--stacked"> 30 <div class="slds-form-element slds-is-required"> 31 <div class="slds-form-element__control"> 32 33 <!-- If you registered a namespace, 34 the attributes include your namespace. 35 For example, value="{!v.newExpense.myNamespace__Amount__c}" --> 36 37 <ui:inputText aura:id="expname" label="Expense Name" 38 class="slds-input" 39 labelClass="slds-form-element__label" 40 value="{!v.newExpense.Name}" 41 required="true"/> 42 </div> 43 </div> 44 <div class="slds-form-element slds-is-required"> 45 <div class="slds-form-element__control"> 46 <ui:inputNumber aura:id="amount" label="Amount" 47 class="slds-input" 48 labelClass="slds-form-element__label" 49 value="{!v.newExpense.Amount__c}" 50 placeholder="20.80" required="true"/> 51 </div> 52 </div> 53 <div class="slds-form-element"> 54 <div class="slds-form-element__control"> 55 <ui:inputText aura:id="client" label="Client" 56 class="slds-input" 57 labelClass="slds-form-element__label" 58 value="{!v.newExpense.Client__c}" 59 placeholder="ABC Co."/> 60 </div> 61 </div> 62 <div class="slds-form-element"> 63 <div class="slds-form-element__control"> 64 <ui:inputDateTime aura:id="expdate" label="Expense Date" 65 class="slds-input" 66 labelClass="slds-form-element__label" 67 value="{!v.newExpense.Date__c}" 68 displayDatePicker="true"/> 69 </div> 70 </div> 71 <div class="slds-form-element"> 72 <ui:inputCheckbox aura:id="reimbursed" label="Reimbursed?" 73 class="slds-checkbox" 74 labelClass="slds-form-element__label" 75 value="{!v.newExpense.Reimbursed__c}"/> 76 <ui:button label="Submit" 77 class="slds-button slds-button--neutral" 78 labelClass="label" 79 press="{!c.createExpense}"/> 80 </div> 81 </form> 82 </div><!-- ./container--> 83 84 <!-- Expense Counters --> 85 <div class="container slds-p-top--medium"> 86 <div class="row"> 87 <div class="slds-tile "> 88 <!-- Make the counter red if total amount is more than 100 --> 89 <div class="{!v.total >= 100 90 ? 'slds-notify slds-notify--toast slds-theme--error slds-theme--alert-texture' 91 : 'slds-notify slds-notify--toast slds-theme--alert-texture'}"> 92 <p class="slds-tile__title slds-truncate">Total Expenses</p> 93 $<ui:outputNumber class="slds-truncate" value="{!v.total}" format=".00"/> 94 </div> 95 </div> 96 <div class="slds-tile "> 97 <div class="slds-notify slds-notify--toast slds-theme--alert-texture"> 98 <p class="slds-tile__title slds-truncate">No. of Expenses</p> 99 <ui:outputNumber class="slds-truncate" value="{!v.exp}"/> 100 </div> 101 </div> 102 </div> 103 </div> 104 <!-- Display expense records --> 105 <div class="container slds-p-top--medium"> 106 <div id="list" class="row"> 107 <aura:iteration items="{!v.expenses}" var="expense"> 108 109 <!-- If you’re using a namespace, 110 use the format 111 {!expense.myNamespace__myField__c} instead. --> 112 113 <p>{!expense.Name}, {!expense.Client__c}, 114 {!expense.Amount__c}, {!expense.Date__c}, 115 {!expense.Reimbursed__c}</p> 116 </aura:iteration> 117 </div> 118 </div> 119</aura:component>Components provide a rich set of attributes and browser event support. Attributes are typed fields that are set on a specific instance of a component, and can be referenced using an expression syntax. All aura:attribute tags have name and type values. For more information, see Supported aura:attribute Types.
The attributes and expressions here will become clearer as you build the app. {!v.exp} evaluates the number of expenses records and {!v.total} evaluates the total amount. {!c.createExpense} represents the client-side controller action that runs when the Submit button (1) is clicked, which creates a new expense. The press event in ui:button enables you to wire up the action when the button is pressed.
The expression {!v.expenses} wires up the component to the expenses object. var="expense" denotes the name of the variable to use for each item inside the iteration. {!expense.Client__c} represents data binding to the client field in the expense object. -
Click STYLE in the sidebar to create
a new resource named form.css. Enter these CSS
rule sets.
1.THIS .uiInputDateTime .datePicker-openIcon { 2 position: absolute; 3 left: 45%; 4 top: 45%; 5} 6 7.THIS .uiInputDateTime .timePicker-openIcon { 8 position: absolute; 9 left: 95%; 10 top: 70%; 11} 12 13.THIS .uiInputDefaultError li { 14 list-style: none; 15} -
Add the component to the app. In expenseTracker.app, add the new component to the markup.
This step adds <c:form /> to the markup. If you’re using a namespace, you can use <myNamespace:form /> instead. If you haven’t set a namespace prefix for your organization, use the default namespace c when referencing components that you’ve created.
1<aura:application> 2 <ltng:require styles="{!$Resource.SLDSv2 + 3 '/assets/styles/salesforce-lightning-design-system-ltng.css'}"/> 4 5 <div class="slds"> 6 <div class="slds-page-header"> 7 <div class="slds-grid"> 8 <div class="slds-col slds-has-flexi-truncate"> 9 <p class="slds-text-heading--label">Expenses</p> 10 <div class="slds-grid"> 11 <div class="slds-grid slds-type-focus slds-no-space"> 12 <h1 class="slds-text-heading--medium slds-truncate" title="My Expenses">My Expenses</h1> 13 </div> 14 </div> 15 </div> 16 </div> 17 </div> 18 19 <div class="slds-col--padded slds-p-top--large"> 20 <c:form /> 21 </div> 22 </div> 23 </aura:application> -
Save your changes and click Update Preview in the sidebar to preview your app. Alternatively, reload your browser.
Good job! You created a component that provides an input form and view of your expenses. Next, you’ll create the logic to display your expenses.