How to Use Wrapper Class In Lightning WebComponent




Apex Class
==========
public  class ContactController
{
    @AuraEnabled(cacheable=true)
    public  static List<WrapperClass> displayConRecords()
    {
        List<WrapperClass> wrapperConList=new List<WrapperClass>();
        for(Contact con:[select Id,LastName,Email,Phone from Contact LIMIT 10])
        {

            WrapperClass wrap=new WrapperClass();
            wrap.LastName=con.LastName;
            wrap.Email=con.Email;
            wrap.Phone=con.Phone;
            wrapperConList.add(wrap);
        }
        return wrapperConList;
    }
    public class WrapperClass
    {
        @AuraEnabled
        public String LastName;
        @AuraEnabled
        public String Email;
        @AuraEnabled
        public String Phone;

    }
  }

displayContacts.html
====================
<template>
    <table class="slds-table slds-table_cell-buffer slds-table_header-hidden">
        <tr>
            <th>Name</th>
            <th>Email</th>
            <th>Phone</th>
        </tr>
        <template for:each={contacts.data} for:item="items">
            <tr key={items.Id}>
                <td>
                    {items.LastName}
                </td>
                <td>
                    {items.Email}
                </td>
                <td>
                    {items.Phone}
                </td>
         
            </tr>

        </template>
    </table>
 
</template>

displayContacts.js
==================
import { LightningElement,wire } from 'lwc';
import displayConRecords from '@salesforce/apex/ContactController.displayConRecords'

export default class DisplayContacts extends LightningElement {
    @wire(displayConRecords) contacts;

}

LightningApplication
====================
<aura:application extends="force:slds">
    <c:displayContacts/>
</aura:application>



Comments

  1. Quick! Book Your Spot for Wednesday[4th March, 11 AM(IST) | 4 PM(GST) | 11:00 AM(EST)] WEBINAR on topic: How to Perform Bulk Operation in Salesforce using BOFC App?

    You'll learn
    How to Bulk Compare Multiple Profiles and Permission Sets and Export in XLS, Manage Record Types in Bulk, Clone Multiple Custom Fields and Objects.

    ReplyDelete
  2. Effective and lucid explanation
    You can also visit https://maytheforcebewithall.blogspot.com in case you are looking for Q&A series on Salesforce key topics

    ReplyDelete

Post a Comment