We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

AutoComplete: Differentiate between pressing enter with custom data and selecting a suggested item

I want to have an AutoComplete to handle both suggested items and custom entered data. When they search custom data, I want to navigate to an advanced search page. If they entered a suggested value, I want to go to a details page for that item.

I'm having trouble accomplishing the above.

Realistically I'd like to get callbacks on my blazor page after the following:
  • User types text, press enter key
  • User types text, then clicks on a suggested item
  • User types text, arrows down to a suggested item, presses enter key
  • User types text, which matches a suggested item, presses enter key

And like to know if the callback was executed with custom user text or a valid suggested item.

Please see the attached sln for an incomplete example. I've tried using ValueChange but it fires frequently, event when window focus is lost. Also tried OnValueSelect but that only happens for suggested items, and I can't seem to get the TestItem instance that the event was fired for. The args only contains js objects it seems.

Attachment: SfAutoCompleteBehaviour_a095968.zip

1 Reply 1 reply marked as answer

JM Jeyanth Muthu Pratheeban Sankara Subramanian Syncfusion Team August 20, 2020 03:14 PM UTC

Hi Alek, 

Greetings from Syncfusion support. 


We have analyzed your requirement and checked the attached project. Please find the details below.


 
Scenario 1: User types name that isnt suggested and presses enter 
Result 1: Call NavigateToSearch with entered text 

Solution:  
 
We would like to know you that filtering event will trigger if you type the value in the control. So we suggest you to compare the typed value with the data source items in the filtering event and call the NavigateToSearch in the keyup event when pressing Enter key using Boolean value. 


    public void onFiltering(FilteringEventArgs args) 
    { 
        searchText = args.Text; 
 
        foreach (TestItem item in Options) 
        { 
            if (item.DisplayText.Contains(args.Text)) 
            { 
                isSuggested = true; 
            } 
            else 
            { 
                isSuggested = false; 
            } 
        } 
    } 
 
    public void onInput(Microsoft.AspNetCore.Components.Web.KeyboardEventArgs e) 
    { 
        if(e.Code == "Enter") 
        { 
            // Call the NavigateToSearch method using searchText variable 
        } 
    } 
 

Scenario 2: User types text and either clicks, or arrows down to a suggested item 

Result 2: Call NavigateToAsset with item that was suggested 

Solution:  

When selecting any value from popup, ValueSelect event will be triggered. So we suggest you to get the suggested item from the ValueSelect event arguments as mentioned code example. 


private async Task OnValueSelect( SelectEventArgs obj ) 
    { 
        Selected = AutoComplete.Value ?? "Null"; 
        var itemData = Newtonsoft.Json.JsonConvert.DeserializeObject<TestItem>(obj.ItemData != null ? obj.ItemData.ToString() : ""); 
        var value = itemData.DisplayText; 
 
        //Call to NavigateToAsset using the searchText variable for query 2 and query 3 
 
        //var blah = await AutoComplete.GetDataByValue( AutoComplete.Value ) as TestItem; 
 
        StateHasChanged(); 
    } 
 

Scenario 3: User types text that matches a suggested item 

Result 3: Same as #2 

Solution: 
 
 
Also when you type value that matches with the popup value and select that item, ValueSelect event will be triggered We suggest to the use the solution that we provided in the scenario 2 for the scenario 3 also.  


Please find the sample from the below link: 


 
 
Regards, 
Jeyanth. 


Marked as answer
Loader.
Up arrow icon