You need to sign in to do that
Don't have an account?
Pass value of Class from controller component Salesforce Lightning
Hi Folks,
I have the following code which displays an alert on my component:
<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId,lightning:actionOverride,lightning:backgroundUtilityItem" access="GLOBAL" controller="AlertController"> <aura:attribute name="Alert" type="Alert__c" default="{'sobjectType': 'Alert__c', 'Tipo_de_Alerta__c': '', 'Name': '', 'Alerta1__c': '' }"/> <!-- Alert --> <div aura:id="divToast" class="demo-only" style="height: 4rem;"> <div class="slds-notify_container slds-is-relative"> <div class="slds-notify slds-notify_toast slds-theme_warning" role="status"> <span class="slds-assistive-text">Warning</span> <span class="slds-icon_container slds-icon-utility-warning slds-m-right_small slds-no-flex slds-align-top" title=""> </span> <div class="slds-notify__content"> <h2 class="slds-text-heading_small ">Alerta 1</h2> <lightning:buttonIcon iconName="utility:close" variant="bare" alternativeText="Close" iconClass="{!v.type == 'warning' ? 'light' : 'dark'}" size="large" class="slds-button slds-button_icon slds-notify__close slds-button_icon-inverse" onclick="{!c.close}"/> </div> </div> </div> </div> </aura:component>
In line <div class="slds-notify slds-notify_toast slds-theme_warning" role="status">, class can be variable, the value of class, being:
<div class="slds-notify slds-notify_toast slds-theme_warning" role="status">
<div class="slds-notify slds-notify_toast slds-theme_error" role="status">
<div class="slds-notify slds-notify_toast slds-theme_success" role="status">
I need to pass the value of the class through the Controller.js of the component.
It would be something of the type :
<div class="{!v.Alert.Tipo_de_Alerta__c}" role="status">
and in Controller.js according to the condition, pass the class:
doInit : function(component, event, helper)
{
if (Condition== true)
{
component.set("v.Tipo_de_Alerta__c", "slds-notify slds-notify_toast slds-theme_warning" );
}
if (Another Condition == true)
{
component.set("v.Tipo_de_Alerta__c", "slds-notify slds-notify_toast slds-theme_error" );
}
}
...
Does anyone know how to tell me?
Thanks
Yes you can set the Class from Controller by Using aura:id and $A.util.addClass
1. Declare the Aura:Id in div Like Below:
<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId,lightning:actionOverride,lightning:backgroundUtilityItem" access="GLOBAL" controller="AlertController">
<aura:attribute name="Alert" type="Alert__c" default="{'sobjectType': 'Alert__c',
'Tipo_de_Alerta__c': '',
'Name': '',
'Alerta1__c': ''
}"/>
<!-- Alert -->
<div aura:id="divToast" class="demo-only" style="height: 4rem;">
<div class="slds-notify_container slds-is-relative">
<div aura:id="divNotific" class="slds-notify" role="status">
<span class="slds-assistive-text">Warning</span>
<span class="slds-icon_container slds-icon-utility-warning slds-m-right_small slds-no-flex slds-align-top" title="">
</span>
<div class="slds-notify__content">
<h2 class="slds-text-heading_small ">Alerta 1</h2>
<lightning:buttonIcon iconName="utility:close" variant="bare" alternativeText="Close"
iconClass="{!v.type == 'warning' ? 'light' : 'dark'}" size="large"
class="slds-button slds-button_icon slds-notify__close slds-button_icon-inverse"
onclick="{!c.close}"/>
</div>
</div>
</div>
</div>
</aura:component>
2. From Controller use the $A.Util.addClass:
doInit : function(component, event, helper)
{
if (Condition== true)
{
$A.util.addClass(component.find("divNotific"), "slds-notify_toast slds-theme_warning");
}
if (Another Condition == true)
{
$A.util.addClass(component.find("divNotific"), "slds-notify_toast slds-theme_error");
}
....
Reference Link :
http://www.biswajeetsamal.com/blog/tag/adding-and-removing-styles/
Can you please Let me know if it helps or not!!!
If it helps don't forget to mark this as a best answer!!!
Thanks,
Maharajan.C
All Answers
The Controller is empty, I need to write it
doInit : function(component, event, helper)
{
// var alerta = "slds-notify slds-notify_toast slds-theme_warning";
// component.set("v.Tipo_de_Alerta__c", alerta );
},
Yes you can set the Class from Controller by Using aura:id and $A.util.addClass
1. Declare the Aura:Id in div Like Below:
<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId,lightning:actionOverride,lightning:backgroundUtilityItem" access="GLOBAL" controller="AlertController">
<aura:attribute name="Alert" type="Alert__c" default="{'sobjectType': 'Alert__c',
'Tipo_de_Alerta__c': '',
'Name': '',
'Alerta1__c': ''
}"/>
<!-- Alert -->
<div aura:id="divToast" class="demo-only" style="height: 4rem;">
<div class="slds-notify_container slds-is-relative">
<div aura:id="divNotific" class="slds-notify" role="status">
<span class="slds-assistive-text">Warning</span>
<span class="slds-icon_container slds-icon-utility-warning slds-m-right_small slds-no-flex slds-align-top" title="">
</span>
<div class="slds-notify__content">
<h2 class="slds-text-heading_small ">Alerta 1</h2>
<lightning:buttonIcon iconName="utility:close" variant="bare" alternativeText="Close"
iconClass="{!v.type == 'warning' ? 'light' : 'dark'}" size="large"
class="slds-button slds-button_icon slds-notify__close slds-button_icon-inverse"
onclick="{!c.close}"/>
</div>
</div>
</div>
</div>
</aura:component>
2. From Controller use the $A.Util.addClass:
doInit : function(component, event, helper)
{
if (Condition== true)
{
$A.util.addClass(component.find("divNotific"), "slds-notify_toast slds-theme_warning");
}
if (Another Condition == true)
{
$A.util.addClass(component.find("divNotific"), "slds-notify_toast slds-theme_error");
}
....
Reference Link :
http://www.biswajeetsamal.com/blog/tag/adding-and-removing-styles/
Can you please Let me know if it helps or not!!!
If it helps don't forget to mark this as a best answer!!!
Thanks,
Maharajan.C