Create dynamic tree grid with expande / collapse selected rows and select checkbox for the entire row select/deselect in Salesforce lightning web component LWC #61
Step 1:- Create Lightning Web Component : lwcTreeGrid.html
`
Tree Grid:- Displays a hierarchical view of data in a table in Lightning Web Component (LWC)
`
Step 2:- Create Lightning Web Component : lwcTreeGrid.js
` 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();
}
}`
Step 3:- Create Lightning Web Component : sampleData.js
Step 5:- Create Lightning Web Component : lwcTreeGrid.js-meta.xml
` <?xml version="1.0" encoding="UTF-8"?>
45.0truelightning__AppPagelightning__RecordPagelightning__HomePage`
**[→ Get source code live demo link:-](https://www.w3web.net/tree-grid-dynamic-expand-collapse-in-lwc/)**
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.
→ Get source code live demo link:-
Step 1:- Create Lightning Web Component : lwcTreeGrid.html
`
`
Step 2:- Create Lightning Web Component : lwcTreeGrid.js
` import { LightningElement, track } from 'lwc'; import { EXAMPLES_COLUMNS_DEFINITION_BASIC, EXAMPLES_DATA_BASIC, } from './sampleData';
export default class LwcTreeGrid extends LightningElement { @track currentExpandedRows;
}`
Step 3:- Create Lightning Web Component : sampleData.js
` 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', },
];
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', },
];`
Step 4:- Create Lightning Web Component : lwcTreeGrid.css
` .lgc-bg { background-color: rgb(242, 242, 242); }
.lgc-bg-inverse { background-color: rgb(22, 50, 92); }`
Step 5:- Create Lightning Web Component : lwcTreeGrid.js-meta.xml
` <?xml version="1.0" encoding="UTF-8"?>