LWC Imp Concepts V4
LWC Imp Concepts V4
<template>
<lightning-layout>
</lightning-layout-item>
</lightning-layout>
</lightning-card>
</template>
JS File -
import { LightningElement,api } from 'lwc';
@api word;
Hangler(event) {
this.word = event.value.target;
<template>
<lightning-card title="EmployeeInfo">
<div class="slds-m-around_medium">
</div>
</lightning-card>
</template>
JS ---
@track name;
@track age;
@track salary;
nameHandler(event)
this.name=event.target.value;
ageHandler(event)
this.age=event.target.value;
salaryHandler(event)
this.salary=event.target.value;
</template>
Js
something=false;
<template>
<template if:true={show}>
<lightning-card title="Employee Information">
<div class="slds-n-around_medium">
<lightning-input label="Enter Name" value={name}
onchange={nameHandler}></lightning-input>
<lightning-input label="Enter Age" value={age} onchange={ageHandler}></lightning-
input>
<lightning-input label="Enter Salary" value={salary}
onchange={salaryHandler}></lightning-input>
<lightning-button label="Save" onclick={saveButtonHandler}></lightning-button>
</div>
</lightning-card>
</template>
<template if:false={show}>
<lightning-card title="Employee Details">
<div class="slds-m-around_medium">
<p>Employee Name: {name}</p>
<p>Employee Age : {age}</p>
<p>Employee Salary : {salary}</p>
</div>
</lightning-card>
</template>
</template>
Js
import { LightningElement,track } from 'lwc';
<template>
<lightning-card title="HelloForEach">
<ul class="slds-m-around_medium">
</template>
</ul>
</lightning-card>
</template>
Js
contacts=[
{Id:1,
Name:'Amy Taylor',
Title:'VP of Engineering'
},
Id:2,
Name:'Michael Jones',
Title:'VP of Sales'
},
Id:3,
Name:'Jennifer Wu',
Title:'CEO'
},
Id:4,
Name:'Jennifer Anigston',
Title:'VP of Marketing'
},
Id:5,
Name:'Deepika Khanna',
Title:'CTO'
----------------Iterator directive----------------
<template>
<lightning-card title="HelloForEach">
<ul class="slds-m-around_medium">
<template iterator:it={contacts}>
<li key={it.value.id}>
{it.value.Name},{it.value.Title}
</li>
</template>
</ul>
</lightning-card>
</template>
import { LightningElement } from 'lwc';
contacts=[
{Id:1,
Name:'Amy Taylor',
Title:'VP of Engineering'
},
Id:2,
Name:'Michael Jones',
Title:'VP of Sales'
},
Id:3,
Name:'Jennifer Wu',
Title:'CEO'
},
Id:4,
Name:'Jennifer Anigston',
Title:'VP of Marketing'
},
Id:5,
Name:'Deepika Khanna',
Title:'CTO'
}
]
<template>
</template>
@api show=false;
constructor()
super();
connectedCallback()
renderedCallback()
disconnectedCallback()
}
--------Reactive and nonreactive properties -----------
<template>
<tr>
<td>
<br/>
<b>value: {reactivePrivateProperty}</b>
</td>
</tr>
<tr>
<td>
<br/>
<b>value: {nonReactivePrivateProperty}</b>
</td>
</tr>
</table>
</template>
JS -----------
@track reactivePrivateProperty;
nonReactivePrivateProperty;
changeHandler1(event)
this.reactivePrivateProperty= event.target.value;
changeHandler2(event)
this.nonReactivePrivateProperty=event.target.value;
Parent component -
<template>
<c-p2c-alert-component
message="Hurray !! I got the data"
card-heading="Parent to Child primitive data communication"
number=20
is-valid
></c-p2c-alert-component>
</template>
Child comp-
<template>
<lightning-card title={cardHeading}>
<div class="slds-notify_alert slds-theme_error" role="alert">
{number} {message} {isValid}
</div>
</lightning-card>
</template>
------------------Parent comp
<template>
<div class="slds-var-m-bottom_medium"></div>
<lightning-card title="Parent to Child communication on action">
<div class="slds-var-m-around_medium">
<lightning-input type="number"
label="Enter percentage"
onkeyup={changeHandler}
value={percentage}
></lightning-input>
<c-p2c-progress-component
progress-value={percentage}
></c-p2c-progress-component>
</div>
</lightning-card>
</template>
<template>
<lightning-layout>
<c-child-comp></c-child-comp>
</lightning-layout-item>
</lightning-layout>
</lightning-card>
</template>
handleChangeEvent(event)
this.template.querySelector('c-child-comp').changeMessage(event.target.value);
Child Component –
<template>
</template>
export default class ChildComp extends LightningElement {
@track message;
@api
changeMessage(strString)
this.message=strString.toUpperCase();
<template>
<div class="slds-m-around_medium">
<c-child-component2 onmycustomevent={handleCustomEvent}><c-child-component2>
</div>
</template>
@track msg;
/*constructor()
super();
this.template.addEventListener('mycustomevent',this.handleCustomEvent.bind(this));
}*/
handleCustomEvent(event)
this.msg=event.detail;
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata"
fqn="parentComponent2">
<apiVersion>46.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__HomePage</target>
<target>lightning__RecordPage</target>
</targets>
</LightningComponentBundle>
<template>
<div class="slds-m-around_medium">
<lightning-input name="textVal" label="Enter Text" onchange={handleChange}></lightning-
input>
</div>
</lightning-card>
</template>
handleChange(event)
const name=event.target.value;
this.dispatchEvent(selectEvent);
<template>
<lightning-record-form record-id={recordId} object-api-name="Account" layout-
type="Compact" mode="view"></lightning-record-form>
</template>
import { LightningElement,api } from 'lwc';
<template>
<lightning-record-view-form record-id={recordId} object-api-name="Account">
<div class="slds-grid">
<div class="slds-col slds-size_1-of-2">
<lightning-output-field field-name="Name"></lightning-output-field>
<lightning-output-field field-name="Phone"></lightning-output-field>
</div>
<div class="slds-col slds-size_1-of-2">
<lightning-output-field field-name="Industry"></lightning-output-field>
<lightning-output-field field-name="AnnualRevenue"></lightning-output-field>
</div>
</div>
</lightning-record-view-form>
</template>
import { LightningElement,api } from 'lwc';
<template>
<lightning-record-edit-form record-id={recordId} object-api-name="Contact">
<lightning-output-field field-name="AccountId"></lightning-output-field>
<lightning-input-field field-name ="FirstName"></lightning-input-field>
<lightning-input-field field-name="LastName"></lightning-input-field>
<lightning-input-field field-name="Email"></lightning-input-field>
<lightning-button class="slds-m-top_small" variant="brand" type="submit"
name="update" label="Update"></lightning-button>
</lightning-record-edit-form>
</template>
</lightning-card>
</lightning-layout-item>
</lightning-layout>
</lightning-card>
</template>
contactNameChangeHandler(event)
{
this.contactName=event.target.value;
}
contactPhoneChangeHandler(event)
{
this.contactPhone=event.target.value;
}
contactEmailChangeHandler(event)
{
this.contactEmail=event.target.value;
}
createContact()
{
const
fields={'LastName':this.contactName,'Phone':this.contactPhone,'Email':this.contactEmail};
const recordInput={apiName:'Contact',fields};
createRecord(recordInput).then(response=>{
console.log('Contact has been created successfully ',response.id);
}).catch(error=>{
console.log('Error in creating contact: ',error.body.message);
});
}
}
<template>
<lightning-card title="All Contact Records">
<ul>
<template if:true={responseReceived}>
<template for:each={contacts.data} for:item="contact">
<li key={contact.id}>
<div class="slds-p-around_medium lgc-bg">
<lightning-tile label={contact.LastName}>
<p class="slds-truncate" title={contact.Phone}>{contact.Phone}</p>
</lightning-tile>
</div>
</li>
</template>
</template>
</ul>
</lightning-card>
</template>
<template>
<lightning-card>
<center>
<div class="slds-m-top_medium slds-m-bottom_x-large">
<h2 class="slds-text-heading_medium slds-m-bottom_medium">Show Toast
Notification Demo</h2>
</div>
<lightning-button-group>
<lightning-button label="Show Success" onclick={handleSuccess}></lightning-button>
<lightning-button label="Show Error" onclick={handleError}></lightning-button>
<lightning-button label="Show Warning" onclick={handleWarning}></lightning-
button>
<lightning-button label="Show Info" onclick={handleInfo}></lightning-button>
</lightning-button-group>
</center>
</lightning-card>
</template>
handleSuccess()
{
const showSuccess=new ShowToastEvent({
title:'Success!!',
message:'This is Success Message',
variant:'Success',
});
this.dispatchEvent(showSuccess);
}
handleError()
{
const showError=new ShowToastEvent({
title:'Error!!',
message:'This is a Error Message',
variant:'error',
});
this.dispatchEvent(showError);
}
handleWarning()
{
const showWarn=new ShowToastEvent({
title:'Warning!!',
message:'This is a Warning Message',
variant:'warning',
});
this.dispatchEvent(showWarn);
}
handleInfo()
{
const showInfo=new ShowToastEvent({
title:'Info!!',
message:'This is info Message',
variant:'info',
});
this.dispatchEvent(showInfo);
}
}
------------Navigation service---------
<template>
<lightning-card title="Navigation Service in Lightning Web components">
<lightning-card title="Navigate to Record Pages">
<lightning-button-group>
<lightning-button label="New Record Page"
onclick={navigateToNewRecordPage}></lightning-button>
<lightning-button label="Edit Record Page"
onclick={navigateToEditRecordPage}></lightning-button>
<lightning-button label="View Record Page"
onclick={navigateToViewRecordPage}></lightning-button>
</lightning-button-group> <br/>
</lightning-card>
<lightning-card title="Navigate to List Views">
<lightning-button-group>
<lightning-button label="Account Recent View"
onclick={navigateAccRecentView}></lightning-button>
<lightning-button label="Related List View"
onclick={navigateRelatedListView}></lightning-button>
</lightning-button-group>
</lightning-card><br/>
<lightning-card title="Navigate to Object Page">
<lightning-button-group>
</lightning-button-group>
</lightning-card>
</lightning-card>
</template>
navigateToEditRecordPage(){
this[NavigationMixin.Navigate]({
type:'standard__recordPage',
attributes:{
"recordId":this.recordId,
"objectApiName":"Account",
"actionName":"edit"
}
});
}
navigateToViewRecordPage(){
this[NavigationMixin.Navigate]({
type:'standard__recordPage',
attributes:{
"recordId":this.recordId,
"objectApiName":"Account",
"actionName":"view"
}
});
}
navigateAccRecentView(){
this[NavigationMixin.Navigate]({
"type":"standard__objectPage",
"attributes":{
"objectApiName":"Account",
"actionName":"list"
},
"state":{
"filterName":"Recent"
}
});
}
navigateRelatedListView(){
this[NavigationMixin.Navigate]({
"type":"standard__recordRelationshipPage",
"attributes":{
recordId:this.recordId,
objectApiName:'Account',
relationshipApiName:'Contacts',
actionName:'view'
}
});
}
navigateAccObject(){
this[NavigationMixin.Navigate]({
"type":"standard__objectPage",
"attributes":{
"objectApiName":"Account",
"actionName":"home"
}
});
}
navigateConObject(){
this[NavigationMixin.Navigate]({
"type":"standard__objectPage",
"attributes":{
"objectApiName":"Contact",
"actionName":"home"
}
});
}
navigateToWebPage(){
this[NavigationMixin.Navigate]({
"type":"standard__webPage",
"attributes":{
"url":"https://www.mytutorialrack.com/"
}
});
}
navigateToHomePage(){
this[NavigationMixin.Navigate]({
type:'standard__namedPage',
attributes:{
pageName:'home'
},
});
}
navigateToChatterHome()
{
this[NavigationMixin.Navigate]({
type:'standard__namedPage',
attributes:{
pageName:'chatter'
},
});
}
}
<template>
<lightning-layout multiple-rows>
onchange={PrincipalchangeHandler}></lightning-input>
</lightning-layout-item>
onchange={RateofInterrestchangeHandler}></lightning-input>
</lightning-layout-item>
<lightning-layout-item size="12" padding="around-Medium">
onchange={NumberOfyearchangeHandler}>
</lightning-input>
</lightning-layout-item>
<lightning-formatted-text value={CurrentOutPut}></lightning-formatted-text>
</lightning-layout-item>
</lightning-layout>
</lightning-card>
</template>
@track CurrentOutPut;
principal;
rate;
numberofyear;
PrincipalchangeHandler(event) {
}
RateofInterrestchangeHandler(event) {
this.rate = event.target.value;
NumberOfyearchangeHandler(event) {
this.numberofyear = event.target.value;
simpleSIhnaderler()