muddatagrid – 创建超链接或按钮

huangapple go评论88阅读模式
英文:

muddatagrid - make an hyperlink or a button

问题

如何在元素名称上创建超链接或按钮?

@using System.Net.Http.Json
@using MudBlazor.Examples.Data.Models
@inject HttpClient httpClient

<!-- This is the MudDataGrid component -->
<MudDataGrid Items="@Elements.Take(4)">
    <Columns>
        <Column T="Element" Field="Number" Title="Nr" />
        <Column T="Element" Field="Sign" />
        <Column T="Element" Field="Name">
            <!-- You can create a hyperlink or button here -->
            <!-- Example: Creating a hyperlink -->
            <Template Context="element">
                <a href="#">@element.Name</a>
            </Template>
        </Column>
        <Column T="Element" Field="Position" />
        <Column T="Element" Field="Molar" Title="Molar mass" />
    </Columns>
</MudDataGrid>

@code {
    private IEnumerable<Element> Elements = new List<Element>();

    protected override async Task OnInitializedAsync()
    {
        Elements = await httpClient.GetFromJsonAsync<List<Element>>("webapi/periodictable");
    }
}

在上面的代码中,我在"Name"列中创建了一个超链接示例。您可以在模板中使用<a>标签来创建超链接,将其链接到您想要的目标页面。

英文:

This is the following link to try out the code

@using System.Net.Http.Json
@using MudBlazor.Examples.Data.Models
@inject HttpClient httpClient

&lt;MudDataGrid Items=&quot;@Elements.Take(4)&quot;&gt;
    &lt;Columns&gt;
        &lt;Column T=&quot;Element&quot; Field=&quot;Number&quot; Title=&quot;Nr&quot; /&gt;
        &lt;Column T=&quot;Element&quot; Field=&quot;Sign&quot; /&gt;
        &lt;Column T=&quot;Element&quot; Field=&quot;Name&quot; /&gt;
        &lt;Column T=&quot;Element&quot; Field=&quot;Position&quot; /&gt;
        &lt;Column T=&quot;Element&quot; Field=&quot;Molar&quot; Title=&quot;Molar mass&quot; /&gt;
    &lt;/Columns&gt;
&lt;/MudDataGrid&gt;

@code { 
    private IEnumerable&lt;Element&gt; Elements = new List&lt;Element&gt;();

    protected override async Task OnInitializedAsync()
    {
        Elements = await httpClient.GetFromJsonAsync&lt;List&lt;Element&gt;&gt;(&quot;webapi/periodictable&quot;);
    }
}
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace MudBlazor.Examples.Data.Models
{
    public class Element
    {
        public string Group { get; set; }
        public int Position { get; set; }
        public string Name { get; set; }
        public int Number { get; set; }

        [JsonPropertyName(&quot;small&quot;)]
        public string Sign { get; set; }
        public double Molar { get; set; }
        public IList&lt;int&gt; Electrons { get; set; }

        public override string ToString()
        {
            return $&quot;{Sign} - {Name}&quot;;
        }
    }
}

How to make an hyperlink or a button over the element Name?

答案1

得分: 3

以下是翻译好的代码部分:

@using System.Net.Http.Json
@using MudBlazor.Examples.Data.Models
@inject HttpClient httpClient

<MudDataGrid Items="@Elements.Take(4)">
    <Columns>
        <Column T="Element" Field="Number" Title="Nr" />
        <Column T="Element" Field="Sign" />
        <Column T="Element" Field="Name">
            <CellTemplate>
                <MudButton Size="@Size.Small" Variant="@Variant.Filled" Color="@Color.Primary" OnClick="@(context => this.Click(context.Item))">
                    @context.Item.Name
                </MudButton>
            </CellTemplate>
        </Column>
        <Column T="Element" Field="Position" />
        <Column T="Element" Field="Molar" Title="Molar mass" />
    </Columns>
</MudDataGrid>

@code { 
    private IEnumerable<Element> Elements = new List<Element>();

    protected override async Task OnInitializedAsync()
    {
        Elements = await httpClient.GetFromJsonAsync<List<Element>>("webapi/periodictable");
    }

    private void Click(Element element)
    {
        Console.WriteLine($"Clicked: {element.Name}");
    }
}
英文:

Something like this should do:

@using System.Net.Http.Json
@using MudBlazor.Examples.Data.Models
@inject HttpClient httpClient

&lt;MudDataGrid Items=&quot;@Elements.Take(4)&quot;&gt;
    &lt;Columns&gt;
        &lt;Column T=&quot;Element&quot; Field=&quot;Number&quot; Title=&quot;Nr&quot; /&gt;
        &lt;Column T=&quot;Element&quot; Field=&quot;Sign&quot; /&gt;
        &lt;Column T=&quot;Element&quot; Field=&quot;Name&quot;&gt;
            &lt;CellTemplate&gt;
                &lt;MudButton Size=&quot;@Size.Small&quot; Variant=&quot;@Variant.Filled&quot; Color=&quot;@Color.Primary&quot; OnClick=&quot;@(() =&gt; this.Click(context.Item))&quot;&gt;
                    @context.Item.Name
                &lt;/MudButton&gt;
            &lt;/CellTemplate&gt;
        &lt;/Column&gt;
        &lt;Column T=&quot;Element&quot; Field=&quot;Position&quot; /&gt;
        &lt;Column T=&quot;Element&quot; Field=&quot;Molar&quot; Title=&quot;Molar mass&quot; /&gt;
    &lt;/Columns&gt;
&lt;/MudDataGrid&gt;

@code { 
    private IEnumerable&lt;Element&gt; Elements = new List&lt;Element&gt;();

    protected override async Task OnInitializedAsync()
    {
        Elements = await httpClient.GetFromJsonAsync&lt;List&lt;Element&gt;&gt;(&quot;webapi/periodictable&quot;);
    }

    private void Click(Element element)
    {
        Console.WriteLine($&quot;Clicked: {element.Name}&quot;);
    }
}

huangapple
  • 本文由 发表于 2023年1月9日 02:13:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050247.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定