Sunday, May 16, 2021

Utility Methods in Lightning Aura Component

1. addClass(): This method is used to add css class dynamically to any component to apply css styles.

Syntax: 
addClass(Object element, String newClass);

Sample Code:
// find a component with aura:id="myCmp" in markup
var myCmp = component.find("myCmp");
$A.util.addClass(myCmp, "myClass");

2. getBooleanValue(): This  method is used to get true or false value when an Object is returned.
Returns true if the object is truthy, or false otherwise.

Syntax: getBooleanValue(Object val);

3. hasClass(): This method is used to check whether the component has specific css class. Returns true if the specified class is found for the component, or false otherwise.

Syntax: hasClass(Object element, String className);

Sample Code:
// find a component with aura:id="myCmp" in markup
var myCmp = component.find("myCmp");
$A.util.hasClass(myCmp, "myClass");

4. isArray(): This method is used to check whether the specified object is array or not. Returns true if the object is an array, or false otherwise.

Syntax: isArray(Object obj);

5. isEmpty(): This method is used to check whether the object is empty, empty of the object in the sense object is undefined, null, empty of an array or string. Returns true if the object is empty, or false otherwise.

Syntax: isEmpty(Object obj);

6. isObject(): This method is used to check whether the specified object is valid or not. Returns true if the object is a valid object, or false otherwise.

Syntax: isObject(Object obj);

7. isUndefined(): This method is used to check whether the object is undefined. Returns true if the object is undefined, or false otherwise.

Syntax: isUndefined(Object obj);

8. isUndefinedOrNull(): This method is used to check whether the object is undefined or null. Returns true if the object is undefined or null, or false otherwise.

Syntax: isUndefinedOrNull(Object obj);

9. removeClass(): This method is used to remove css class dynamically to any component to remove css styles.

Syntax: removeClass(Object element, String newClass);

Sample Code:
//find a component with aura:id="myCmp" in markup
var myCmp = component.find("myCmp");
$A.util.removeClass(myCmp, "myClass");

10. toggleClass(): This method is used to add or remove css class dynamically to any component, it will help to apply and remove css styles dynamically by calling this method.

Syntax: toggleClass(Object element, String className);

Sample Code: 
// find a component with aura:id="toggleMe" in markup
var toggleText = component.find("toggleMe");
$A.util.toggleClass(toggleText, "toggle");

No comments:

Post a Comment