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 <aura:attribute name="expenses" type="Expense__c[]"/> 3 <aura:attribute name="newExpense" type="Expense__c" 4 default="{ 'sobjectType': 'Expense__c', 5 'Name': '', 6 'Amount__c': 0, 7 'Client__c': '', 8 'Date__c': '', 9 'Reimbursed__c': false 10 }"/> 11 <!-- If you registered a namespace, replace the previous aura:attribute tags with the following --> 12 <!-- <aura:attribute name="expenses" type="myNamespace.Expense__c[]"/> 13 <aura:attribute name="newExpense" type="myNamespace__Expense__c" 14 default="{ 'sobjectType': 'myNamespace__Expense__c', 15 'Name': '', 16 'myNamespace__Amount__c': 0, 17 'myNamespace__Client__c': '', 18 'myNamespace__Date__c': '', 19 'myNamespace__Reimbursed__c': false 20 }"/> --> 21 <!-- Attributes for Expense Counters --> 22 <aura:attribute name="total" type="Double" default="0.00" /> 23 <aura:attribute name="exp" type="Double" default="0" /> 24 25 <!-- Input Form using components --> 26 <div class="container"> 27 <form class="slds-form--stacked"> 28 <div class="slds-form-element slds-is-required"> 29 <div class="slds-form-element__control"> 30 31 <!-- If you registered a namespace, 32 the attributes include your namespace. 33 For example, value="{!v.newExpense.myNamespace__Amount__c}" --> 34 35 <ui:inputText aura:id="expname" label="Expense Name" 36 class="slds-input" 37 labelClass="slds-form-element__label" 38 value="{!v.newExpense.Name}" 39 required="true"/> 40 </div> 41 </div> 42 <div class="slds-form-element slds-is-required"> 43 <div class="slds-form-element__control"> 44 <ui:inputNumber aura:id="amount" label="Amount" 45 class="slds-input" 46 labelClass="slds-form-element__label" 47 value="{!v.newExpense.Amount__c}" 48 placeholder="20.80" required="true"/> 49 </div> 50 </div> 51 <div class="slds-form-element"> 52 <div class="slds-form-element__control"> 53 <ui:inputText aura:id="client" label="Client" 54 class="slds-input" 55 labelClass="slds-form-element__label" 56 value="{!v.newExpense.Client__c}" 57 placeholder="ABC Co."/> 58 </div> 59 </div> 60 <div class="slds-form-element"> 61 <div class="slds-form-element__control"> 62 <ui:inputDateTime aura:id="expdate" label="Expense Date" 63 class="slds-input" 64 labelClass="slds-form-element__label" 65 value="{!v.newExpense.Date__c}" 66 displayDatePicker="true"/> 67 </div> 68 </div> 69 <div class="slds-form-element"> 70 <ui:inputCheckbox aura:id="reimbursed" label="Reimbursed?" 71 class="slds-checkbox" 72 labelClass="slds-form-element__label" 73 value="{!v.newExpense.Reimbursed__c}"/> 74 <ui:button label="Submit" 75 class="slds-button slds-button--neutral" 76 labelClass="label" 77 press="{!c.createExpense}"/> 78 </div> 79 </form> 80 </div><!-- ./container--> 81 82 <!-- Expense Counters --> 83 <div class="container slds-p-top--medium"> 84 <div class="row"> 85 <div class="slds-tile "> 86 <!-- Make the counter red if total amount is more than 100 --> 87 <div class="{!v.total >= 100 88 ? 'slds-notify slds-notify--toast slds-theme--error slds-theme--alert-texture' 89 : 'slds-notify slds-notify--toast slds-theme--alert-texture'}"> 90 <p class="slds-tile__title slds-truncate">Total Expenses</p> 91 $<ui:outputNumber class="slds-truncate" value="{!v.total}" format=".00"/> 92 </div> 93 </div> 94 <div class="slds-tile "> 95 <div class="slds-notify slds-notify--toast slds-theme--alert-texture"> 96 <p class="slds-tile__title slds-truncate">No. of Expenses</p> 97 <ui:outputNumber class="slds-truncate" value="{!v.exp}"/> 98 </div> 99 </div> 100 </div> 101 </div> 102 <!-- Display expense records --> 103 <div class="container slds-p-top--medium"> 104 <div id="list" class="row"> 105 <aura:iteration items="{!v.expenses}" var="expense"> 106 107 <!-- If you’re using a namespace, 108 use the format 109 {!expense.myNamespace__myField__c} instead. --> 110 111 <p>{!expense.Name}, {!expense.Client__c}, 112 {!expense.Amount__c}, {!expense.Date__c}, 113 {!expense.Reimbursed__c}</p> 114 </aura:iteration> 115 </div> 116 </div> 117</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 extends="force:slds"> 2 <div class="slds"> 3 <div class="slds-page-header"> 4 <div class="slds-grid"> 5 <div class="slds-col slds-has-flexi-truncate"> 6 <p class="slds-text-heading--label">Expenses</p> 7 <div class="slds-grid"> 8 <div class="slds-grid slds-type-focus slds-no-space"> 9 <h1 class="slds-text-heading--medium slds-truncate" title="My Expenses">My Expenses</h1> 10 </div> 11 </div> 12 </div> 13 </div> 14 </div> 15 16 <div class="slds-col--padded slds-p-top--large"> 17 <c:form /> 18 </div> 19 </div> 20 </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.