在一个屏幕上显示两个数据集,在Blazor中它们之间有一个匹配列



我有两个表,一个叫NewYearPrices,一个叫做FruitTable。以下是这些表格的型号

public partial class FruitTable
{
public int FruitTableId { get; set; }
public int? Fruit{ get; set; }
public int? SalesCost { get; set; }
public int? Branch{ get; set; }
}
public partial class NewYearPrices
{
public int NewYearPricesId { get; set; }
public int? NyPrice{ get; set; }
public int? Month { get; set; }
public int? Branch{ get; set; }
}

public partial class Branches
{
public int Id { get; set; }
public int? Description{ get; set; }            
}

目前,我有一个视图,它以表格形式显示FruitsTable,还有一个下拉列表,允许用户选择一个分支,列表将被过滤以显示该分支的产品。这是它的代码:

分支选择下拉菜单:

<label for="Branch">Choose a Branch:</label>
<select Name="Branch" id="Branch" @bind="selectedBranchString">
<option value="">---All Branches---</option>
@foreach (var item in branch)
{
<option value="@item.Id">@item.Description</option>
}
</select>

水果桌代码:

<MudTable Dense="true" Virtualize="true" FixedHeader="true" Height="750px" Elevation="25" Items="FilteredFruits" @bind-customer="fruits">
<HeaderContent>
<MudTh>Branch</MudTh>
<MudTh>Fruit</MudTh>
<MudTh>Cost</MudTh>
</HeaderContent>
<RowTemplate>
<MudTd DataLabel="Branch">@context.Branch</MudTd>
<MudTd DataLabel="Fruit">@context.Fruit</MudTd>
<MudTd DataLabel="Cost">@context.Cost</MudTd>
</RowTemplate>   
</MudTable>
@code{
private int? selectedBranch;
private List<FruitTable> fruits=new List <FruitTable>();
private List<FruitTable> FilteredFruits => selectedBranch.HasValue ?
fruits.Where(s => s.Branch==selectedBranch.Value).ToList() :
fruits;
public string selectedBranchString{ get{
if(selectedBranch.HasValue){return selectedBranch.Value.ToString();
}
else{
return null;
}} 
set{selectedBranch = int.Parse(value);}}
private NewYearsPrices nyprices= new NewYearsPrices();
private List<NewYearsPrices> nyprice= new List<NewYearsPrices>();
Branches branches = new Branches();
private List<Branches> branch = new List<Branches>();
private FruitTable fruit= new FruitTable();
protected override async Task OnInitializedAsync()
{
await Task.Delay(0);
GetFruitTable();
GetNewYearsPrices();
GetBranches();
}
private List<FruitTable> GetFruitTable()
{
fruits= fruitService.GetFruitTable();       
return fruits;
}
private List<NewYearsPrices> GetNewYearsPrices()
{
nyprice=fruitService.GetNewYearsPrices();
return nyprice;
}
private List<Branches> GetBranches()
{
branch=fruitService.GetBranches();
return branch;
}
}

目前使用过滤记录

private List<FruitTable> FilteredFruits => selectedBranch.HasValue ?
fruits.Where(s => s.Branch==selectedBranch.Value).ToList() :
fruits;
public string selectedBranchString{ get{
if(selectedBranch.HasValue){return selectedBranch.Value.ToString();
}
else{
return null;
}} 
set{selectedBranch = int.Parse(value);}}

所以我试图在NewYearPrices表中重复这一点,然后我重新分析了这意味着绑定两次到分支下拉列表。。。有人能告诉我该做什么或该朝哪个方向走吗?

您已经将所选分支保存到字段中,因此与FilteredFruits类似,您只需要对NewYearPrices:执行相同操作

private List<NewYearsPrices> nyprice = new List<NewYearsPrices>();
private List<NewYearPrices> FilteredNewYearPrices => !string.IsNullOrEmpty(selectedBranch) ?
nyprice.Where(s => s.Branch == selectedBranch).ToList() :
nyprice;

此外,我想指出的是,不需要selectedBranchString属性。您可以直接绑定到selectedBranch:

<label for="Branch">Choose a Branch:</label>
<select Name="Branch" id="Branch" @bind="selectedBranch">
<option value="">---All Branches---</option>
@foreach (var item in branch)
{
<option value="@item.Id">@item.Description</option>
}
</select>
@code {
private string selectedBranch;
}

相关内容

  • 没有找到相关文章

最新更新