Showing posts with label iterate list in lwc. Show all posts
Showing posts with label iterate list in lwc. Show all posts

Thursday, January 9, 2020

How to iterate over the list in lightning web components

How to iterate over list in lightning web components


This blog post will explain "How to iterate over list in lightning web components".


listdemolwc.html


<template>
    <lightning-card>
        <table class="slds-table slds-table_cell-buffer slds-table_bordered">
            <thead>
              <tr class="slds-line-height_reset">
                <th class="" scope="col">
                  <div class="slds-truncate" title="Key">Account Name</div>
                </th>
                <th class="" scope="col">
                  <div class="slds-truncate" title="Value">Account Description</div>
                </th>
            </tr>
        </thead>
        <tbody>
            <template for:each={accountsdata.data} for:item="item" for:index="index">
                <tr key={item.id}>
                    <th scope="col">
                     {item.Name}
                    </th>
                    <th scope="col">
                      {item.Description}
                    </th>
                </tr>
            </template>
        </tbody>
        </table>
    </lightning-card>
  </template>

listdemolwc.js


/* eslint-disable no-unused-vars */
/* eslint-disable no-alert */
/* eslint-disable no-console */
/* eslint-disable guard-for-in */

/* eslint-disable vars-on-top */

import { LightningElement, track, wire } from 'lwc';

import getDataFromApex from '@salesforce/apex/listapexcontroller.methodName';

export default class listdemolwc extends LightningElement {

@wire(getDataFromApex) accountsdata;

}

listapexcontroller.cls


public with sharing class listapexcontroller {
    @AuraEnabled(cacheable=true)
    public static list<Account> methodName(){

        list<Account> acclist=new list<Account>();
        acclist=[SELECT id,Name,Description from Account];
        return acclist;
    }
    public listapexcontroller() {

    }
}

lightningapp.app


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

Tuesday, December 31, 2019

for:each directive and iterator directive in lightning web component

for:each directive:

for:each directive is used to render an array. To render an array add the for:each directive to a nested template tag, for:item is used to access the current item, for:index is used to access the current index.

.html file

<template>
    <template for:each={students} for:item="item" for:index="index">
        <p key={item.id}>
            {item.Name}
            {item.Branch}
        </p>
    </template>
    
</template>

for:each loop in lightning web component


.js file

import { LightningElement } from 'lwc';

export default class HelloForEach extends LightningElement {
    students = [
        {
            Id: '112',
            Name: 'Farukh',
            Branch: 'CS',
        },
        {
            Id: '113',
            Name: 'Haider',
            Branch: 'CS',
        },
        {
            Id: '114',
            Name: 'Shaikh',
            Branch: 'IT',
        },
    ];
}

for each in lwc

OUTPUT

for:each directive and iterator directive in lightning web component

Iterator directive:


If you have the requirement to access the first and the last element in the list use the iterator directive.

.html file

<template>
    <!--for:each-->
    <b>for:each directive</b>
    <template for:each={students} for:item="item" for:index="index">
        <p key={item.id}>
            {item.Name}
            {item.Branch}
        </p>
    </template>
    <!--Iterator directive-->
    <b>Iterator directive</b>
    <template iterator:it={students}>
<li key={it.value.id}>
{it.value.id}
{it.value.Name}
{it.value.Branch}
</li>
<!--To access the first and the last element use {it.first} and {it.last}-->
    </template>
   
</template>

for:each directive and iterator directive in lightning web component


.js file

import { LightningElement } from 'lwc';

export default class HelloForEach extends LightningElement {
    students = [
        {
            Id: '112',
            Name: 'Farukh',
            Branch: 'CS',
        },
        {
            Id: '113',
            Name: 'Haider',
            Branch: 'CS',
        },
        {
            Id: '114',
            Name: 'Shaikh',
            Branch: 'IT',
        },
    ];

}

for:each directive and iterator directive in lightning web component

OUTPUT

for:each directive and iterator directive in lightning web component