Showing posts with label Wrapper Class. Show all posts
Showing posts with label Wrapper Class. Show all posts

Monday, January 20, 2020

Using Wrapper Class in Lighting Web Components(lwc)

What is a Wrapper Class?
A Wrapper class is a class whose instances are a collection of other objects.
OR
A wrapper or container class is a class, a data structure, or an abstract data type which contains different objects or collection of objects as its members.

This post explains how to use wrapper class properties in Lighting Web Components
in this demo, I am displaying Account and related Contacts using Wrapper Class.


HTML Code
<template>
    <lightning-card title="Wrapper Class in Lightning Web Components">

        <template if:true={data}>
                <table class="slds-table slds-table_bordered slds-table_cell-buffer slds-table_col-bordered">
                    <thead>
                        <tr class="slds-text-title_caps">
                            <th scope="col">
                                <div title="Key">Account Name</div>
                            </th>
                            <th scope="col">
                                <div title="Value">Account Related Contacts</div>
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <template for:each={data} for:item="keyValue">
                            <tr key={keyValue.Id}>
                                <th scope="col">
                                    <div>{keyValue.objAcc.Name}</div>
                                </th>
                                <th scope="col">
                                    <template for:each={keyValue.lstCons} for:item="con">
                                        <div key={con.Id}>{con.Name}</div>
                                    </template>
                                </th>
                            </tr>
                        </template>
                    </tbody>
                </table>
        </template>
</template>

JS Code
import { LightningElement, wire } from 'lwc';
import AccAndCons from '@salesforce/apex/LWCExampleController.fetchAccAndCons';

export default class WrapperClassInLWC extends LightningElement {
    data;
    error;


    @wire(AccAndCons)
    accs({error, data}) {
        if(data) {
            this.data = data;
            window.console.log('data ==> '+JSON.stringify(data));
        }
        else if(error) {
            this.error = error;
        }
    }
}

Apex Class
public inherited sharing class LWCExampleController {

    @AuraEnabled(Cacheable = true)
    public static List<WrapperDemo> fetchAccAndCons() {
        List<WrapperDemo> lstWrapper = new List<WrapperDemo>();

        for(Account acIterator : [ SELECT Id, Name, (Select Id, Name From Contacts) FROM Account WHERE Id = '001B000000vZWOHIA4'] ) {
            lstWrapper.add(new WrapperDemo(acIterator, acIterator.Contacts));
        }

        return lstWrapper;
        
    }
 
 // Wrapper Class
 public class WrapperDemo {
        @AuraEnabled public Account objAcc;
        @AuraEnabled public list<Contact> lstCons;

        public WrapperDemo(Account acc, list<Contact> lstCons) {
            this.objAcc = acc;
            this.lstCons = lstCons;
        }
    }
}

Output

Sunday, August 26, 2018

Dynamically Add Row / Remove Row in PageBlock Table in Visualforce Page

In this Post, Explore More about the How To Add Row or Remove Row in PageBlockTable in Visualforce Page

To achieve this Functionality I Created The One Simple Apex Controller and Visualforce Page.

Apex Class
public class AddOrRemoveRowController {
    private Integer counter = 0;
    public list<WrapperData> lstWrapperData {get;set;}
    
    public AddOrRemoveRowController() {
        lstWrapperData =  new list<WrapperData>();
        addRow();
    }
    
    // Adding Rows 
    public PageReference addRow() {
        counter++;
        lstWrapperData.add(new WrapperData(new Account(), counter));
        return null;
    }
    
    // Removing The Rows
    public PageReference removeRow() {
        Integer iRemoveRow = Integer.valueOf(Apexpages.currentpage().getParameters().get('index'));
        for(Integer i=0; i<lstWrapperData.size(); i++) {
            if(lstWrapperData[i].iIndex == iRemoveRow ) {
                lstWrapperData.remove(i);     
            }
        }
        return null;
    }
    
    // Wrapper Class
    public class WrapperData {
        public Account objAcc {get;set;}
        public Integer iIndex {get;set;}
        
        public WrapperData(Account objAcc, Integer iRow) {
            this.objAcc = objAcc;
            this.iIndex = iRow;
        }
    }
}
Visualforce Page
<apex:page controller="AddOrRemoveRowController" tabStyle="Account">
    <apex:form >
        <apex:pageBlock >
            <apex:outputPanel id="showpageBlockData" >
                <apex:pageBlockSection columns="1" title="Dynamically Add or Remove Row" collapsible="false">
                    <apex:pageBlockTable value="{!lstWrapperData}" var="wd" id="pgb" >
                        <apex:column headerValue="Account Name">
                            <apex:inputField value="{!wd.objAcc.Name}" />
                        </apex:column>
                        <apex:column headerValue="Industry">
                            <apex:inputField value="{!wd.objAcc.Industry}" />
                        </apex:column>
                        <apex:column headerValue="Phone">
                            <apex:inputField value="{!wd.objAcc.Phone}" />
                        </apex:column>
                        <apex:column headerValue="Account Type">
                            <apex:inputField value="{!wd.objAcc.Type}" />
                        </apex:column>
                        <apex:column > 
                            <apex:commandLink value="Add Row" action="{!addRow}" reRender="pgb" style="color:#61afe8;" />&nbsp;&nbsp;||&nbsp;&nbsp;
                            <apex:commandLink value="Remove Row" action="{!removeRow}" reRender="pgb" style="color:red;" >
                                <apex:param value="{!wd.iIndex}" name="index" />
                            </apex:commandLink>
                        </apex:column>
                    </apex:pageBlockTable>
                </apex:pageBlockSection>           
            </apex:outputPanel> 
        </apex:pageBlock>
    </apex:form>
</apex:page>

Happy Learning!!!