简体   繁体   English

Blazor:如何自动对 MudBlazor 表中的行进行编号?

[英]Blazor: How to number the rows in MudBlazor table automatically?

I have an ASP.NET Blazor server project using MudBlazor library to create an HTML table .我有一个 ASP.NET Blazor 服务器项目,使用MudBlazor库创建一个 HTML My issue is with the numbering.我的问题是编号。 In the example code below, the numbering of the rows is retrieved from the class property.在下面的示例代码中,行的编号是从 class 属性中检索的。 However, in my class I don't have a number property and it's not nice to have a number property in all classes that I intend to display in tables.但是,在我的 class 中,我没有number属性,而且在我打算在表中显示的所有类中都有数字属性并不好。

Since the table accepts a list of items, is there a way of getting the index of the item being rendered and use it instead of @context.Number to display the row number in the MudBlazor table?由于该表接受项目列表,是否有办法获取正在呈现的项目的索引并使用它而不是@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>

This example code can be found in MudBlazor Table .可以在MudBlazor Table中找到此示例代码。

With Linq's TakeWhile you can count elements until you find element that match element of row you're currently in:使用 Linq 的 TakeWhile,您可以计算元素,直到找到与您当前所在行的元素匹配的元素:

<MudTable Items="@MyElements">
    <HeaderContent>
        <MudTh>Row</MudTh>
    </HeaderContent>
    <RowTemplate>
        <MudTd>@GetRowNumber(context)</MudTd>
    </RowTemplate>
</MudTable>
@code{
    public IEnumerable<object> MyElements => Elements.Take(4);
    public int? GetRowNumber(object element) =>
        MyElements?.TakeWhile(x => x != element).Count();
}

Well, a bit crude but simplest solution would be to map current number-less object to a model that contains needed number.好吧,有点粗略但最简单的解决方案是将 map 当前无编号 object 转换为包含所需编号的 model。
Create following:创建以下内容:

public class Model<T>
{
    public int Number {get; set;}
    public T Value {get; set;}
}

Then order your original collection by some property of your choosing and iterate through, each time creating new Model object with consecutive Number and Value of the original object.然后通过您选择的某些属性订购您的原始集合并迭代,每次创建新Model object 与原始 object 的连续编号和值。
Use this new model as display data source for the table and all should go just fine.使用这个新的 model 作为表格的显示数据源,所有应该 go 就好了。

<MudTd DataLabel="Nr">@context.Number</MudTd>
<MudTd DataLabel="Sign">@context.Sign</MudTd>

will become会变成

<MudTd DataLabel="Nr">@context.Number</MudTd>
<MudTd DataLabel="Sign">@context.Value.Sign</MudTd>
// etc...

Again, it's a bit crude on the eyes, but will do the trick.同样,它对眼睛有点粗糙,但会奏效。

Let's use a counter variable and increment it for each row template An example that I'm using:让我们使用一个计数器变量并为每个行模板递增一个我正在使用的示例:

<RowTemplate>
   <MudTd DataLabel="#">@(rowIndex++)</MudTd>
   ...
 </RowTemplate>

@code{
   private int rowIndex = 1;
}

It is better to use index of list as it is much simpler to implement to show the row numbers in a table.最好使用列表索引,因为在表中显示行号更容易实现。

@(Elements.IndexOf(context)+1)

<MudTable Items="@Elements" 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">@(Elements.IndexOf(context)+1)</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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM