I have a standard Blazor app where I dynamically create a table and want to be able to identify each table entry by a unique identifier.
Currently I have each <td>
element be assigned the counter value in my for
loop like so:
for (var i=0; i < 7; i++)
{
<td id=@i @onlick="() => SelectEntry()">@i</td>
}
I want to be able to pass the id value to the onclick
method so I know which table entry was clicked on but am unsure how.
2
Answers
It is important that you assign your value from
i
to a new variable, otherwise you would always pass the last value of i into the function.What you can do would be this:
You current method will not produce unique ids: you assign the same
id
to cells in each row.Here’s some code that will make each cell unique. It uses a guid to identify the table and then
Rx:Cx
to identify the cell.