Your search did not match any results.

Autocomplete

Autocomplete is a text box that displays suggestions while a user types. This demo shows how to customize the Autocomplete component and handle value changes.

Bind Autocomplete to Data

Use one of the following properties to supply data to the component:

  • items[]
    Accepts a local data array.

  • dataSource
    Accepts a local data array, DataSource object, or DevExtreme data store. In this demo, every Autocomplete component is bound to a local array, except the "Custom Item Template and Data Source Usage" component that uses an ODataStore.

The value property stores the selected item. You can use the same property to select an item programmatically, as shown in the "Disabled" Autocomplete component.

If the data source contains objects, you should specify the valueExpr property. It accepts a data field name that uniquely identifies each data object. In this demo, valueExpr is set only for the "Custom Item Template and Data Source Usage" Autocomplete because others are bound to arrays of primitive values.

Configure Search Parameters

When a user types the first character, Autocomplete displays suggestions. To increase the number of characters that triggers suggestions, use the minSearchLength property. You can also specify the time interval Autocomplete should wait before it displays suggestions. Assign this time interval in milliseconds to the searchTimeout property. See the "With Custom Search Options" Autocomplete component for an example.

Usually, the data field that supplies Autocomplete with suggestions (valueExpr) is the same data field that is searched for the typed text. If you use two different fields, assign the field that supplies Autocomplete with suggestions to the valueExpr property and the field to be searched to the searchExpr property. Note that searchExpr also accepts arrays if you want to search multiple fields.

Handle Value Change

To handle value changes, implement the onValueChanged function. In this demo, we use onValueChanged to display the values of all Autocomplete components.

Appearance Customization

You can specify the following properties to customize the Autocomplete component's appearance:

  • placeholder
    The text that is displayed when Autocomplete is empty.

  • showClearButton
    Adds a Clear button that empties the Autocomplete value as shown in the "With Clear Button" Autocomplete component.

  • disabled
    Disables the component.

  • itemTemplate
    Customizes the appearance of the Autocomplete suggestions. See the "Custom Item Template and Data Source Usage" component for an example.

Backend API
<div class="form"> <div class="dx-fieldset"> <div class="dx-fieldset-header">Default Mode</div> <div class="dx-field"> <div class="dx-field-label">First Name</div> <div class="dx-field-value"> @(Html.DevExtreme().Autocomplete() .DataSource(d => d.Mvc().LoadAction("GetNames")) .Placeholder("Type first name...") .OnValueChanged("name_changed") ) </div> </div> </div> <div class="dx-fieldset"> <div class="dx-fieldset-header">With Clear Button</div> <div class="dx-field"> <div class="dx-field-label">Last Name</div> <div class="dx-field-value"> @(Html.DevExtreme().Autocomplete() .DataSource(d => d.Mvc().LoadAction("GetSurnames")) .ShowClearButton(true) .Placeholder("Type last name...") .OnValueChanged("surname_changed") ) </div> </div> </div> <div class="dx-fieldset"> <div class="dx-fieldset-header">Disabled</div> <div class="dx-field"> <div class="dx-field-label">Position</div> <div class="dx-field-value"> @(Html.DevExtreme().Autocomplete() .DataSource(d => d.Mvc().LoadAction("GetPositions")) .Value("CEO") .Disabled(true) .OnValueChanged("position_changed") ) </div> </div> </div> <div class="dx-fieldset"> <div class="dx-fieldset-header">With Custom Search Options</div> <div class="dx-field"> <div class="dx-field-label">City</div> <div class="dx-field-value"> @(Html.DevExtreme().Autocomplete() .DataSource(d => d.Mvc().LoadAction("GetCities")) .MinSearchLength(2) .SearchTimeout(500) .Placeholder("Type two symbols to search...") .OnValueChanged("city_changed") ) </div> </div> </div> <div class="dx-fieldset"> <div class="dx-fieldset-header">Custom Item Template and Data Source Usage</div> <div class="dx-field"> <div class="dx-field-label">State</div> <div class="dx-field-value"> @(Html.DevExtreme().Autocomplete() .DataSource(d => d.OData() .Url("https://js.devexpress.com/Demos/DevAV/odata/States?$select=Sate_ID,State_Long,State_Short") .Key("Sate_ID") .KeyType(EdmType.Int32) ) .ValueExpr("State_Long") .Placeholder("Type state name...") .OnValueChanged("state_changed") .ItemTemplate(@<text> <div><%- State_Long %> (<%- State_Short %>)</div> </text>) ) </div> </div> </div> <div class="dx-fieldset"> <div class="dx-fieldset-header">Event Handling</div> <div class="employees-data"> Employee data: <span id="employee-data"></span> </div> </div> </div> <script> var firstName = "", lastName = "", position = "CEO", city = "", state = ""; function updateEmployeeInfo() { var result = $.trim((firstName || "") + " " + (lastName || "")); result += (result && position) ? (", " + position) : position; result += (result && city) ? (", " + city) : city; result += (result && state) ? (", " + state) : state; $("#employee-data").text(result); } function name_changed(data) { firstName = data.value; updateEmployeeInfo(); } function surname_changed(data) { lastName = data.value; updateEmployeeInfo(); } function position_changed(dat) { position = data.value; updateEmployeeInfo(); } function city_changed(data) { city = data.value; updateEmployeeInfo(); } function state_changed(data) { state = data.value; updateEmployeeInfo(); } </script>
using DevExtreme.AspNet.Data; using DevExtreme.AspNet.Mvc; using DevExtreme.NETCore.Demos.Models.SampleData; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; namespace DevExtreme.NETCore.Demos.Controllers { public class AutocompleteController : Controller { public ActionResult Overview() { return View(); } [HttpGet] public object GetNames(DataSourceLoadOptions loadOptions) { return DataSourceLoader.Load(SampleData.Names, loadOptions); } [HttpGet] public object GetSurnames(DataSourceLoadOptions loadOptions) { return DataSourceLoader.Load(SampleData.Surnames, loadOptions); } [HttpGet] public object GetCities(DataSourceLoadOptions loadOptions) { return DataSourceLoader.Load(SampleData.Cities, loadOptions); } [HttpGet] public object GetPositions(DataSourceLoadOptions loadOptions) { return DataSourceLoader.Load(SampleData.Positions, loadOptions); } } }
using System; using System.Collections.Generic; using System.Linq; namespace DevExtreme.NETCore.Demos.Models.SampleData { public partial class SampleData { public static readonly IEnumerable<string> Cities = new[] { "Albuquerque", "Anaheim", "Anchorage", "Arlington", "Atlanta", "Aurora", "Austin", "Bakersfield", "Baltimore", "Baton Rouge", "Boise", "Boston", "Buffalo", "Chandler", "Charlotte", "Chesapeake", "Chicago", "Chula Vista", "Cincinnati", "Cleveland", "Colorado Springs", "Columbus", "Corpus Christi", "Dallas", "Denver", "Detroit", "Durham", "El Paso", "Fort Wayne", "Fort Worth", "Fremont", "Fresno", "Garland", "Gilbert", "Glendale", "Greensboro", "Henderson", "Hialeah", "Honolulu", "Houston", "Indianapolis", "Irvine", "Irving", "Jacksonville", "Jersey City", "Kansas City", "Laredo", "Las Vegas", "Lexington", "Lincoln", "Long Beach", "Los Angeles", "Louisville", "Lubbock", "Madison", "Memphis", "Mesa", "Miami", "Milwaukee", "Minneapolis", "Nashville", "New Orleans", "New York", "Newark", "Norfolk", "North Las Vegas", "Oakland", "Oklahoma City", "Omaha", "Orlando", "Philadelphia", "Phoenix", "Pittsburgh", "Plano", "Portland", "Raleigh", "Reno", "Richmond", "Riverside", "Sacramento", "Saint Paul", "San Antonio", "San Diego", "San Francisco", "San Jose", "Santa Ana", "Scottsdale", "Seattle", "St. Louis", "St. Petersburg", "Stockton", "Tampa", "Toledo", "Tucson", "Tulsa", "Virginia Beach", "Washington", "Wichita", "Winston–Salem" }; } }
using System; using System.Collections.Generic; using System.Linq; namespace DevExtreme.NETCore.Demos.Models.SampleData { public partial class SampleData { public static readonly IEnumerable<string> Names = new[] { "James", "John", "Robert", "Michael", "William", "David", "Richard", "Charles", "Joseph", "Thomas", "Christopher", "Daniel", "Paul", "Mark", "Donald", "George", "Kenneth", "Steven", "Edward", "Brian", "Ronald", "Anthony", "Kevin", "Jason", "Jeff", "Mary", "Patricia", "Linda", "Barbara", "Elizabeth", "Jennifer", "Maria", "Susan", "Margaret", "Dorothy", "Lisa", "Nancy", "Karen", "Betty", "Helen", "Sandra", "Donna", "Carol", "Ruth", "Sharon", "Michelle", "Laura", "Sarah", "Kimberly", "Deborah" }; } }
using System; using System.Collections.Generic; using System.Linq; namespace DevExtreme.NETCore.Demos.Models.SampleData { public partial class SampleData { public static readonly IEnumerable<string> Surnames = new[] { "Anderson", "Smith", "Johnson", "Williams", "Jones", "Brown", "Davis", "Miller", "Wilson", "Moore", "Taylor", "Thomas", "Jackson", "White", "Harris", "Martin", "Thompson", "Garcia", "Martinez", "Robinson", "Clark", "Rodriguez", "Lewis", "Lee", "Walker", "Hall", "Allen", "Young", "Hernandez", "King", "Wright", "Lopez", "Hill", "Scott", "Green", "Adams", "Baker", "Gonzalez", "Nelson", "Carter", "Mitchell", "Perez", "Roberts", "Turner", "Phillips", "Campbell", "Parker", "Evans", "Edwards", "Collins" }; } }
.employees-data { padding-top: 16px; padding-bottom: 10px; }