Newer Version Available

This content describes an older version of this product. View Latest

Step 4: Create a Nested Component

As your component grows, you want to break it down to maintain granularity and encapsulation. This step walks you through creating a component with repeating data and whose attributes are passed to its parent component. You’ll also add a client-side controller action to load your data on component initialization.
  1. Click File | New | Lightning Component.
  2. Enter expenseList in the New Lightning Bundle window. This creates a new component, expenseList.cmp.
  3. In expenseList.cmp, enter this code.
    Replace namespace with the name of your registered namespace.
    1swfobject.registerObject("clippy.codeblock-0", "9");<aura:component>
    2  <aura:attribute name="expense" type="namespace.Expense__c"/>
    3
    4  <!-- Color the item blue if the expense is reimbursed -->
    5  <div class="{!v.expense.namespace__Reimbursed__c == true 
    6                   ? 'listRecord recordLayout blue' : 'listRecord recordLayout white'}">
    7    <a aura:id="expense" href="{!'/' + v.expense.Id}">
    8      <div class="itemTitle">{!v.expense.Name}</div>
    9    </a>
    10      <div class="recordItem">Amount: 
    11          <ui:outputNumber 
    12           value="{!v.expense.namespace__Amount__c}" format=".00"/>
    13      </div>
    14      <div class="recordItem">Client: 
    15          <ui:outputText 
    16           value="{!v.expense.namespace__Client__c}"/>
    17      </div>
    18      <div class="recordItem">Date: 
    19          <ui:outputDateTime
    20           value="{!v.expense.namespace__Date__c}" />
    21      </div>
    22      <div class="recordItem">Reimbursed? 
    23          <ui:inputCheckbox 
    24           value="{!v.expense.namespace__Reimbursed__c}" click="{!c.update}"/>
    25      </div>
    26  </div>
    27</aura:component>

    Instead of using {!expense.namespace__Amount__c}, you’re now using {!v.expense.namespace__Amount__c}. This expression accesses the expense object and the amount values on it. Note that only custom fields must be prefixed with namespaces. expense is of type Expense__c. For non-primitive types, use the format myNamespace.myApexClass.

    Additionally, href="{!'/' + v.expense.Id}" uses the expense ID to set the link to the detail page of each expense record.

  4. Click STYLE in the sidebar and enter these CSS rule sets.
    1swfobject.registerObject("clippy.codeblock-1", "9");.THIS.recordLayout {
    2    list-style: none;
    3    padding: 14px;
    4    margin: 10px;
    5    border-bottom:1px solid #cfd4d9;
    6}
    7
    8.THIS.listRecord {
    9    margin: 14px;
    10    border-radius: 5px;
    11    border: 1px solid #cfd4d9;
    12    position: relative;
    13}
    14
    15.THIS .uiOutputText {
    16    line-height: 1.1em;
    17}
    18
    19.THIS .itemTitle {
    20    font-size: 15px;
    21    padding-bottom: 3px; 
    22    overflow: hidden;
    23    text-overflow: ellipsis;
    24    white-space: nowrap;
    25}
    26
    27.THIS .recordItem {
    28    text-overflow: ellipsis;
    29    white-space: nowrap;
    30}
    31
    32.THIS a:hover {
    33    text-decoration: none;
    34    background-color: #235636;
    35}
  5. 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-2", "9");    <aura:iteration items="{!v.expenses}" var="expense">
    2        <p>{!expense.Name}, {!expense.namespace__Client__c}, {!expense.namespace__Amount__c}, {!expense.namespace__Date__c}, {!expense.namespace__Reimbursed__c}</p>
    3    </aura:iteration>

    Replace it with an aura:iteration tag that uses the expenseList component. Replace namespace with the name of your registered namespace.

    1swfobject.registerObject("clippy.codeblock-3", "9");    <aura:iteration items="{!v.expenses}" var="expense">
    2      <namespace:expenseList expense="{!expense}"/>
    3    </aura:iteration>
    4

    Notice 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.

  6. Save your changes and reload your browser.
You created a nested component and pass its attributes to a parent component. Next, you’ll learn how to process user input and update the expense object.

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.