1

我想将下面我以前的 html 表转换为 MudTable。但我不知道如何使用 MudBlazor 库实际做到这一点。在 HTML 中,我使用 rowspan 和 colspan 来实现下表。我是使用 Mudblazor 库的新手,但我进行了很多搜索以找到解决方案,但我找不到。希望可以有人帮帮我。

目标表

在此处输入图像描述

我的尝试

<MudTable Items="@selectedDriver.Capability.cActions"
      Class="mt-5"
      Dense="@true" Hover="@true" Bordered="@true" Striped="@true" ReadOnly="@dronly" CanCancelEdit="@true"
      RowEditPreview="BackupActionItem"
      RowEditCancel="ResetActionItemToOriginalValues"
      RowEditCommit="ActionItemHasBeenCommitted"
      CustomHeader="@true"
      @ref="actionTable">
<HeaderContent>
    <MudTHeadRow>
        <MudTh>No.</MudTh>
        <MudTh>Action</MudTh>
        <MudTh colspan="2">Parameters</MudTh>
        <MudTh>Return Type</MudTh>
    </MudTHeadRow>
</HeaderContent>

<RowTemplate>
    @foreach (var item in context.Parameters.Select((value, i) => new { i, value }))
    {
        var index = item.i;
        var parameter = item.value;
        var paramCount = context.Parameters.Count();

        @if (index == 0)
        {
            <MudTd rowspan="@(paramCount)" scope="row" DataLabel="No.">*</MudTd>
            <MudTd rowspan="@(paramCount)" scope="row" DataLabel="Name">@context.Name</MudTd>
        }

        <MudTd DataLabel="Parameter">@parameter.Name</MudTd>
        <MudTd DataLabel="Type">@parameter.Type.ToString()</MudTd>

        @if (index == 0)
        {
            <MudTd rowspan="@(paramCount)" scope="row" DataLabel="Name">@context.ReturnType.ToString()</MudTd>
        }
    }
</RowTemplate>

结果

在此处输入图像描述

4

1 回答 1

2

我建议您在行模板中嵌入另一个<table>。这是一个供您玩耍的小提琴:https ://try.mudblazor.com/snippet/ckGPPbwepSPTlFpg

它看起来像这样: 表中的表

完整示例:

<MudTable Items="@actions"
      Class="mt-5"
      Dense="@true" Hover="@true" Bordered="@true" Striped="@true"  CanCancelEdit="@true"
      CustomHeader="@true"
      >
<HeaderContent>
    <MudTHeadRow>
        <MudTh>No</MudTh>
        <MudTh>Action</MudTh>
        <MudTh>Parameters</MudTh>
        <MudTh>Return Type</MudTh>
    </MudTHeadRow>
</HeaderContent>

<RowTemplate>
    <MudTd DataLabel="No">@context.No</MudTd>
    <MudTd DataLabel="Action">@context.Action</MudTd>
    <MudTd DataLabel="Parameters">
        <table>
            @foreach (var p in context.Params)
            {
               <tr  style="border: 1px solid silver;  border-collapse: collapse;">
                   <td class="pa-1">@p.No</td>
                   <td class="pa-1">@p.Name</td>
                   <td class="pa-1">@p.Type</td>
               </tr>     
            }
        </table>
    </MudTd>
    <MudTd DataLabel="ReturnType">@context.ReturnType</MudTd>
</RowTemplate>

</MudTable>

@code {
    public class MyAction {
        public int No {get; set;}
        public string Action {get; set;}
        public List<Param> Params {get; set;} = new List<Param>();
        public string ReturnType {get; set;} = "void";
    }
    public class Param {
        public int No {get; set;}
        public string Name {get; set;}
        public string Type {get; set;}="string";
    }

    List<MyAction> actions = new List<MyAction>() {
        new MyAction { No = 1, Action="HelloWorld",  },
        new MyAction { No = 2, Action="SomeFunction",  Params=new List<Param> {
                new Param { No=1, Name="param1", },                
                new Param { No=2, Name="param2", },                
                new Param { No=3, Name="param3", },                
                new Param { No=4, Name="param4", },                
            },
        },
    };
}
于 2021-11-24T15:03:56.147 回答