AutoComplete Control
The AutoComplete control allows you to search for and select a value from a data source. The selected value must be present in the data source. Useful when the data source contains many elements that would make the form too long or slow if viewed at the same time.
Personalization
We can set up a custom data source by specifying the ControlClass value for DataSourceMode and putting namespace and class name in the CustomControlClass control's property.
In the GetListValuesAsync method we have to manage both the retrieval phase of the suggestion ( Title parameter) and that of retrieval, validation and saving of the relative value ( Text parameter).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using DataWeb.Data;
using DataWeb.Data.Controls;
using DataWeb.Identity;
using DataWeb.Structure;
using MyApp.Infrastructure;
using MyApp.Structure;
namespace MyApp.DataWeb.Data.Controls
{
public class Project_CompanyIdMaster : AutoComplete
{
private readonly CompanyStore companyStore;
public Project_CompanyIdMaster(Form form, IServiceProvider serviceProvider) : base(form, serviceProvider)
{
companyStore = serviceProvider.GetService<CompanyStore>();
}
public override async Task<IEnumerable<List.ListItem>> GetListValuesAsync(Dictionary<string, object> parameters, IUser user, string itemId = null, NavigationContext navigationContext = null, CancellationToken cancellationToken = default)
{
var companies = Enumerable.Empty<Company>();
if (parameters.ContainsKey("Value"))
{
// Get selected items
companies = await companyStore.GetCompaniesAsync(new CompanyStore.Filter { IdMaster = Convert.ToString(parameters["Value"]) });
}
if (parameters.ContainsKey("Title"))
{
// Get suggested items
companies = await companyStore.GetCompaniesAsync(new CompanyStore.Filter { SearchText = Convert.ToString(parameters["Title"]) });
}
return companies.Select(x => new List.ListItem { Title = x.Name, Value = x.IdMaster, AdditionalValues = new List<List.ListItem.AdditionalValue>() });
}
}
}