Skip to main content

LWC Change Message

Requirement :



Create a LWC that show a message in the center of the component on the basis of any one of the 3 button clicked. Initially, when the component loads, the message in the center of the component should say: "How's the weather Today ?". There will be 3 buttons with these properties below the message:

a) Btn 1- Color- Red, Label- Hot- On click changes the message to: "Its Hot !"
b) Btn 2- Color- Blue, Label- Cool- On Click changes the message to: "Its Cool"
c) Btn 3- Color- Green, Label- Hazy- On Click changes the message to:"Not Very
clear"

or in other words

Change message on button clicked.

cmpChangeMessage.html

<template>
    <center>
        <lightning-card title="Change Message on button click">            
                <lightning-badge label={message}></lightning-badge>            
            <br><br>
            <lightning-button 
                variant="destructive"
                label="Hot" 
                onclick={hotbutton}>
            </lightning-button>
            &nbsp;&nbsp;               
            <lightning-button 
                variant="brand"
                label="Cool" 
                onclick={coolbutton}>
            </lightning-button>
            &nbsp;&nbsp;
            <lightning-button 
                variant="success"
                label="Hazy" 
                onclick={clearbutton}>
            </lightning-button>
        </lightning-card>
    </center>
</template>

cmpChangeMessage.js

import { LightningElementtrack } from 'lwc';
export default class cmpChangeMessage extends LightningElement {
    @track message = 'How \'s the weather Today ?';
    hotbutton(){
        this.message = 'Its Hot !';
    }
    coolbutton(){
        this.message = 'Its Cool !';
    }
    clearbutton(){
        this.message = 'Not very clear !';
    }
}

cmpChangeMessage.js-meta.xml

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>48.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__AppPage</target>
        <target>lightning__RecordPage</target>
        <target>lightning__HomePage</target>
        <target>lightning__Tab</target>
      </targets>
</LightningComponentBundle>

Output :

Other Useful Blog

Lightning Component

Happy Learning !!


Comments

Popular posts from this blog

LWC Development Environment

Requirement : ‹ › Setup LWC Development Environment  1. VS (Visual Stdio) Code 2. Salesforce CLI Download and install the latest version of  Visual Studio Code . Open the Visual Studio Code and add extensions by clicking on the extensions icon present on the left side of the VS code editor. Search for  Salesforce Extension Pack  and install the extension. After that install  Lightning Web Components extension. Once you done with installing the above two extensions Re-launch the VS code editor. Now it’s time to check that our environment is ready for creating Lightning Web Components by pressing  Command + Shift + P  on  macOS or Ctrl + Shift + P  on Windows and type  sfdx . Hola..!! you are now ready to create your Lightning Web Components. Creating SFDX Project First create a folder somewhere on your machine and name it  trailblazer  or anything of your choice. Navigate to that folder from VS code ...

LWC Update Single Record Datatable

Requirement : ‹ › Create a LWC in which get the contact records in Datatable and Update single record. Or in other words Update Single Record through Datatable. cmpDatatableUpdateSingleRecord.html < template >           < lightning-card   title = "Update Contact Record"   icon-name = "custom:custom63" >          < div   class = "slds-m-around_medium" >              < template   if:true = {contact.data} >                  < lightning-datatable                      key-field = "Id"                    ...

LWC Decorators

Decorators : ‹ › In LWC decorators are used to add functionality to a property or function. There are three types of decorators : 1. @track 2. @api 3. @wire @track : It is used to make properties or methods in LWC is private. This properties is not accessible outside the LWC in which it is declared. @api : It is used to make properties or methods in LWC is public. This way they are accessible outside of the component in which they are declared. For ex : parent component can set the value of the properties in the child component. @wire : It is used to read the Salesforce data from apex class into LWC. It is generally used while making the server call to apex class. LWC Properties : There are two type of LWC properties. 1. Reactive Properties 2. Non-Reactive Properties Reactive Properties : In case of reactive properties , value of the properties change when the component re-render. There are two types of Reactive Properties. 1. Public properties...