Friday, May 3, 2024
HomeC#Remodeling Blazor AutoComplete: Unleashing Google-Like Search Options

Remodeling Blazor AutoComplete: Unleashing Google-Like Search Options


The Blazor AutoComplete is a textual content field element that gives an inventory of recommendations to pick out from because the consumer varieties. It has a number of out-of-the-box options, comparable to information binding, filtering, grouping, UI customization, and accessibility. You’ll be able to customise the suggestion listing, filter sort, minimal size, autofill function, and extra. You may also outline your personal information supply for the suggestion listing.

On this weblog, we’ll discover tips on how to implement Google-like search recommendations within the Blazor AutoComplete element. This entails conducting a Google seek for every keypress within the Blazor AutoComplete and displaying the related Google search ends in the suggestion listing.

Let’s get began!

Conditions

Process

Observe these steps to implement Google search recommendations within the Blazor AutoComplete management:

Step 1: Create a Blazor server app

Create a brand new Blazor Server app utilizing Visible Studio, set up the Syncfusion Blazor packages, and configure the type and script references utilizing the getting began documentation.

Step 2: Add the AutoComplete element to the app

Add the Blazor AutoComplete management with the sphere settings configurations to the Index.razor file.

Consult with the next code instance.

<SfAutoComplete TValue="string" Placeholder="Search one thing" Titem="GoogleSearch" Autofill=true>
 <AutoCompleteFieldSettings Worth="SuggestionText" />
</SfAutoComplete>

@code {
    class GoogleSearch
    {
        public string SuggestionText { get; set; } = String.Empty;
    }
}

Step 3: Add the Filtering occasion in AutoComplete

Add the Filtering occasion to the AutoComplete element to deal with customized search choices inside it.

<SfAutoComplete TValue="string" TItem="GoogleSearch">
 <AutoCompleteFieldSettings Worth="SuggestionText"/>
 <AutoCompleteEvents TItem="GoogleSearch" TValue="string" Filtering="@OnCustomFiltering" />
</SfAutoComplete>

@code {
    class GoogleSearch
    {
        public string SuggestionText { get; set; } = String.Empty;
    }
    personal async Process OnCustomFiltering(FilteringEventArgs args)
    {
        
    }
}

Step 4: Get recommendations from the Google search API primarily based on consumer enter

Once we use a search engine to search for one thing, the recommendations are displayed primarily based on the characters we enter.

Let’s see tips on how to retrieve recommendations from the Google search engine.

You need to use the next URL to get the search end result information primarily based on user-typed textual content.

https://suggestqueries.google.com/full/search?consumer=firefox&q={typed textual content}

Consult with the next code instance to acquire recommendations from the Google search engine.

static readonly HttpClient consumer = new HttpClient();

personal async Process OnCustomFiltering(FilteringEventArgs args)
{
    var recommendations = new Record<GoogleSearch>();
    var url = "https://suggestqueries.google.com/full/search?consumer=firefox&q=" + args.Textual content;

    var response = await consumer.GetAsync(url);
    response.EnsureSuccessStatusCode();
    utilizing (var stream = await response.Content material.ReadAsStreamAsync())
}

Step 5: Parse the response content material to an inventory of objects

The response content material from the HttpClient request shall be transformed to JSON by means of stream conversion. The JSON information is then transformed to an inventory of objects of the GoogleSearch class and loaded into the AutoComplete suggestion listing utilizing the FilterAsync public methodology.

Consult with the next code instance.

@utilizing Syncfusion.Blazor.Inputs
@utilizing Syncfusion.Blazor.DropDowns;
@utilizing System.Internet;
@utilizing System.Textual content.Json;

<SfAutoComplete @ref="AutoCompleteRef" Placeholder="Search one thing" TValue="string" TItem="GoogleSearch" Width="500px">
 <AutoCompleteFieldSettings Worth="SuggestionText" />
 <AutoCompleteEvents TItem="GoogleSearch" TValue="string" Filtering="@OnCustomFiltering" />
</SfAutoComplete>
@code {
    SfAutoComplete<string, GoogleSearch> AutoCompleteRef { get; set; }
    static readonly HttpClient consumer = new HttpClient();
    class GoogleSearch
    {
        public string SuggestionText { get; set; } = String.Empty;
    }
    personal async Process OnCustomFiltering(FilteringEventArgs args)
    {
        var recommendations = new Record<GoogleSearch>();
        var url = "https://suggestqueries.google.com/full/search?consumer=firefox&q=" + args.Textual content;

        var response = await consumer.GetAsync(url);
        response.EnsureSuccessStatusCode();
        utilizing (var stream = await response.Content material.ReadAsStreamAsync())
        utilizing (var reader = new StreamReader(stream))
        {
            var end result = reader.ReadToEnd();
            var json = JsonDocument.Parse(end result);
            var array = json.RootElement.EnumerateArray();
            foreach (var merchandise in array.Skip(1).Take(1).ToList())

            {
                foreach (var suggestion in merchandise.EnumerateArray())
                {
                    recommendations.Add(new GoogleSearch { SuggestionText = suggestion.GetString() });
                }
            }
        }
        await AutoCompleteRef.FilterAsync(recommendations);
    }
}
Google-Like Search Suggestions in Blazor AutoComplete
Google-Like Search Options in Blazor AutoComplete

Reference

You’ll be able to discuss with the Google-like search recommendations within the Blazor Autocomplete demo on GitHub.

Conclusion

Thanks for studying! On this weblog, we’ve seen tips on how to implement Google-like autosuggestions within the Syncfusion Blazor AutoComplete element. Check out the steps and depart your suggestions within the feedback part beneath!

Attempt our Blazor parts by downloading a free 30-day trial or by downloading our NuGet package deal. Be at liberty to take a look at our on-line examples and documentation to discover different accessible options.

If you wish to contact us, you possibly can attain us by means of our help boardshelp portal, or suggestions portal. We’re at all times completely happy to help you!

Associated blogs

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments