
<aura:component >
<aura:attribute name="newItem" type="Camping_Item__c"
default="{ 'sobjectType': 'Camping_Item__c',
'Name': '',
'Quantity__c': 0,
'Price__c': 0,
'Packed__c': false }"/>
<aura:attribute name="items" type="Camping_Item__c[]"/>
<!-- CREATE NEW ITEM FORM -->
<form class="slds-form--stacked">
<lightning:input aura:id="itemname" label="Camping Item Name"
name="itemname"
value="{!v.newItem.Name}"
required="true"/>
<lightning:input type="number" aura:id="quantity" label="Quantity"
name="itemquantity"
value="{!v.newItem.silk__Quantity__c}"
/> <!--placeholder="1" min="1"-->
<lightning:input aura:id="price" label="Price"
formatter="currency"
name="itemprice"
value="{!v.newItem.silk__Price__c}"/>
<lightning:input type="checkbox" aura:id="packed" label="Packed?"
name="itempacked"
checked="{!v.newItem.silk__Packed__c}"/>
<lightning:button label="Create Camping Item"
class="slds-m-top--medium"
variant="brand"
onclick="{!c.clickCreateItem}"/>
</form>
<!-- / CREATE NEW ITEM FORM -->
<c:campingHeader/>
<div class="slds-card slds-p-top--medium">
<section class="slds-card__body">
<div id="list" class="row">
<aura:iteration items="{!v.items}" var="item">
<c:campingListItem item="{!item}"/>
</aura:iteration>
</div>
</section>
</div>
</aura:component>
CampingListController.js
({
clickCreateItem: function(component, event, helper) {
// Simplistic error checking
var validItem = true;
// Name must not be blank
var nameField = component.find("itemname");
var itemname = nameField.get("v.value");
if ($A.util.isEmpty(itemname)){
validItem = false;
nameField.set("v.errors", [{message:"Item name can't be blank123."}]);
}
else {
nameField.set("v.errors", null);
}
// Quantity must not be blank
var quantityField = component.find("quantity");
var quantity1 = nameField.get("v.value");
//alert('Alert quantity'+quantity);
console.log(quantity1);
if ($A.util.isEmpty(quantity1)){
validItem = false;
quantityField.set("v.errors", [{message:"Quantity can't be blank."}]);
}
else {
quantityField.set("v.errors", null);
}
var priceField = component.find("price");
var price = priceField.get("v.value");
if ($A.util.isEmpty(price)){
validItem = false;
priceField.set("v.errors", [{message:"Price can't be blank."}]);
}
else {
quantityField.set("v.errors", null);
}
if(validItem){
var newItem = component.get("v.newItem");
var item = JSON.parse(JSON.stringify(newItem));
console.log(item);
var itemarray = component.get("v.items");
itemarray.push(item);
component.set("v.items",itemarray);
}
component.set("v.newItem",{ 'sobjectType': 'Camping_Item__c','Name': '','Quantity__c': 0,
'Price__c': 0,'Packed__c': false});
}
})
CampinListItems.cmp
<aura:component >
<aura:attribute name="Item" type="Camping_Item__c" required="true"/>
<p>Name: {!v.Item.Name}</p>
<p> <lightning:formattedNumber value="{!v.Item.Price__c}" style="currency"/></p>
<p><lightning:formattedNumber value="{!v.Item.Quantity__c}" /></p>
<p><lightning:input type="toggle" label="Packed?" name="Packed" checked="{!v.Item.Packed__c}"/></p>
<p><lightning:button label="Packed!" onclick="{!c.packItem}"/></p>
</aura:component>
CampingHeader.cmp
<aura:component >
<lightning:layout class="slds-page-header slds-page-header--object-home">
<lightning:layoutItem >
<lightning:icon iconName="action:goal" alternativeText="My Component Item"/>
</lightning:layoutItem>
<lightning:layoutItem padding="horizontal-small">
<div class="page-section page-header">
<h1 class="slds-text-heading--label">Component Item</h1>
<h2 class="slds-text-heading--medium">My Component Item</h2>
</div>
</lightning:layoutItem>
</lightning:layout>
<!-- / PAGE HEADER -->
</aura:component>