Last active
November 11, 2021 16:42
-
-
Save pawelel/a49b955fb67323295df67487b5d80bf5 to your computer and use it in GitHub Desktop.
How to get multiselect
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
<MudTable Items="@(GetPlaces())" T="AreaPlace" Dense="true" @bind-SelectedItem="selectedItem1" Filter="new Func<AreaPlace,bool>(FilterFunc1)"> | |
<ToolBarContent> | |
<MudItem xs="12" md="5"> | |
<MudTextField @bind-Value="searchString1" Placeholder="@L["Search"]" Adornment="Adornment.Start" AdornmentIcon="@Icons.Material.Filled.Search" IconSize="Size.Medium" Class="mt-0"></MudTextField> | |
</MudItem> | |
<MudSpacer/> | |
<MudItem xs="12" md="4"> | |
<MudSelect T="string" @bind-SelectedValues="Options" @bind-Value="@tempArea" MultiSelection="true" Label="@L["Select Areas"]"> | |
@foreach (var area in areas) | |
{ | |
<MudSelectItem T="string" [email protected]/> | |
} | |
</MudSelect> | |
</MudItem> | |
</ToolBarContent> | |
<HeaderContent> | |
<MudTh><b>@L["Area Name"]</b></MudTh> | |
<MudTh><b>@L["Place Name"]</b></MudTh> | |
</HeaderContent> | |
<RowTemplate> | |
<MudTd DataLabel=@L["Area Name"]>@context.AreaName</MudTd> | |
<MudTd DataLabel=@L["Place Name"]>@context.PlaceName</MudTd> | |
</RowTemplate> | |
</MudTable> | |
public partial class AreaPlaceTable : ComponentBase | |
{ | |
[Inject] | |
DataManager<Area, ApplicationDbContext> AreaManager { get; set; } | |
[Inject] | |
private IStringLocalizer<LanguageService> L { get; set; } | |
List<Area> areas=new(); | |
string tempArea; | |
List<AreaPlace> locations=new(); | |
protected override async Task OnInitializedAsync() | |
{ | |
areas = await AreaManager.Get(); | |
var data = await AreaManager.Get(includeProperties: "Places"); | |
locations = (from a in data from p in a.Places select new AreaPlace { AreaId=a.Id, AreaName=a.Name, PlaceId=p.Id, PlaceName=p.Name }).ToList(); | |
} | |
private bool FilterFunc1(AreaPlace detail) => FilterFunc(detail, searchString1); | |
private string searchString1 = ""; | |
private AreaPlace selectedItem1 = null; | |
private static bool FilterFunc(AreaPlace detail, string searchString) | |
{ | |
if (string.IsNullOrWhiteSpace(searchString)) | |
return true; | |
if (detail.AreaName.Contains(searchString, StringComparison.OrdinalIgnoreCase)) | |
return true; | |
if (detail.PlaceName.Contains(searchString, StringComparison.OrdinalIgnoreCase)) | |
return true; | |
return false; | |
} | |
private IEnumerable<string> Options { get; set; } = new HashSet<string>() { }; | |
private List<AreaPlace> GetPlaces() | |
{ | |
if (!Options.Any()) | |
{ | |
return locations; | |
} | |
return (from a in locations from o in Options where a.AreaName==o select a).ToList(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment