Controllo AutoCompleteList

Il controllo AutoCompleteList permette di cercare e selezionare uno o più valori a partire da una sorgente dati. I valori selezionati devono essere presenti nella sorgente dati. Utile quando la sorgente dati contiene molti elementi che renderebbero il form troppo lungo o lento se visualizzati contemporaneamente.
 
Personalizzazione
Possiamo impostare una sorgente dati personalizzata specificando il valore ControlClass per DataSourceMode e inserendo namespace e nome classe nella proprietà del controllo CustomControlClass.
 
Nel metodo GetListValuesAsync dobbiamo gestire sia la fase di recupero dei suggerimento (parametro Title) sia quella di recupero, validazione e salvataggio dei relativi valori (parametro Text).
 
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;

namespace MyApp.DataWeb.Data.Controls
{
    public class News_UserIdMasters : AutoCompleteList
    {
        private readonly UserProfileStore userProfileStore;

        public News_UserIdMasters(Form form, IServiceProvider serviceProvider) : base(form, serviceProvider)
        {
            userProfileStore = serviceProvider.GetService<UserProfileStore>();
        }

        public override async Task<IEnumerable<List.ListItem>> GetListValuesAsync(Dictionary<string, object> parameters, IUser user, string itemId = null, NavigationContext navigationContext = null)
        {
            var userProfiles = Enumerable.Empty<UserProfile>();

            if (parameters.ContainsKey("Title"))
            {
                userProfiles = await userProfileStore.GetUserProfilesAsync(new UserProfileStore.Filter { SuggestionText = Convert.ToString(parameters["Title"]), IsPrivateAreaAccess = true });
            }
            else
            {
                // Value filter

                if (!string.IsNullOrEmpty(Convert.ToString(parameters["Value"])))
                {
                    var values = Convert.ToString(parameters["Value"]).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    if (values.Length != 0)
                    {
                        userProfiles = await userProfileStore.GetUserProfilesAsync(new UserProfileStore.Filter { IdMasters = values.ToList(), IsPrivateAreaAccess = true });
                    }
                }
            }

            return userProfiles.Select(x => new List.ListItem { Title = x.UserName, Value = x.IdMaster });
        }

        public override async Task<object> ProcessOnSaveDataAsync(object value, List<Form.ProvidedValue> providedValues, Dictionary<string, object> sectionData, IUser user, string itemId = null, NavigationContext navigationContext = null)
        {
            var listValues = await GetListValuesAsync(new Dictionary<string, object> { ["Value"] = value }, user, itemId, navigationContext);

            // Set additional data
            sectionData["UserDescriptions"] = string.Join(", ", listValues.Select(x => x.Title));

            return value;
        }
    }
}
 
In questo esempio deriviamo anche il metodo ProcessOnSaveDataAsync per salvare nella base dati anche le descrizioni dei valori selezionati.