Using an iterator in the template in Lightning Web Component
Let's look at how to use an iterator and loop over all the records that we get back from Apex method in Lightning Web Components.
When we get data from objects using Apex we need to follow a series of steps to display them back to the users.
We need to check if the data is not null and then we need to loop them through an iterator to go over all the records one after other and for that, we would need a tag which is <template for:each={} for:item=""></template>
Let's look at some sample code!
This is a simple Apex Class that sends data by querying it.
This here is the controller file in the Lightning Web Component since in which we are trying to pipe the output of the Apex method to a property.
This is the template file and we are checking that the data is not null
and then we are passing records.data
to <template for:each={} for:item=""></template>
tag.
We need to make sure every row should be associated with an attribute called key and the value that we provide to it should be unique. That's how Lightning gets to a record if it gets modified and updates it with the current value.
Hope this is helpful!