Using Component.find vs Document.getElementById to Dynamically Hide or Show Content in Lightning Components
Business Scenario: To dynamically Show or Hide content in Lightning Components.
Using "Document.getElementById"
HideShow.cmp
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" access="global" > <!--Set component attribute--> <aura:attribute name="valSet" type="Set"/> <center> <!--Button to display Set values--> <lightning:button onclick="{!c.getSetValues}" label="Set Values"/> </center> <!--Construct HTML Table using SLDS--> <table class="slds-table slds-table--bordered slds-table--striped slds-table--cell-buffer slds-table--fixed-layout"> <thead> <tr class="slds-text-heading--label"> <th scope="col"><div class="slds-truncate" title="ID">SET DATA</div></th> </tr> </thead> <tbody id="bodyId"> <aura:iteration items="{!v.valSet}" var="s"> <tr> <td><div class="slds-truncate" title="{!s}">{!s}</div></td> </tr> </aura:iteration> </tbody> </table> </aura:component>
HideShow.js
/** Client-side Controller **/
({
getSetValues: function(cmp) {
// Get dom id for the table body
var x = document.getElementById("bodyId");
// Variable to input Set values manually
var tempSet = ['SetValue1', 'SetValue2', 'SetValue3', 'SetValue4', 'SetValue5', 'SetValue6'];
// Assign above variable to the Set attribute
cmp.set("v.valSet", tempSet);
// Below logic to show/hide table body based on user click
if (x.style.display === "block") {
x.style.display = "none";
} else {
x.style.display = "block";
}
}
})
Output:

Using "Component.find"
HideShow.cmp
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" access="global" > <!--Set component attribute--> <aura:attribute name="valSet" type="Set"/> <aura:handler name="init" value="{!this}" action="{!c.doInit}"/> <center> <!--Button to display Set values--> <lightning:button onclick="{!c.toggleText}" label="Set Values"/> </center> <!--Construct HTML Table using SLDS--> <table class="slds-table slds-table--bordered slds-table--striped slds-table--cell-buffer slds-table--fixed-layout" aura:id="bodyId"> <thead> <tr class="slds-text-heading--label"> <th scope="col"><div class="slds-truncate" title="ID">SET DATA</div></th> </tr> </thead> <tbody> <aura:iteration items="{!v.valSet}" var="s"> <tr> <td><div class="slds-truncate" title="{!s}">{!s}</div></td> </tr> </aura:iteration> </tbody> </table> </aura:component>
HideShow.js
/** Client-side Controller **/
({
doInit: function(cmp) {
// Variable to input Set values manually
var tempSet = ['SetValue1', 'SetValue2', 'SetValue3', 'SetValue4', 'SetValue5', 'SetValue6'];
// Assign above variable to the Set attribute
cmp.set("v.valSet", tempSet);
},
toggleText: function(cmp) {
var toggleText = cmp.find("bodyId");
$A.util.toggleClass(toggleText, "toggle");
}
})
HideShow.css
/*toggleCss.css*/
.THIS.toggle {
display: none;
}
Output:

Reference Links:
ReplyDeleteGreat Blog!!! Was an interesting blog with a clear concept. And will surely help many to update them.
Salesforce Training in Chennai
Salesforce Training
ccna Training in Chennai
PHP Training in Chennai
ReactJS Training in Chennai
ui ux design course in chennai
Angularjs Training in Chennai
web designing training in chennai
Tally Course in Chennai
Great information thank you so much,
ReplyDeleteUpdate Multiple Workflow Rulesin few clicks
Hi sir,I have seen your posts.It is very thoughtful information for freshers and starters.
ReplyDeleteCyber Security Training Course in Chennai | Certification | Cyber Security Online Training Course | Ethical Hacking Training Course in Chennai | Certification | Ethical Hacking Online Training Course |
CCNA Training Course in Chennai | Certification | CCNA Online Training Course | RPA Robotic Process Automation Training Course in Chennai | Certification | RPA Training Course Chennai | SEO Training in Chennai | Certification | SEO Online Training Course
Buy high quality Grade A counterfeit money Real Passports, Visas, Driver's License ,ID CARDS, Marriage certificates, Diplomas, Birth Certificates, Credit cards, Utility bills, Social Security cards, Resident permits, Death certificates, Seaman cards, e.t.c Text/WhatsApp +17202488130 ( lifiben@gmail.com )
ReplyDeletehigh quality undetectable counterfeit banknotes for sale
buy high quality counterfeit money online
buy undetectable counterfeit money online cheap
buy 100% undetectable counterfeit money uk
high quality counterfeit money for sale
counterfeit money supplier
best place to buy counterfeit money
buy counterfeit money online
Text/WhatsApp +17202488130 ( lifiben@gmail.com )