How to Get Code in Autocomplete, while searching on name

Dear i am using syncfusion auto complete, i am passing class with Name, Code i want to search on name but on selection i need code of selected item, kindly help

1 Reply 1 reply marked as answer

PM Ponmani Murugaiyan Syncfusion Team November 27, 2020 03:34 PM UTC

Hi Nadeem, 

Greetings from Syncfusion support. 

We would like to know you that mapping text field to AutoComplete will only update the text to list items in popup but functionalities like sorting and filtering will be performed based on the value field. So, we suggest you that, in order to filter the items in AutoComplete based on text field, you can map the filed in the filtering event as like below code to meet your requirement . Please find the code snippet and text sample below for reference. 

<p>AutoComplete value is: @AutoVal</p> 
 
<SfAutoComplete @ref="autoObj" ID="autocomplete" TValue="string" TItem="Countries" @bind-Value="@AutoVal" Placeholder="e.g. Australia" DataSource="@Country"> 
    <AutoCompleteFieldSettings Text="Name" Value="Code"></AutoCompleteFieldSettings> 
    <AutoCompleteEvents Filtering="OnFiltering" TValue="string" TItem="Countries"></AutoCompleteEvents> 
</SfAutoComplete> 
 
@code { 
    public string AutoVal 
    SfAutoComplete<string, Countries> autoObj; 
    public Query query; 
    public class Countries 
    { 
        public string Name { get; set; } 
        public string Code { get; set; } 
    } 
    List<Countries> Country = new List<Countries> 
    { 
        new Countries() { Name = "Australia", Code = "AU" }, 
        new Countries() { Name = "Bermuda", Code = "BM" }, 
        new Countries() { Name = "Canada", Code = "CA" }, 
        new Countries() { Name = "Cameroon", Code = "CM" }, 
    }; 
    public async Task OnFiltering(FilteringEventArgs e) 
    { 
        e.PreventDefaultAction = true; 
        query = new Query().Where(new WhereFilter() 
        { 
            Field = "Name", 
            value = e.Text, 
            Operator = "startswith", 
            IgnoreCase = true 
        }); 
        await this.autoObj.Filter(Country, query); 
    } 
} 

 
For more details regarding the Autocomplete component functionalities and behavior, please find the link below. 


Please get back us, if you need further assistance. 

Regards, 
Ponmani M 


Marked as answer
Loader.
Up arrow icon