AutoComplete with Blazorised Textbox


Currently i have used this control in one of my client blazor project. It is very handy and completely free to use.
@page "/"
@using Blazorise.Components

<Autocomplete TItem="Countries"
              TValue="string"
              Data="@LocalData"
              TextField="@(( item ) => item.Name)"
              ValueField="@(( item ) => item.Code)"
              Placeholder="Search..."
              Filter="AutocompleteFilter.StartsWith"
              @bind-SelectedValue="@selectedSearchValue"
              @bind-SelectedText="selectedAutoCompleteText"
              FreeTyping
              CustomFilter="@(( item, searchValue ) => item.Name.IndexOf( searchValue, 0, StringComparison.CurrentCultureIgnoreCase ) >= 0 )">
    <NotFoundContent> Sorry... @context was not found! :( </NotFoundContent>
</Autocomplete>

<p role="status">Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Clear Text</button>

@code
{
    public string? selectedSearchValue { get; set; }
    public string? selectedAutoCompleteText { get; set; }

    public class Countries
    {
        public string Name { get; set; }
        public string Code { get; set; }
    }

    List<Countries> LocalData = 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" },
        new Countries() { Name = "Denmark", Code = "DK" }
    };

    private int currentCount = 0;

    private void IncrementCount()
    {
        currentCount++;
        this.selectedAutoCompleteText = string.Empty;
    }
}
Advertisement

3 thoughts on “AutoComplete with Blazorised Textbox

  1. Marvin Jno-Baptiste July 8, 2022 / 9:21 pm

    I get a load of errors about delegates and attribute names cannot be inferred when using this code. Any idea what I’m doing wrong?

  2. Marvin Jno-Baptiste July 8, 2022 / 9:24 pm

    Never mind – I was being stupid

  3. Chandradev July 13, 2022 / 5:08 am

    Please send me complete stack trace errors message.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.