Newer Version Available
Step 4: Create a Nested Component
- Click .
- Enter expenseList in the New Lightning Bundle window. This creates a new component, expenseList.cmp.
-
In expenseList.cmp, enter this code.
1swfobject.registerObject("clippy.codeblock-0", "9"); 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17<aura:component> 18 <aura:attribute name="expense" type="Expense__c"/> 19 20 <!-- Color the item blue if the expense is reimbursed --> 21 22 <!-- If you registered a namespace, use v.expense.myNamespace__Reimbursed__c instead. --> 23 <div class="{!v.expense.Reimbursed__c == true 24 ? 'alert alert-success' : 'alert alert-warning'}"> 25 <a aura:id="expense" href="{!'/' + v.expense.Id}"> 26 <h3>{!v.expense.Name}</h3> 27 </a> 28 29 <!-- If you registered a namespace, 30 replace the following values with v.expense.myNamespace__fieldName__c instead --> 31 <p>Amount: 32 <ui:outputNumber value="{!v.expense.Amount__c}" format=".00"/> 33 </p> 34 <p>Client: 35 <ui:outputText value="{!v.expense.Client__c}"/> 36 </p> 37 <p>Date: 38 <ui:outputDateTime value="{!v.expense.Date__c}" /> 39 </p> 40 <p>Reimbursed? 41 <ui:inputCheckbox value="{!v.expense.Reimbursed__c}" click="{!c.update}"/> 42 </p> 43 </div> 44</aura:component>Instead of using {!expense.Amount__c}, you’re now using {!v.expense.Amount__c}. This expression accesses the expense object and the amount values on it.
Additionally, href="{!'/' + v.expense.Id}" uses the expense ID to set the link to the detail page of each expense record.
-
In form.cmp, update the aura:iteration tag to use the new nested component, expenseList. Locate the existing aura:iteration tag.
1swfobject.registerObject("clippy.codeblock-1", "9"); 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17<aura:iteration items="{!v.expenses}" var="expense"> 18 <p>{!expense.Name}, {!expense.Client__c}, {!expense.Amount__c}, {!expense.Date__c}, {!expense.Reimbursed__c}</p> 19</aura:iteration>Replace it with an aura:iteration tag that uses the expenseList component.
1swfobject.registerObject("clippy.codeblock-2", "9"); 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17<aura:iteration items="{!v.expenses}" var="expense"> 18<!--If you’re using a namespace, use myNamespace:expenseList instead--> 19 <c:expenseList expense="{!expense}"/> 20</aura:iteration> 21Notice how the markup is simpler as you’re just passing each expense record to the expenseList component, which handles the display of the expense details.
- Save your changes and reload your browser.
Beyond the Basics
When you create a component, you are providing the definition of that component. When you put the component in another component, you are create a reference to that component. This means that you can add multiple instances of the same component with different attributes. For more information about component attributes, see Component Composition.