<TelerikListView Data=@ListViewData2
Pageable="true"
PageSize="5">
<HeaderTemplate>
Current Scheduled Deliveries (@DateTime.Now.ToString("M/d/yyyy"))
</HeaderTemplate>
<Template>
<div class="k-card k-card-horizontal">
<div class="k-vbox k-flex-grow">
<div class="k-card-body">
<h4 class="k-card-title">@context.Title</h4>
<h5 class="k-card-subtitle">@context.Description</h5>
@*<div class="card-date">@context.Date.ToString("MMM dd yyyy")</div>*@
</div>
<br />
<div class="k-card-actions k-card-actions-horizontal k-card-actions-start">
<ListViewCommandButton OnClick="@StartDelivery()">Start Delivery</ListViewCommandButton>
</div>
</div>
@*<img class="k-card-image" src="images/articles/@context.ImageUrl" alt="@context.Subtitle" @*/>*@
</div>
</Template>
</TelerikListView>
@code {
public static DateTime Now { get; }
List<ListData> ListViewData2 = new List<ListData>()
{
new ListData
{
Title = "Delivery: Alpha High School",
Description = "3097 Sandy Ct, Atlanta, GA",
},
new ListData
{
Title = "Delivery: Roosevelt Elementary School",
Description = "555 Teddy St, Conyers, GA",
},
new ListData
{
Title = "Delivery: St John Middle School",
Description = "203 Holy Dr, Morrow, GA",
}
};
async Task StartDelivery()
{
var addy = new ListData();
addy.Description = "3097 Sandy Ct, Atlanta, GA";
var current = "Current Location";
var url = string.Format("http://maps.google.com/maps?f=d&source=s_d&saddr={0}&daddr={1}", current, addy.Description);
//NavManager.NavigateTo(url);
await JSRuntime.InvokeAsync<object>("open", url, "_blank");
//return addy;
}
public class ListData
{
public string Title { get; set; } = null!;
public string Description { get; set; } = null!;
}
}
在浏览器上,列表ListViewData2显示为三个不同标题的一行,其中包含三个不同的描述(地址(和三个不同按钮,用于在谷歌地图中启动方向。
浏览器列表的图片(第一个项目被排除在外,因为我在那里使用了真实的地址,不想在代码中更改它(
在StartDelivery((函数中,我必须手动将列表中的地址分配给addy。我的谷歌地图url的描述,但有没有一种方法可以喜欢引用描述,这样它就会自动知道列表中三个的正确地址?例如,如果我不在地址中硬编码,addy。描述保持空白,并且没有从列表中读取每个学校的地址。如果我这样做了,所有开始发送按钮都将链接到同一地址。:(
我可能措辞很糟糕,但这个问题似乎有一个非常简单的答案,我似乎无法理解。此外,我对C#和blazor以及校外编码还比较陌生。
正如upvoter所说的@keithwill,您可以将ListData
参数传递给StartDelivery
方法。
async Task StartDelivery(ListData addy)
{
var current = "Current Location";
var url = string.Format("http://maps.google.com/maps?f=d&source=s_d&saddr={0}&daddr={1}", current, addy.Description);
//NavManager.NavigateTo(url);
await JSRuntime.InvokeAsync<object>("open", url, "_blank");
//return addy;
}
然后像这样绑定按钮点击事件:
@foreach (var data in ListViewData2)
{
@* omitted code *@
<button @onclick="() => StartDelivery(data)">Start Delivery</button>
@* omitted code *@
}