Created
December 2, 2021 21:25
-
-
Save pawelel/e70a387fb555d79dc2d3f98c0cb27821 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Components; | |
using Microsoft.EntityFrameworkCore; | |
using Microsoft.Extensions.Localization; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
public partial class LocationTable : ComponentBase | |
{ | |
[Inject] private DataManager<Area, ApplicationDbContext> AreaManager { get; set; } | |
[Inject] private DataManager<Place, ApplicationDbContext> PlaceManager { get; set; } | |
[Inject] DataManager<Coordinate, ApplicationDbContext> CoordinateManager { get; set; } | |
[Inject] private IStringLocalizer<LanguageService> L { get; set; } | |
private List<Area> areas = new(); | |
private List<Place> places = new(); | |
private List<Coordinate> coordinates = new(); | |
private List<AreaPlaceCoordinate> locations = new(); | |
protected override async Task OnInitializedAsync() | |
{ | |
places = await PlaceManager.Get(include: src => src.Include(a => a.Area)); | |
coordinates = await CoordinateManager.Get(include: src => src.Include(a => a.Area).Include(a => a.Place)); | |
areas = await AreaManager.Get(); | |
locations = (from a in areas join p in places on a.Id equals p.AreaId join c in coordinates on p.Id equals c.PlaceId select new AreaPlaceCoordinate { AreaId = a.Id, AreaName = a.Name, PlaceId = p.Id, PlaceName = p.Name, CoordinateId = c.Id, CoordinateName = c.Name }).ToList(); | |
} | |
private AreaPlaceCoordinate selectedItem1 = null; | |
private IEnumerable<string> PlaceHash { get; set; } = new HashSet<string>() { }; | |
private IEnumerable<string> AreaHash { get; set; } = new HashSet<string>() { }; | |
private IEnumerable<string> CoordinateHash { get; set; } = new HashSet<string>() { }; | |
List<Place> GetPlaces() | |
{ | |
if (!AreaHash.Any()) | |
{ | |
return places; | |
} | |
return places.Where(p => AreaHash.Any(a => p.Area.Name == a)).ToList(); | |
} | |
List<Coordinate> GetCoordinates() | |
{ | |
if (!AreaHash.Any() && !PlaceHash.Any()) | |
{ | |
return coordinates; | |
} | |
if (AreaHash.Any() && !PlaceHash.Any()) | |
{ | |
return coordinates.Where(c => AreaHash.Any(p => c.Area.Name == p)).ToList(); | |
} | |
return coordinates.Where(c => PlaceHash.Any(p => c.Place.Name == p)).ToList(); | |
} | |
private string searchString1 = ""; | |
static bool FilterHash<T>(IEnumerable<string> list, T viewModel, Func<string, bool> p) | |
{ | |
if (!list.Any()) | |
{ | |
return true; | |
} | |
return list.Any(p); | |
} | |
public bool TestFilter(AreaPlaceCoordinate apc) | |
{ | |
bool textEmpty = (string.IsNullOrWhiteSpace(searchString1)); | |
List<string> names = new() { apc.AreaName, apc.PlaceName, apc.CoordinateName }; | |
bool filterText = textEmpty || names.Any(n => n.Contains(searchString1, StringComparison.OrdinalIgnoreCase)); | |
bool areaSelector(string str) => apc.AreaName.Contains(str); | |
bool coordinateSelector(string str) => apc.CoordinateName.Contains(str); | |
bool placeSelector(string str) => apc.PlaceName.Contains(str); | |
return FilterHash(AreaHash, apc, areaSelector) && FilterHash(CoordinateHash, apc, coordinateSelector) && FilterHash(PlaceHash, apc, placeSelector) && filterText; | |
} | |
} | |
<MudTable Items="@locations" T="AreaPlaceCoordinate" Dense="true" @bind-SelectedItem="selectedItem1" Filter="new Func<AreaPlaceCoordinate,bool>(TestFilter)"> | |
<ToolBarContent> | |
<MudItem xs="12" md="5"> | |
<MudTextField @bind-Value="searchString1" Placeholder="@L["Find Area"]" Adornment="Adornment.Start" AdornmentIcon="@Icons.Material.Filled.Search" IconSize="Size.Medium" Class="mt-0"></MudTextField> | |
</MudItem> | |
</ToolBarContent> | |
<HeaderContent> | |
<MudTh><b>@L["Area Name"]</b> | |
<MudSelect Clearable="true" T="string" @bind-SelectedValues="AreaHash" MultiSelection="true" Label="@L["Select Areas"]"> | |
@foreach (var area in areas) | |
{ | |
<MudSelectItem T="string" [email protected] /> | |
} | |
</MudSelect> | |
</MudTh> | |
<MudTh><b>@L["Place Name"]</b> | |
<MudSelect Clearable="true" T="string" @bind-SelectedValues="PlaceHash" MultiSelection="true" Label="@L["Select Places"]"> | |
@foreach (var place in GetPlaces()) | |
{ | |
<MudSelectItem T="string" [email protected] /> | |
} | |
</MudSelect> | |
</MudTh> | |
<MudTh><b>@L["Coordinate Name"]</b> | |
<MudSelect Clearable="true" T="string" @bind-SelectedValues="CoordinateHash" MultiSelection="true" Label="@L["Select Coordinates"]"> | |
@foreach (var place in GetCoordinates()) | |
{ | |
<MudSelectItem T="string" [email protected] /> | |
} | |
</MudSelect> | |
</MudTh> | |
</HeaderContent> | |
<RowTemplate> | |
<MudTd DataLabel=@L["Area Name"]>@context.AreaName</MudTd> | |
<MudTd DataLabel=@L["Place Name"]>@context.PlaceName</MudTd> | |
<MudTd DataLabel=@L["Coordinate Name"]>@context.CoordinateName</MudTd> | |
</RowTemplate> | |
</MudTable> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment