Jul 3, 2024
17 Views
Comments Off on Using Ternary Operator in Salesforce Lightning Components
0 0

Using Ternary Operator in Salesforce Lightning Components

Written by

The below code shows how to use Ternary Operator in Salesforce Lightning Components:

Ternary_Operator.cmp

<aura:component >
    <aura:attribute name=”isError” type=”Boolean” description=”If Name is empty then true, else false”/>
    <aura:handler name=”init” value=”{!this}” action=”{!c.doInit}” />
    <div class=”init”> 
        <lightning:input label=”Enter your Name” aura_id=”FullName” class=”{!v.isError == true ? ‘error errorBorder’ : ‘correct correctBorder’}” onchange=”{!c.handleNameChange}” />
    </div>
</aura:component>

Ternary_Operator.js

({
    doInit: function(component) {
        component.set(“v.isError”,true);
    },
    handleNameChange : function(component){
        var name = component.find(“FullName”).get(“v.value”);
        if(name == null || name == ” || name == undefined){
            component.set(“v.isError”,true);
        }
        else{
            component.set(“v.isError”,false); 
        }
    },
})

Ternary_Operator.css

.THIS {}
.THIS .error{ background-color:#D25A5A;}
.THIS .correct{ background-color:#a3ea82;}
.THIS .errorBorder{ border-color: red; border-width: 10px; border-style: solid;}
.THIS .correctBorder{ border-color: green; border-width: 10px; border-style: solid; }
.THIS.init{ width:50%; position:relative; left:200px; top:100px; }

Ternary_OperatorApp.app

<aura:application extends=”force:slds”> <c:Ternary_Operator/></aura:application>

Happy Salesforce Coding!

Article Categories:
Lightning
Priya Singh
https://thestarbiznews.com/

Expertise: Technology Trends, Startups, Digital Disruption Bio: Priya Singh is a tech-savvy millennial with a finger on the pulse of the ever-evolving digital landscape. A graduate of Stanford's Computer Science program and a former engineer at a Silicon Valley giant, Priya has a deep understanding of the technologies shaping our future. Her passion lies in demystifying complex tech jargon and exploring the disruptive potential of emerging trends, making her articles essential reading for anyone who wants to stay ahead of the curve. When not scouring the web for the latest tech tidbits, Priya enjoys traveling to off-the-beaten-path destinations and immersing herself in diverse cultures, always seeking new inspiration and insights.