Hey guys, today in this post we are going to learn about How to Create dynamic tree grid with expanded/collapsed selected rows and select checkbox for the entire row select/deselect in Salesforce lightning web component LWC.
A lightning-tree-grid component displays hierarchical data in a table. Its appearance resembles lightning-datatable since it implements lightning-datatable internally, with the exception that each row in the table can be expanded to reveal a nested group of items. Rows that contain nested data display a chevron icon to denote that they can be expanded or collapsed. This visual representation is useful for displaying structured data such as account hierarchy or forecasting data. To know more details about lightning-tree-grid, Click Here..
This component implements the tree grid blueprint in the Lightning Design System.
Inline editing and sorting of columns are not supported.
Supported features include.
- Displaying and formatting of columns with appropriate data types
- Header-level actions
- Row-level actions
- Resizing of columns
- Selecting of rows
- Text wrapping and clipping
- Don’t forget to check out:- how to create progress indicator bar circular/ring or horizontal slider/range in Salesforce LWC
Files we used to create lightning tree grid in Salesforce LWC →
lwcTreeGrid.html | LWc HTML File | Template HTML file to display expande/collapse selected rows in Salesforce Lightning Web Component (LWC) |
lwcTreeGrid.js | LWC JavaScript File | It’s hold getCurrentExpandedRows action method to retrieve the list of rows in Salesforce LWC. |
sampleData.js | LWC JavaScript File | It’s export the gridData for expanded rows in Salesforce LWC. |
lwcTreeGrid.css | Style CSS | It’s fill the background color on the page in Salesforce LWC. |
lwcTreeGrid.js-meta.xml | XML Meta File | It is used to where this lightning web component file you want to display as lightning__AppPage, lightning__RecordPage, lightning__HomePage. |
Final Output →
Other related post that would you like to learn in Salesforce
Create Lightning Web Component HTML →
Step 1:- Create Lightning Web Component : lwcTreeGrid.html
SFDX:Lightning Web Component >> New >> lwcTreeGrid.html
lwcTreeGrid.html [Lightning Web Component HTML]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
<template> <lightning-card> <div class="slds-p-around_medium"> <h3 class="slds-text-heading_medium"><lightning-icon icon-name="custom:custom92" size="small"></lightning-icon> <strong style="color:#270086; font-size:13px; margin-right:5px;"> Tree Grid:- Displays a hierarchical view of data in a table in Lightning Web Component (LWC) </strong></h3> <br/><br/> <div class="slds-p-around_medium lgc-bg"> <lightning-tree-grid columns={gridColumns} data={gridData} key-field="name" ></lightning-tree-grid> <br/> <br/> <!--Start RelatedTopics Section--> <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;"> <p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/05/thumbsUpLike.png" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:16px; font-style:italic; display:inline-block; margin-right:5px;">Don't forget to check out:-</span><a href="https://www.w3web.net/" target="_blank" rel="noopener noreferrer" style="text-decoration:none;" data-aura-rendered-by="440:0">An easy way to learn step-by-step online free Salesforce tutorial, To know more Click <span style="color:#ff8000; font-size:18px;" data-aura-rendered-by="442:0">Here..</span></a></strong></p> <br/><br/> <p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/07/tickMarkIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:17px; font-style:italic; display:inline-block; margin-right:5px; color:rgb(255 128 0);">You May Also Like →</span> </strong></p> <div style="display:block; overflow:hidden;"> <div style="width: 50%; float:left; display:inline-block"> <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> <li><a href="https://www.w3web.net/lwc-get-set-lightning-checkbox-value/" target="_blank" rel="noopener noreferrer">How to get selected checkbox value in lwc</a></li> <li><a href="https://www.w3web.net/display-account-related-contacts-in-lwc/" target="_blank" rel="noopener noreferrer">how to display account related contacts based on AccountId in lwc</a></li> <li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to create lightning datatable row actions in lwc</a></li> <li><a href="https://www.w3web.net/if-and-else-condition-in-lwc/" target="_blank" rel="noopener noreferrer">how to use if and else condition in lwc</a></li> <li><a href="https://www.w3web.net/get-selected-radio-button-value-and-checked-default-in-lwc/" target="_blank" rel="noopener noreferrer">how to display selected radio button value in lwc</a></li> </ul> </div> <div style="width: 50%; float:left; display:inline-block"> <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> <li><a href="https://www.w3web.net/display-account-related-contacts-lwc/" target="_blank" rel="noopener noreferrer">display account related contacts based on account name in lwc</a></li> <li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to insert a record of account Using apex class in LWC</a></li> <li><a href="https://www.w3web.net/fetch-picklist-values-dynamic-in-lwc/" target="_blank" rel="noopener noreferrer">how to get picklist values dynamically in lwc</a></li> <li><a href="https://www.w3web.net/edit-save-and-remove-rows-dynamically-in-lightning-component/" target="_blank" rel="noopener noreferrer">how to edit/save row dynamically in lightning component</a></li> <li><a href="https://www.w3web.net/update-parent-object-from-child/" target="_blank" rel="noopener noreferrer">update parent field from child using apex trigger</a></li> </ul> </div> <div style="clear:both;"></div> <br/> <div class="youtubeIcon"> <a href="https://www.youtube.com/channel/UCW62gTen2zniILj9xE6LmOg" target="_blank" rel="noopener noreferrer"><img src="https://www.w3web.net/wp-content/uploads/2021/11/youtubeIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;"/> <strong>TechW3web:-</strong> To know more, Use this <span style="color: #ff8000; font-weight: bold;">Link</span> </a> </div> </div> </div> <!--End RelatedTopics Section--> </div> </div> </lightning-card> </template> |
Create Lightning Web Component JavaScript →
Step 2:- Create Lightning Web Component : lwcTreeGrid.js
SFDX:Lightning Web Component >> New >> lwcTreeGrid.js
lwcTreeGrid.js [LWC JavaScript File]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import { LightningElement, track } from 'lwc'; import { EXAMPLES_COLUMNS_DEFINITION_BASIC, EXAMPLES_DATA_BASIC, } from './sampleData'; export default class LwcTreeGrid extends LightningElement { @track currentExpandedRows; // definition of columns for the tree grid gridColumns = EXAMPLES_COLUMNS_DEFINITION_BASIC; // data provided to the tree grid gridData = EXAMPLES_DATA_BASIC; // list of names for rows that are expanded gridExpandedRows = [ '123556', '123556-A', '123556-B', '123556-B-B', '123557', ]; // retrieve the list of rows currently marked as expanded getCurrentExpandedRows() { const treegrid = this.template.querySelector('.lgc-example-treegrid'); this.currentExpandedRows = treegrid.getCurrentExpandedRows().toString(); } } |
Create Lightning Web Component JavaScript →
Step 3:- Create Lightning Web Component : sampleData.js
SFDX:Lightning Web Component >> New >> sampleData.js
sampleData.js [LWC JavaScript File]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 |
export const KEYFIELD = 'name'; export const COLUMNS_DEFINITION_BASIC = [ { type: 'text', fieldName: 'accountName', label: 'Account Name', }, { type: 'phone', fieldName: 'phone', label: 'Phone Number', }, ]; export const COLUMNS_DEFINITION_NONWHITELIST = [ { type: 'text', fieldName: 'accountName', label: 'Account Name', }, { type: 'phone', fieldName: 'phone', label: 'Phone Number', sortable: true, }, ]; export const EXAMPLES_COLUMNS_DEFINITION_BASIC = [ { type: 'text', fieldName: 'accountName', label: 'Account Name', initialWidth: 300, }, { type: 'number', fieldName: 'employees', label: 'Employees', }, { type: 'phone', fieldName: 'phone', label: 'Phone Number', }, { type: 'url', fieldName: 'accountOwner', label: 'Account Owner', typeAttributes: { label: { fieldName: 'accountOwnerName' }, }, }, { type: 'text', fieldName: 'billingCity', label: 'Billing City', }, ]; export const EXPANDED_ROWS_BASIC = ['584996-s7', '377526-zg']; export const EXPANDED_ROWS_MISSING_CHILDREN_CONTENT = [ '584996-s7', '377526-zg', '816682-xr', ]; export const EXPANDED_ROWS_INVALID = [ '584996-s7', '377526-zg', 'AWEFUL-bad_iD', '882894-s3', 'PiCkLeS', '31337-ID', ]; export const SELECTED_ROWS_BASIC = ['125313-7j', '584996-s7']; export const SELECTED_ROWS_INVALID = [ '584996-s7', '377526-zg', 'AWEFUL-bad_iD', '882894-s3', 'PiCkLeS', '31337-ID', ]; export const DATA_BASIC = [ { name: '125313-7j', accountName: 'Dach-Welch', phone: '837-555-0100', }, { name: '584996-s7', accountName: 'Corkery-Windler', phone: '837-555-0100', _children: [ { name: '747773-jw', accountName: 'Corkery-Abshire', phone: '837-555-0100', }, { name: '377526-zg', accountName: 'Robel, Friesen and Flatley', phone: '837-555-0100', _children: [ { name: '955330-wp', accountName: 'Donnelly Group', phone: '837-555-0100', }, { name: '343693-9x', accountName: 'Kshlerin Group', phone: '837-555-0100', }, ], }, { name: '638483-y2', accountName: 'Bruen, Steuber and Spencer', phone: '837-555-0100', }, ], }, { name: '306979-mx', accountName: 'Spinka LLC', phone: '837-555-0100', }, { name: '066195-o1', accountName: 'Koelpin LLC', phone: '837-555-0100', _children: [], }, ]; export const DATA_MISSING_CHILDREN_CONTENT = [ { name: '125313-7j', accountName: 'Dach-Welch', phone: '837-555-0100', }, { name: '584996-s7', accountName: 'Corkery-Windler', phone: '837-555-0100', _children: [], }, { name: '816682-xr', accountName: 'Schmitt-Littel', phone: '837-555-0100', _children: [ { name: '118985-mf', accountName: 'Hegmann-Turcotte', phone: '837-555-0100', }, { name: '841476-yo', accountName: 'Kuhlman LLC', phone: '837-555-0100', }, ], }, { name: '653331-j4', accountName: 'Swaniawski-Hilpert', phone: '366-145-6134', _children: [ { name: '605249-ei', accountName: 'Swaniawski, Veum and Barton', phone: '837-555-0100', }, { name: '686777-5d', accountName: 'Lubowitz, McClure and Russel', phone: '837-555-0100', }, { name: '582166-n4', accountName: 'Reichel-Jerde', phone: '837-555-0100', _children: [ { name: '513683-mm', accountName: 'Tromp Inc', phone: '837-555-0100', }, ], }, ], }, { name: '306979-mx', accountName: 'Spinka LLC', phone: '837-555-0100', }, { name: '066195-o1', accountName: 'Koelpin LLC', phone: '837-555-0100', _children: [], }, ]; export const EXAMPLES_DATA_BASIC = [ { name: '123555', accountName: 'Rewis Inc', employees: 3100, phone: '837-555-0100', accountOwner: 'http://salesforce.com/fake/url/jane-doe', accountOwnerName: 'Jane Doe', billingCity: 'Phoeniz, AZ', }, { name: '123556', accountName: 'Acme Corporation', employees: 10000, phone: '837-555-0100', accountOwner: 'http://salesforce.com/fake/url/jane-doe', accountOwnerName: 'John Doe', billingCity: 'San Francisco, CA', _children: [ { name: '123556-A', accountName: 'Acme Corporation (Bay Area)', employees: 3000, phone: '837-555-0100', accountOwner: 'http://salesforce.com/fake/url/jane-doe', accountOwnerName: 'John Doe', billingCity: 'New York, NY', _children: [ { name: '123556-A-A', accountName: 'Acme Corporation (Oakland)', employees: 745, phone: '837-555-0100', accountOwner: 'http://salesforce.com/fake/url/jane-doe', accountOwnerName: 'John Doe', billingCity: 'New York, NY', }, { name: '123556-A-B', accountName: 'Acme Corporation (San Francisco)', employees: 578, phone: '837-555-0100', accountOwner: 'http://salesforce.com/fake/url/jane-doe', accountOwnerName: 'Jane Doe', billingCity: 'Los Angeles, CA', }, ], }, { name: '123556-B', accountName: 'Acme Corporation (East)', employees: 430, phone: '837-555-0100', accountOwner: 'http://salesforce.com/fake/url/jane-doe', accountOwnerName: 'John Doe', billingCity: 'San Francisco, CA', _children: [ { name: '123556-B-A', accountName: 'Acme Corporation (NY)', employees: 1210, phone: '837-555-0100', accountOwner: 'http://salesforce.com/fake/url/jane-doe', accountOwnerName: 'Jane Doe', billingCity: 'New York, NY', }, { name: '123556-B-B', accountName: 'Acme Corporation (VA)', employees: 410, phone: '837-555-0100', accountOwner: 'http://salesforce.com/fake/url/jane-doe', accountOwnerName: 'John Doe', billingCity: 'New York, NY', _children: [ { name: '123556-B-B-A', accountName: 'Allied Technologies', employees: 390, phone: '837-555-0100', accountOwner: 'http://salesforce.com/fake/url/jane-doe', accountOwnerName: 'Jane Doe', billingCity: 'Los Angeles, CA', _children: [ { name: '123556-B-B-A-A', accountName: 'Allied Technologies (UV)', employees: 270, phone: '837-555-0100', accountOwner: 'http://salesforce.com/fake/url/jane-doe', accountOwnerName: 'John Doe', billingCity: 'San Francisco, CA', }, ], }, ], }, ], }, ], }, { name: '123557', accountName: 'Rhode Enterprises', employees: 6000, phone: '837-555-0100', accountOwner: 'http://salesforce.com/fake/url/jane-doe', accountOwnerName: 'John Doe', billingCity: 'New York, NY', _children: [ { name: '123557-A', accountName: 'Rhode Enterprises (UCA)', employees: 2540, phone: '837-555-0100', accountOwner: 'http://salesforce.com/fake/url/jane-doe', accountOwnerName: 'John Doe', billingCity: 'New York, NY', }, ], }, { name: '123558', accountName: 'Tech Labs', employees: 1856, phone: '837-555-0100', accountOwner: 'http://salesforce.com/fake/url/jane-doe', accountOwnerName: 'John Doe', billingCity: 'New York, NY', _children: [ { name: '123558-A', accountName: 'Opportunity Resources Inc', employees: 1934, phone: '837-555-0100', accountOwner: 'http://salesforce.com/fake/url/jane-doe', accountOwnerName: 'John Doe', billingCity: 'Los Angeles, CA', }, ], }, ]; export const EXAMPLES_DATA_LAZY_LOADING = [ { name: '123555', accountName: 'Rewis Inc', employees: 3100, phone: '837-555-0100', accountOwner: 'http://salesforce.com/fake/url/jane-doe', accountOwnerName: 'Jane Doe', billingCity: 'Phoeniz, AZ', }, { name: '123556', accountName: 'Acme Corporation', employees: 10000, phone: '837-555-0100', accountOwner: 'http://salesforce.com/fake/url/jane-doe', accountOwnerName: 'John Doe', billingCity: 'San Francisco, CA', _children: [ { name: '123556-A', accountName: 'Acme Corporation (Bay Area)', employees: 3000, phone: '837-555-0100', accountOwner: 'http://salesforce.com/fake/url/jane-doe', accountOwnerName: 'John Doe', billingCity: 'New York, NY', _children: [], }, { name: '123556-B', accountName: 'Acme Corporation (East)', employees: 430, phone: '837-555-0100', accountOwner: 'http://salesforce.com/fake/url/jane-doe', accountOwnerName: 'John Doe', billingCity: 'San Francisco, CA', _children: [ { name: '123556-B-A', accountName: 'Acme Corporation (NY)', employees: 1210, phone: '837-555-0100', accountOwner: 'http://salesforce.com/fake/url/jane-doe', accountOwnerName: 'Jane Doe', billingCity: 'New York, NY', }, { name: '123556-B-B', accountName: 'Acme Corporation (VA)', employees: 410, phone: '837-555-0100', accountOwner: 'http://salesforce.com/fake/url/jane-doe', accountOwnerName: 'John Doe', billingCity: 'New York, NY', _children: [], }, ], }, ], }, { name: '123557', accountName: 'Rhode Enterprises', employees: 6000, phone: '837-555-0100', accountOwner: 'http://salesforce.com/fake/url/jane-doe', accountOwnerName: 'John Doe', billingCity: 'New York, NY', _children: [ { name: '123557-A', accountName: 'Rhode Enterprises (UCA)', employees: 2540, phone: '837-555-0100', accountOwner: 'http://salesforce.com/fake/url/jane-doe', accountOwnerName: 'John Doe', billingCity: 'New York, NY', }, ], }, { name: '123558', accountName: 'Tech Labs', employees: 1856, phone: '837-555-0100', accountOwner: 'http://salesforce.com/fake/url/jane-doe', accountOwnerName: 'John Doe', billingCity: 'New York, NY', _children: [ { name: '123558-A', accountName: 'Opportunity Resources Inc', employees: 1934, phone: '837-555-0100', accountOwner: 'http://salesforce.com/fake/url/jane-doe', accountOwnerName: 'John Doe', billingCity: 'Los Angeles, CA', }, ], }, ]; |
Create Component Style CSS →
Step 4:- Create Lightning Web Component : lwcTreeGrid.css
SFDX:Lightning Web Component >> New >> lwcTreeGrid.css
lwcTreeGrid.css [Style CSS]
1 2 3 4 5 6 7 |
.lgc-bg { background-color: rgb(242, 242, 242); } .lgc-bg-inverse { background-color: rgb(22, 50, 92); } |
Create Lightning Web Component Meta XML →
Step 5:- Create Lightning Web Component : lwcTreeGrid.js-meta.xml
SFDX:Lightning Web Component >> New >> lwcTreeGrid.js-meta.xml
lwcTreeGrid.js-meta.xml [LWC Meta Data XML]
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0" encoding="UTF-8"?> <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata"> <apiVersion>45.0</apiVersion> <isExposed>true</isExposed> <targets> <target>lightning__AppPage</target> <target>lightning__RecordPage</target> <target>lightning__HomePage</target> </targets> </LightningComponentBundle> |
Further post that would you like to learn in Salesforce
If you think you and your family members also like to earn money in additional time, then go this link. In this site, you can earn money for the lifetime at low effort. To Earn Unlimited Everyday With This Trick, Sign Up Free.. Get Started for Free
Flyout is a free, simple way to earn money by placing sponsored content on your blog
Check Out:- Earn Unlimited Everyday With This Trick:- Click Here
What is Lightning Tree grid?
A tree grid displays structured data in a table with expandable rows.
What is not supported in the Lightning Tree grid?
This component inherits styling from trees in the Lightning Design System. Inline editing and sorting of columns are not supported.
How do you make a grid in lightning component?
A) Add a grid container by adding slds-grid to an HTML element. B) Add as many slds-col elements as you want inside of your grid container.
Related Topics | You May Also Like
- Your reaction of the article ▾
Great work with the hard work you have done I appreciate your work thanks for sharing it…
Great work with the hard work you have done I appreciate your work thanks for sharing it…
Wow, amazing block structure! How long
Have you written a blog before? Working on a blog seems easy.
So nice. Hard working post
A dynamic tree grid is a visual representation of information, in which each item in the grid can be expanded or collapsed to reveal more details. It is often used as an organizational tool in presentations, reports, and other documents.
Expanding the cells of a tree grid will reveal more information about the data contained within. This method is often called “drag and drop” because it allows users to move columns and rows by simply dragging them with their mouse pointer.
In this tutorial, we will learn how to create a dynamic tree grid using expande.
– What is expande?
– How does it work?
– What are some use cases for using expande on your website?
– How do you customize settings for your own project?
I really like your blog post, it was quite an amazing and informative one, will recommend it to others and keep
Nice Bro Thanks a lot for sharing.
Thank you for share this article with anyone really this is the helpful
this is the best knowledge you share
very good
very informative thanks