Creating a tree view using the tree and tree grid components

The tree grid component allows us to create a tree view along with a grid. A sample code snippet is as follows:

<aura:application extends="force:slds">
<aura:handler name="init" value="{!this}" action="{!c.init}" />
<aura:attribute name="gridColumns" type="List" />
<aura:attribute name="gridData" type="Object" />
<aura:attribute name="gridExpandedRows" type="Object" />
<Lightning:treeGrid
columns="{! v.gridColumns }"
data="{! v.gridData }"
expandedRows="{! v.gridExpandedRows }"
keyField="name"
aura:id="mytree"
/>
</aura:application>

The JavaScript controller code for this component generates mock data, shown as follows. You can easily substitute mock data with data from the backend by using Apex code:

({
init: function(cmp) {
var columns = [{
type: 'text',
fieldName: 'accountName',
label: 'Account Name'
},
{
type: 'number',
fieldName: 'employees',
label: 'Employees'
},
{
type: 'phone',
fieldName: 'phone',
label: 'Phone Number'
},
{
type: 'url',
fieldName: 'accountOwner',
label: 'Account Owner',
typeAttributes: {
label: {
fieldName: 'accountOwnerName'
}
}
}
];
cmp.set('v.gridColumns', columns);
var nestedData = [{
"name": "123555",
"accountName": "Rewis Inc",
"employees": 3100,
"phone": "837-555-1212",
"accountOwner": "http://sfdc.co/jane-doe",
"accountOwnerName": "Jane Doe"
},
{
"name": "123556",
"accountName": "Acme Corporation",
"employees": 10000,
"phone": "837-555-1212",
"accountOwner": "http://sfdc.co/john-doe",
"accountOwnerName": "John Doe",
"_children": [{
"name": "123556-A",
"accountName": "Acme Corporation (Bay Area)",
"employees": 3000,
"phone": "837-555-1212",
"accountOwner": "http://sfdc.co/john-doe",
"accountOwnerName": "John Doe",
"_children": [{
"name": "123556-A-A",
"accountName": "Acme Corporation (Oakland)",
"employees": 745,
"phone": "837-555-1212",
"accountOwner": "http://sfdc.co/john-doe",
"accountOwnerName": "John Doe"
},
{
"name": "123556-A-B",
"accountName": "Acme Corporation (San Francisco)",
"employees": 578,
"phone": "837-555-1212",
"accountOwner": "http://sfdc.co/jane-doe",
"accountOwnerName": "Jane Doe"
}
]
}]
},
];
cmp.set('v.gridData', nestedData);
var expandedRows = ["123556"];
cmp.set('v.gridExpandedRows', expandedRows);
}
})

The following screenshot shows the tree grid component:

To learn more about the tree grid component, explore https://developer.Salesforce.com/docs/component-library/bundle/Lightning:treeGrid/example. If you do not need a grid view, there is a tree component illustrated at https://developer.Salesforce.com/docs/component-library/bundle/Lightning:tree/example.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.200.220