I have a LWC that includes a table where I am using a for:each
directive to iterate over my dataset and create the rows for my table, with each row having the item Id as the unique key:
<table>
<template for:each={list} for:item={item}>
<tr key={item.Id}>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
</template>
</table>
The above worked fine.
I am now trying to use a custom component to replace some of the <td>
elements that have special behaviour.
<table>
<template for:each={list} for:item={item}>
<tr key={item.Id}>
<c-my-custom-table-cell></c-my-custom-table-cell>
<c-my-custom-table-cell></c-my-custom-table-cell>
<c-my-custom-table-cell></c-my-custom-table-cell>
</tr>
</template>
</table>
The issue is, when I add my custom element to replace the td
, I get a Missing key for element error message saying that elements inside an iterator need a unique key.
Update
I've tried a simplified version of my custom table cell:
<template>
<div>TEST</div>
</template>
with css of:
:host {
display: table-cell;
}
However, I have already applied the key to the tr
element. Why am I being asked for it again?
Further Update In order to be able to use my custom table cells, I also needed to create a custom component for the table row:
css:
:host {
display: table-row;
}
then I could get everything to work together:
<table>
<template for:each={list} for:item={item}>
<c-custom-row key={item.Id}>
<c-custom-cell value={item.Name}></c-custom-cell>
<c-custom-cell value={item.StartDate__c}></c-custom-cell>
<c-custom-cell value={item.EndDate__c}></c-custom-cell>
</c-custom-row>
</template>
</table>
td
) that I wanted to be custom, and not the table row. I will change the above example so that you can see multiple custom table cells in a row.