function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Eric-aejeEric-aeje 

lwc how to display count of items in for each from Apex class

Hi I'm looking to find the number of items in a lighting web components for each loop from an apex class?

 

// html
<template for:each={caselist.data} for:item="c" for:index="index">
<div class="ca-table-row cc_table_row" key={c.Id}>
    {c.name}
    </div>
</template>

// JS
</div>
import { LightningElement, api, wire, track } from 'lwc';
import retrieveCases from '@salesforce/apex/Controller.getCaselist';
export default class lightningComp extends LightningElement {
    @wire(retrieveCases)
    caselist; 
    // find how many items are in caselist for each loop
}


 

 

Best Answer chosen by Eric-aeje
Maharajan CMaharajan C
Hi Eric,

Try the below code:

HTML:
<template if:true={caselists}>
	<template for:each={caselists} for:item="c" for:index="index">
		<div class="ca-table-row cc_table_row" key={c.Id}>
			{c.name}
		</div>
	</template>
</template>

JS:
import { LightningElement, api, wire, track } from 'lwc';
import retrieveCases from '@salesforce/apex/Controller.getCaselist';
export default class lightningComp extends LightningElement {
	caselists;
    error;
	
    @wire(retrieveCases)
    caselist({ error, data }) {
        if (data) {
			// find how many items are in caselist for each loop
			console.log(' No of Cases --> ' + data.length);
            this.caselists = data;
            this.error = undefined;
        } else if (error) {
            this.error = error;
            this.caselists = undefined;
        }
    }
	
    
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Eric,

Try the below code:

HTML:
<template if:true={caselists}>
	<template for:each={caselists} for:item="c" for:index="index">
		<div class="ca-table-row cc_table_row" key={c.Id}>
			{c.name}
		</div>
	</template>
</template>

JS:
import { LightningElement, api, wire, track } from 'lwc';
import retrieveCases from '@salesforce/apex/Controller.getCaselist';
export default class lightningComp extends LightningElement {
	caselists;
    error;
	
    @wire(retrieveCases)
    caselist({ error, data }) {
        if (data) {
			// find how many items are in caselist for each loop
			console.log(' No of Cases --> ' + data.length);
            this.caselists = data;
            this.error = undefined;
        } else if (error) {
            this.error = error;
            this.caselists = undefined;
        }
    }
	
    
}

Thanks,
Maharajan.C
This was selected as the best answer
Eric-aejeEric-aeje
Thanks for the help Maharajan!