In this session, we will learn how to manipulate DOM Tree in Lightning Web Component.
What is DOM?
The acronym of DOM is “Document Object Model”.
DOM is a programming API for HTML & XML.
DOM is a logical structure(tree) of the document.
When DOM is created?
At the time of loading HTML/XML, DOM is created.
Please see my video on youtube on regarding this DOM Manipulation in Lightning Web Component
To understand DOM Manipulation, we have created one Lightning Web Component (LWC) named “showContactsPanel” by which we can show the Contact records based on an account.
According to the showContactsPanel.html file, DOM Tree looks like
Generally, in HTML, we can access particular node with respect to Id, Class, Tag by using document.getElementById(“#Id”), document.getElementsByClassName(“.Class”), document.getElementsByTagName(“h1”) or by using this.document.querySelector().
To manipulate the DOM in LWC, we will use this.template.querySelector() where “template” is the root node in LWC.
Steps to execute
Deploy the downloaded “showContactsPanel” LWC with Apex Class.
Add this LWC into any Lightning Page and assign to any Lightning Application.
Need to focus on manipulateDOM() method in the JS file.
Uncomment the lines for the particular scenario just as mentioned in the Video. Please do not uncomment all the lines in this method.
Refresh the Page in Lightning Experience.
Click on “Check DOM” button to see the output. May be, browser console need to be opened to see the output for certain test cases.
Q: Now, we want to see the Contact detail by clicking a button for every row. How can we achieve it?
If you open the “showContactsPanel” LWC, you can see entire second <tr> was commented in the table. Please uncomment this row and refresh the page. Now, you can see the specific Contact detail by clicking on each row.
We will analyze toggleRow() method in the JS file.
toggleRow(event){
event.preventDefault();
this.recId = event.target.dataset.id;
//get the attribute of DOM element using wild characters
//let currentElement = this.template.querySelector("[id^='" + this.recId + "']");
//console.log('currentElement-->'+currentElement.textContent);
this.recId = event.target.dataset.id;
let conList = this.template.querySelectorAll('.conli');
let btnList = this.template.querySelectorAll('.btn');
for (let j = 0; j < btnList.length; j++){
btnList[j].iconName = "utility:chevronright";
}
for (let i = 0; i < conList.length; i++){
let indvRow = conList[i];
let indvRowId = conList[i].getAttribute('id');
if(indvRowId.includes(this.recId)) {
if(indvRow.classList.contains("slds-show")){
indvRow.classList.add("slds-hide");
indvRow.classList.remove("slds-show");
event.target.iconName = "utility:chevronright";
}
else{
indvRow.classList.remove("slds-hide");
indvRow.classList.add("slds-show");
event.target.iconName = "utility:chevrondown";
}
}
else{
indvRow.classList.add("slds-hide");
indvRow.classList.remove("slds-show");
}
}
}
We have used data-id attribute in <lightning-button-icon> where data-id attribute contains the specific record id. If we want to get this record id on clicking, we have to use event.target.dataset just like event.target.dataset.id. After that, we need to compare the record id with the entire DOM to close the other rows’ panels and open the particular row panel for the specific Contact record just like