我有一個使用MudBlazor庫的 ASP.NET Blazor 服務器專案來創建一個 HTML表。我的問題是編號。在下面的示例代碼中,行的編號是從類屬性中檢索的。但是,在我的班級中,我沒有number屬性,并且在我打算在表格中顯示的所有類中都有一個數字屬性并不好。
由于該表接受專案串列,有沒有辦法獲取正在呈現的專案的索引并使用它而不是@context.Number在 MudBlazor 表中顯示行號?
<MudTable Items="@Elements.Take(4)" Hover="true" Breakpoint="Breakpoint.Sm" Loading="@_loading" LoadingProgressColor="Color.Info">
<HeaderContent>
<MudTh>Nr</MudTh>
<MudTh>Sign</MudTh>
<MudTh>Name</MudTh>
<MudTh>Position</MudTh>
<MudTh>Molar mass</MudTh>
</HeaderContent>
<RowTemplate>
<MudTd DataLabel="Nr">@context.Number</MudTd>
<MudTd DataLabel="Sign">@context.Sign</MudTd>
<MudTd DataLabel="Name">@context.Name</MudTd>
<MudTd DataLabel="Position" HideSmall="_hidePosition">@context.Position</MudTd>
<MudTd DataLabel="Molar mass">@context.Molar</MudTd>
</RowTemplate>
</MudTable>
<MudSwitch @bind-Checked="_hidePosition">Hide <b>position</b> when Breakpoint=Xs</MudSwitch>
<MudSwitch @bind-Checked="_loading">Show Loading</MudSwitch>
此示例代碼可以在MudBlazor 表中找到。
uj5u.com熱心網友回復:
嗯,有點粗糙但最簡單的解決方案是將當前的無數字物件映射到包含所需數字的模型。
創建以下內容:
public class Model<T>
{
public int Number {get; set;}
public T Value {get; set;}
}
然后通過您選擇的某些屬性對原始集合進行排序并迭代,每次創建Model具有原始物件的連續 Number 和 Value 的新物件。
使用這個新模型作為表的顯示資料源,一切都應該沒問題。
<MudTd DataLabel="Nr">@context.Number</MudTd>
<MudTd DataLabel="Sign">@context.Sign</MudTd>
會變成
<MudTd DataLabel="Nr">@context.Number</MudTd>
<MudTd DataLabel="Sign">@context.Value.Sign</MudTd>
// etc...
同樣,它對眼睛來說有點粗糙,但可以解決問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/360504.html
