@page "/"
@using System.Collections.Generic;
@using System.Linq;
<h1>Hello, Shared Search!</h1>
<span>
<SearchableDropdown Data="new List<ISearchableDropdownItem>(DefaultData)" GetDisplayInfo="GetExampleDisplayInfo" OnSearchUpdated="ExampleSearchUpdated" SetItemID="SetExampleID"></SearchableDropdown>
</span>
<span>
<SearchableDropdown Data="new List<ISearchableDropdownItem>(DefaultData)" GetDisplayInfo="GetExampleDisplayInfo" OnSearchUpdated="ExampleSearchUpdated" SetItemID="SetExampleID"></SearchableDropdown>
</span>
<span>
<SearchableDropdown Data="new List<ISearchableDropdownItem>(DefaultData)" GetDisplayInfo="GetExampleDisplayInfo" OnSearchUpdated="ExampleSearchUpdated" SetItemID="SetExampleID"></SearchableDropdown>
</span>
@code
{
List<ExampleData> DefaultData = ExampleData.GetDefaultData();
int SelectedID { get; set; } = 0;
// This would return the item's DropdownDisplayInfo if it exists
// In retrospect, I think I could simplify the SearchableDropdown a bit because of this design
// using the Interface
string GetExampleDisplayInfo(int id)
{
if (DefaultData is null || DefaultData.Count() == 0 || id == 0)
return String.Empty;
return GetDataByID(id).DropdownDisplayInfo;
}
// This is flawed and will break this after a search
// since this is usually info grabbed from a DB
// but for this quick and dirty... idc
void ExampleSearchUpdated(string searchTerms)
{
if (!String.IsNullOrEmpty(searchTerms))
{
var results = DefaultData.Where(data => data.FirstName.Contains(searchTerms)).ToList();
if (results != null && results.Count > 0)
{
DefaultData = results;
}
}
}
void SetExampleID(int exampleID)
{
SelectedID = exampleID;
}
ExampleData GetDataByID(int id)
{
return DefaultData.Where(data => data.ID == id).FirstOrDefault();
}
}