79212966

Date: 2024-11-21 21:25:25
Score: 0.5
Natty:
Report link

I am inferring you want to make a table with Blazor. I will assume that you want to make a table row for each item. The amount of those items is variable. What you need is templated components.

MyTable.razor

<table>
  <thead>
    <tr>
      <th scope="col">#</th>
      <th scope="col">First</th>
      <th scope="col">Last</th>
      <th scope="col">UserName</th>
    </tr>
  </thead>
  <tbody>
     @foreach (var lineItem in LineItems)
     {
         <tr>@LineItemTemplate(lineItem)</tr>
     }
  </tbody>
</table>

MyTable.razor.cs

[Parameter, EditorRequired]
public RenderFragment<TItem> LineItemTemplate { get; set; } = default!;

[Parameter, EditorRequired]
public IReadOnlyList<TItem> LineItems { get; set; } = default!;

Then you can build what you wanted as follows:

<MyTable LineItems="_users" Context="user">
  <LineItemTemplate>
    <MyLine UserWithUserName="user">
  </LineItemTemplate>
</MyTable>

Of course, for completeness sake, MyLine.razor would look something like this:

<tr>
  <th scope="row">@(User.Order)</th>
  <td>@(User.FirstName)</td>
  <td>@(User.LastName)</td>
  <td>@(User.UserName)</td>
</tr>

MyLine.razor.cs

[Parameter, EditorRequired] public UserWithUserName User { get; set; }

Bear in my that this is partially assumption that this is what you wanted. But it seemed that when I was looking for an answer, I found yours. And this is what I needed.

Cheers!

Reasons:
  • Blacklisted phrase (1): Cheers
  • Blacklisted phrase (0.5): I need
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Rich_Rich