Showing posts with label Parent Component. Show all posts
Showing posts with label Parent Component. Show all posts

Thursday, 10 August 2023

LWC:Spread - Simplifying Data Passing Between Parent and Child LWC Components


In Lightning Web Components (LWC), one of the most crucial aspects is data communication between parent and child components. Passing data from the parent component to the child component is a common requirement in many scenarios. While there are multiple ways to achieve this, one approach that simplifies the process is using the "spread" operator. In this blog, we'll explore how to use the spread operator to pass data from a parent LWC component to a child LWC component. 

Why Use LWC:Spread? 

With the introduction of lwc:spread, you gain the ability to consolidate multiple properties into an object, allowing you to effortlessly pass this object to your child component. 

The greatest advantage of lwc:spread is that it promotes the sharing of properties within the object, rather than passing the entire object as a single attribute. This not only enhances performance but also facilitates a more organized and concise approach to managing component data. 


ParentLWC - The Parent Component 

The ParentLWC component template contains a lightning-card with a title and a lightning-button. We have defined a tracked property "arrayVal" to hold the data that we want to pass to the child component.


Parent JS 


Child Component 


Child JS


What if the “arrayVal” already contains some crucial Information? 

Below is the approach we can follow for that: 
Understanding the “SPREAD” Concept 

The "lwc:spread" directive in the parent component helps to pass the entire "arrayVal" object to the child component. By using the spread operator, the child component will receive the data directly as properties, and you do not need to explicitly pass each property individually. 

Output:


 

Conclusion 

In this blog, we explored how to use the "spread" operator to simplify data passing between parent and child LWC components. We saw how the parent component, "ParentLWC," passes data to the child component, "SpreadChild," by utilizing the "lwc:spread" directive. By using the spread operator, the child component can easily access the data as properties without the need for complex data passing mechanisms. 


This approach can enhance the maintainability and readability of your LWC code and make the communication between components more seamless. By applying this technique, you can improve your LWC development and create more efficient and modular Lightning Web Components. Happy coding! 


If you have any questions you can reach out our Salesforce Consulting team here.

Thursday, 18 March 2021

Display Picklist values dynamically in lightning component


Requirement:
While working on one of the user stories of Engineering sector project for a client based out of Tampa, FL, USA;  there was a requirement to provide an interface that would display selected Industry (Pick-list field) values for Accounts on the fly while performing Mass Edit.

Challenge: 

Mass Edit feature was implemented using the Custom Lightning component. And, t
here is no out-of-the-box (OOTB) feature available to display selected pick-list values in the in lightning component, while bulk edit is being performed.

Solution:
To overcome this limitation, we have created an Apex class that contains the mechanism to retrieve the list of Industry (pick-list) field values from the Account object.

This Apex class is then used by Custom Lightning Component (BulkEdit.Cmp) which displays the list of Accounts. Using component.get ("v.pickvalues") in DynamicPickController component, we are retrieving selected pick-list values from the Parent Component (BulkEdit.cmp). 

Then Industry pick-list values are being compared with the values that have been retrieved from Parent Component. If field values are matched, it will set the selected Industry field value.

If values are not matched, then it will set the first value from the pick list options.

Once comparison is done,  value is set to the attribute IndustryPick that we have defined in the Custom DynamicPickController.Cmp component.

BulkEdit.cmp
<aura:iteration items="{!v.selectedlst}" var="ac" >
                                    <c:DynamicPickController selectedOptions = '{!v.selectedlst}'
                                                  Name = '{!ac.lstTelLineAccLocUserobject2.Name}'
                                                  Industry = '{!ac.lstTelLineAccLocUserobject2.Industry}'
                                                  pickvalues = '{!v.pickvalues}'
                                                  />
</aura:iteration>

DynamicPickController.Js
doInit : function(component, event, helper) {
        var IndustryPick = [];
        for(var i = 0 ; i < component.get("v.pickvalues").length ; i++)
        {
            if(component.get("v.pickvalues")[i] == component.get("v.Industry"))  
            {
                IndustryPick.push({"label":component.get("v.pickvalues")[i], "value":component.get("v.pickvalues")[i], "selected":true})
            }
            else
            {
                IndustryPick.push({"label":component.get("v.pickvalues")[i], "value":component.get("v.pickvalues")[i]})
            }
        }
        component.set("v.IndustryPick", IndustryPick);
    }

DynamicPickController.Cmp
<aura:attribute name="pickvalues" type="list"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<lightning:select name="Industry" label="Select a Industry:" aura:id="Industry" value="{!v.Industry}">
                <aura:iteration items="{!v.IndustryPick}" var="option">
                    <option text="{!option.label}" value="{!option.label}" selected="{!option.selected}"/>
                </aura:iteration>
</lightning:select>

Output:

If you have any questions you can reach out our Salesforce Consulting team here.