Hello friends, In this post you will learn, how to add css class in parent node of a html element. This is very helpful for those people who want to highlight element dynamically. So let's get started,
Add css class in parent node of element in lightning component
In this example, I have provide two types of action, first one to highlight table row and second one to highlight column of table.
AddCss.cmp
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome" access="global" >AddCssController.js
<table>
<tr>
<td>
<b>Row Highlight</b>
</td>
<td>
<b>Employee Name</b>
</td>
<td>
<b>Column Highlight</b>
</td>
</tr>
<tr>
<td>
<input type="checkbox" onclick="{!c.highlightrow}"/>
</td>
<td>
<b>First Employee</b>
</td>
<td>
<input type="checkbox" onclick="{!c.highlightColumn}"/>
</td>
</tr>
</table>
</aura:component>
({AddCss.css
//used to highlight row
highlightrow : function(component, event, helper) {
if(event.target.checked){
$A.util.addClass(event.target.parentNode.parentNode,'rowhighlight');
}
else{
$A.util.removeClass(event.target.parentNode.parentNode,'rowhighlight');
}
},
//used to highlight column
highlightColumn : function(component, event, helper) {
if(event.target.checked){
$A.util.addClass(event.target.parentNode,'rowhighlight');
}
else{
$A.util.removeClass(event.target.parentNode,'rowhighlight');
}
}
})
.THIS .rowhighlight {
background-color:red;
}
Output:
Thank you.
0 Comments
Post a Comment