Autocomplete - 图1

Autocomplete

Framework7 comes with mobile-friendly and touch optimized Autocomplete component.

Autocomplete could be used in standalone mode or as a dropdown.

Autocomplete App Methods

Autocomplete can be created and initialized only using JavaScript. We need to use related App’s method:

app.autocomplete.create(parameters)- create Autocomplete instance

  • parameters - object. Object with autocomplete parameters

Method returns created Autocomplete’s instance

app.autocomplete.destroy(el)- destroy Autocomplete instance

  • el - HTMLElement or string (with CSS Selector) or object. Autocomplete instance to destroy.

app.autocomplete.get(el)- get Autocomplete instance by HTML element

  • el - HTMLElement or string (with CSS Selector). Autocomplete element.

Method returns Autocomplete’s instance

app.autocomplete.open(el)- open Autocomplete

  • el - HTMLElement or string (with CSS Selector). Autocomplete element to open.

Method returns Autocomplete’s instance

app.autocomplete.close(el)- closes Autocomplete

  • el - HTMLElement or string (with CSS Selector). Autocomplete element to close.

Method returns Autocomplete’s instance

For example:

  1. var autocomplete = app.autocomplete.create({
  2. inputEl: '#autocomplete-dropdown',
  3. openIn: 'dropdown',
  4. source: function (query, render) {
  5. ...
  6. }
  7. });

Autocomplete Parameters

Let’s look on list of all available parameters:

ParameterTypeDefaultDescription
openInstringpageDefines how to open Autocomplete, can be page or popup (for Standalone) or dropdown
sourcefunction (query, render)Function which accepts search query and render function where you need to pass array with matched items
limitnumberLimit number of maximum displayed items in autocomplete per query
preloaderbooleanfalseSet to true to include Preloader to autocomplete layout
preloaderColorstringPreloader color, one of the default colors
valuearrayArray with default selected values
valuePropertystringidName of matched item object’s key that represents item value
textPropertystringtextName of matched item object’s key that represents item display value which is used as title of displayed options
Standalone Autocomplete Parameters
requestSourceOnOpenbooleanfalseIf enabled, then it will request passed to source function on autocomplete open
openerElstring
HTMLElement
String with CSS selector or HTMLElement of link which will open standalone autocomplete page or popup on click
popupCloseLinkTextstringCloseDefault text for “Close” button when opened as Popup
pageBackLinkTextstringBackDefault text for “Back” link when opened as Page
pageTitlestringAutocomplete page title. If nothing is specified and passed openerEl is an item of List View, then text value of item-title element will be used
searchbarPlaceholderstringSearch…Searchbar placeholder text
searchbarDisableTextstringCancelSearchbar “Cancel” button text
notFoundTextstringNothing foundText which is displayed when no matches found
multiplebooleanfalseSet to true to allow multiple selections
closeOnSelectbooleanfalseSet to true and autocomplete will be closed when user picks value. Not available if multiple is enabled
autoFocusbooleanfalseSet to true to auto focus search field on autocomplete open
animatebooleantrueSet to false to open standalone autocomplete without animation
navbarColorThemestringNavbar color theme. One of the default color themes
formColorThemestringForm (checkboxes or radios) color theme. One of the default color themes
routableModalsbooleantrueWill add opened autocomplete modal (when openIn: ‘popup’) to router history which gives ability to close autocomplete by going back in router history and set current route to the autocomplete modal
urlstringselect/Standalone autocomplete URL that will be set as a current route
viewobjectLink to initialized View instance if you want use standalone Autcomplete. By default, if not specified, it will be opened in Main View.
Dropdown Autocomplete Parameters
inputElstring
HTMLElement
String with CSS selector or HTMLElement of related text input
inputEventsstringinputAllows to configure input events used to handle Autcomplete actions and source request. Can be changed for example to change keyup compositionend if you use keyboard with composition of Chinese characters
highlightMatchesbooleantrueHighlight matches in autcomplete results
typeaheadbooleanfalseEnables type ahead, will prefill input value with first item in match
dropdownPlaceholderTextstringSpecify dropdown placeholder text
updateInputValueOnSelectbooleantrueIf true then value of related input will be update as well
expandInputbooleanfalseIf true then input which is used as item-input in List View will be expanded to full screen wide during dropdown visible.
dropdownContainerElstring
HTMLElement
By default dropdown will be added to parent page-content element. You can specify here different element where to add dropdown element
Render functions
renderDropdownfunction(items)Function to render autocomplete dropdown, must return dropdown HTML string
renderPagefunction(items)Function to render autocomplete page, must return page HTML string
renderPopupfunction(items)Function to render autocomplete popup, must return popup HTML string
renderItemfunction(item, index)Function to render single autocomplete, must return item HTML string
renderSearchbarfunctionFunction to render searchbar, must return searchbar HTML string
renderNavbarfunctionFunction to render navbar, must return navbar HTML string
Events
onobjectObject with events handlers. For example:
  1. var autocomplete = app.autocomplete.create({
  2. on: {
  3. opened: function () {
  4. console.log(‘Autocomplete opened’)
  5. }
  6. }
  7. })

Note that all following parameters can be used in global app parameters under autocomplete property to set defaults for all autcompletes. For example:

  1. var app = new Framework7({
  2. autocomplete: {
  3. openIn: 'popup',
  4. animate: false,
  5. }
  6. });

Autocomplete Methods & Properties

After we initialize Autocomplete we have its initialized instance in variable (like autocomplete variable in example above) with helpful methods and properties:

Properties
autocomplete.paramsObject with passed initialization parameters
autocomplete.valueArray with selected items
autocomplete.openedtrue if Autocomplete is currently opened
autocomplete.openerElHTML element of Autcomplete opener element (if passed on init)
autocomplete.$openerElDom7 instance of of Autcomplete opener element (if passed on init)
autocomplete.inputElHTML element of Autcomplete input (if passed on init)
autocomplete.$inputElDom7 instance of of Autcomplete input (if passed on init)
autocomplete.$dropdownElDom7 instance of Autcomplete dropdown
autocomplete.urlAutcomplete URL (that was passed in url parameter)
autocomplete.viewAutcomplete View (that was passed in view parameter) or found parent view
autocomplete.elHTML element of Autcomplete container: dropdown element, or popup element, or page element. Available when Autocomplete opened
autocomplete.$elDom7 instance of Autcomplete container: dropdown element, or popup element, or page element. Available when Autocomplete opened
autocomplete.searchbarAutcomplete page Searchbar instance
Methods
autocomplete.open()Open Autocomplete (Dropdown, Page or Popup)
autocomplete.close()Close Autcomplete
autocomplete.preloaderShow()Show autocomplete preloader
autocomplete.preloaderHide()Hide autocomplete preloader
autocomplete.destroy()Destroy Autocomplete instance and remove all events
autocomplete.on(event, handler)Add event handler
autocomplete.once(event, handler)Add event handler that will be removed after it was fired
autocomplete.off(event, handler)Remove event handler
autocomplete.off(event)Remove all handlers for specified event
autocomplete.emit(event, …args)Fire event on instance

Autocomplete Events

Autocomplete instance emits events on both self instance and app instance. App instance events has same names prefixed with autocomplete.

EventTargetArgumentsDescription
changeautocompleteautocomplete, valueEvent will be triggered when Autocomplete value changed. Returned value is an array with selected items
autocompleteChangeapp
openautocompleteautocompleteEvent will be triggered when Autocomplete starts its opening animation. As an argument event handler receives autocomplete instance
autocompleteOpenapp
openedautocompleteautocompleteEvent will be triggered after Autocomplete completes its opening animation. As an argument event handler receives autocomplete instance
autocompleteOpenedapp
closeautocompleteautocompleteEvent will be triggered when Autocomplete starts its closing animation. As an argument event handler receives autocomplete instance
autocompleteCloseapp
closedautocompleteautocompleteEvent will be triggered after Autocomplete completes its closing animation. As an argument event handler receives autocomplete instance
autocompleteClosedapp
beforeDestroyautocompleteautocompleteEvent will be triggered right before Autocomplete instance will be destroyed. As an argument event handler receives autocomplete instance
autocompleteBeforeDestroyapp

Examples

  1. var app = new Framework7();
  2. var $$ = Dom7;
  3. // Fruits data demo array
  4. var fruits = ('Apple Apricot Avocado Banana Melon Orange Peach Pear Pineapple').split(' ');

Simple Dropdown Autocomplete

  1. <div class="list no-hairlines-md">
  2. <div class="block-header">Simple Dropdown Autocomplete</div>
  3. <ul>
  4. <li class="item-content item-input inline-label">
  5. <div class="item-inner">
  6. <div class="item-title item-label">Fruit</div>
  7. <div class="item-input-wrap">
  8. <input id="autocomplete-dropdown" type="text" placeholder="Fruit">
  9. </div>
  10. </div>
  11. </li>
  12. </ul>
  13. </div>
  1. var autocompleteDropdownSimple = app.autocomplete.create({
  2. inputEl: '#autocomplete-dropdown',
  3. openIn: 'dropdown',
  4. source: function (query, render) {
  5. var results = [];
  6. if (query.length === 0) {
  7. render(results);
  8. return;
  9. }
  10. // Find matched items
  11. for (var i = 0; i < fruits.length; i++) {
  12. if (fruits[i].toLowerCase().indexOf(query.toLowerCase()) >= 0) results.push(fruits[i]);
  13. }
  14. // Render items by passing array with result items
  15. render(results);
  16. }
  17. });

Dropdown With Input Expand

  1. <div class="list no-hairlines-md">
  2. <div class="block-header">Dropdown With Input Expand</div>
  3. <ul>
  4. <li class="item-content item-input inline-label">
  5. <div class="item-inner">
  6. <div class="item-title item-label">Fruit</div>
  7. <div class="item-input-wrap">
  8. <input id="autocomplete-dropdown-expand" type="text" placeholder="Fruit">
  9. </div>
  10. </div>
  11. </li>
  12. </ul>
  13. </div>
  1. var autocompleteDropdownExpand = app.autocomplete.create({
  2. inputEl: '#autocomplete-dropdown-expand',
  3. openIn: 'dropdown',
  4. expandInput: true, // expand input
  5. source: function (query, render) {
  6. var results = [];
  7. if (query.length === 0) {
  8. render(results);
  9. return;
  10. }
  11. // Find matched items
  12. for (var i = 0; i < fruits.length; i++) {
  13. if (fruits[i].toLowerCase().indexOf(query.toLowerCase()) >= 0) results.push(fruits[i]);
  14. }
  15. // Render items by passing array with result items
  16. render(results);
  17. }
  18. });

Dropdown With All Values

  1. <div class="list no-hairlines-md">
  2. <div class="block-header">Dropdown With All Values</div>
  3. <ul>
  4. <li class="item-content item-input">
  5. <div class="item-inner">
  6. <div class="item-title item-label">Fruit</div>
  7. <div class="item-input-wrap">
  8. <input id="autocomplete-dropdown-all" type="text" placeholder="Fruit">
  9. </div>
  10. </div>
  11. </li>
  12. </ul>
  13. </div>
  1. var autocompleteDropdownAll = app.autocomplete.create({
  2. inputEl: '#autocomplete-dropdown-all',
  3. openIn: 'dropdown',
  4. source: function (query, render) {
  5. var results = [];
  6. // Find matched items
  7. for (var i = 0; i < fruits.length; i++) {
  8. if (fruits[i].toLowerCase().indexOf(query.toLowerCase()) >= 0) results.push(fruits[i]);
  9. }
  10. // Render items by passing array with result items
  11. render(results);
  12. }
  13. });

Dropdown With Placeholder

  1. <div class="list no-hairlines-md">
  2. <div class="block-header">Dropdown With Placeholder</div>
  3. <ul>
  4. <li class="item-content item-input">
  5. <div class="item-inner">
  6. <div class="item-title item-label">Fruit</div>
  7. <div class="item-input-wrap">
  8. <input id="autocomplete-dropdown-placeholder" type="text" placeholder="Fruit">
  9. </div>
  10. </div>
  11. </li>
  12. </ul>
  13. </div>
  1. var autocompleteDropdownPlaceholder = app.autocomplete.create({
  2. inputEl: '#autocomplete-dropdown-placeholder',
  3. openIn: 'dropdown',
  4. dropdownPlaceholderText: 'Try to type "Apple"',
  5. source: function (query, render) {
  6. var results = [];
  7. if (query.length === 0) {
  8. render(results);
  9. return;
  10. }
  11. // Find matched items
  12. for (var i = 0; i < fruits.length; i++) {
  13. if (fruits[i].toLowerCase().indexOf(query.toLowerCase()) >= 0) results.push(fruits[i]);
  14. }
  15. // Render items by passing array with result items
  16. render(results);
  17. }
  18. });

Dropdown With Typeahead

  1. <div class="list no-hairlines-md">
  2. <div class="block-header">Dropdown With Typeahead</div>
  3. <ul>
  4. <li class="item-content item-input">
  5. <div class="item-inner">
  6. <div class="item-title item-label">Fruit</div>
  7. <div class="item-input-wrap">
  8. <input id="autocomplete-dropdown-typeahead" type="text" placeholder="Fruit">
  9. </div>
  10. </div>
  11. </li>
  12. </ul>
  13. </div>
  1. var autocompleteDropdownTypeahead = app.autocomplete.create({
  2. inputEl: '#autocomplete-dropdown-typeahead',
  3. openIn: 'dropdown',
  4. dropdownPlaceholderText: 'Try to type "Pineapple"',
  5. typeahead: true,
  6. source: function (query, render) {
  7. var results = [];
  8. if (query.length === 0) {
  9. render(results);
  10. return;
  11. }
  12. // Find matched items
  13. for (var i = 0; i < fruits.length; i++) {
  14. if (fruits[i].toLowerCase().indexOf(query.toLowerCase()) === 0) results.push(fruits[i]);
  15. }
  16. // Render items by passing array with result items
  17. render(results);
  18. }
  19. });

Dropdown With Ajax-Data

  1. <div class="list no-hairlines-md">
  2. <div class="block-header">Dropdown With Ajax-Data</div>
  3. <ul>
  4. <li class="item-content item-input">
  5. <div class="item-inner">
  6. <div class="item-title item-label">Language</div>
  7. <div class="item-input-wrap">
  8. <input id="autocomplete-dropdown-ajax" type="text" placeholder="Language">
  9. </div>
  10. </div>
  11. </li>
  12. </ul>
  13. </div>
  1. var autocompleteDropdownAjax = app.autocomplete.create({
  2. inputEl: '#autocomplete-dropdown-ajax',
  3. openIn: 'dropdown',
  4. preloader: true, //enable preloader
  5. /* If we set valueProperty to "id" then input value on select will be set according to this property */
  6. valueProperty: 'name', //object's "value" property name
  7. textProperty: 'name', //object's "text" property name
  8. limit: 20, //limit to 20 results
  9. dropdownPlaceholderText: 'Try "JavaScript"',
  10. source: function (query, render) {
  11. var autocomplete = this;
  12. var results = [];
  13. if (query.length === 0) {
  14. render(results);
  15. return;
  16. }
  17. // Show Preloader
  18. autocomplete.preloaderShow();
  19. // Do Ajax request to Autocomplete data
  20. app.request({
  21. url: 'autocomplete-languages.json',
  22. method: 'GET',
  23. dataType: 'json',
  24. //send "query" to server. Useful in case you generate response dynamically
  25. data: {
  26. query: query,
  27. },
  28. success: function (data) {
  29. // Find matched items
  30. for (var i = 0; i < data.length; i++) {
  31. if (data[i].name.toLowerCase().indexOf(query.toLowerCase()) >= 0) results.push(data[i]);
  32. }
  33. // Hide Preoloader
  34. autocomplete.preloaderHide();
  35. // Render items by passing array with result items
  36. render(results);
  37. }
  38. });
  39. }
  40. });

Dropdown With Ajax-Data + Typeahead

  1. <div class="list no-hairlines-md">
  2. <div class="block-header">Dropdown With Ajax-Data + Typeahead</div>
  3. <ul>
  4. <li class="item-content item-input">
  5. <div class="item-inner">
  6. <div class="item-title item-label">Language</div>
  7. <div class="item-input-wrap">
  8. <input id="autocomplete-dropdown-ajax-typeahead" type="text" placeholder="Language">
  9. </div>
  10. </div>
  11. </li>
  12. </ul>
  13. </div>
  1. var autocompleteDropdownAjaxTypeahead = app.autocomplete.create({
  2. inputEl: '#autocomplete-dropdown-ajax-typeahead',
  3. openIn: 'dropdown',
  4. preloader: true, //enable preloader
  5. /* If we set valueProperty to "id" then input value on select will be set according to this property */
  6. valueProperty: 'name', //object's "value" property name
  7. textProperty: 'name', //object's "text" property name
  8. limit: 20, //limit to 20 results
  9. typeahead: true,
  10. dropdownPlaceholderText: 'Try "JavaScript"',
  11. source: function (query, render) {
  12. var autocomplete = this;
  13. var results = [];
  14. if (query.length === 0) {
  15. render(results);
  16. return;
  17. }
  18. // Show Preloader
  19. autocomplete.preloaderShow();
  20. // Do Ajax request to Autocomplete data
  21. app.request({
  22. url: 'autocomplete-languages.json',
  23. method: 'GET',
  24. dataType: 'json',
  25. //send "query" to server. Useful in case you generate response dynamically
  26. data: {
  27. query: query,
  28. },
  29. success: function (data) {
  30. // Find matched items
  31. for (var i = 0; i < data.length; i++) {
  32. if (data[i].name.toLowerCase().indexOf(query.toLowerCase()) === 0) results.push(data[i]);
  33. }
  34. // Hide Preoloader
  35. autocomplete.preloaderHide();
  36. // Render items by passing array with result items
  37. render(results);
  38. }
  39. });
  40. }
  41. });

Searchbar Dropdown

  1. <div class="page">
  2. <div class="navbar">
  3. <div class="navbar-inner sliding">
  4. ...
  5. <!-- Put searchbar in subnavbar -->
  6. <div class="subnavbar">
  7. <form class="searchbar" id="searchbar-autocomplete">
  8. <div class="searchbar-inner">
  9. <div class="searchbar-input-wrap">
  10. <input type="search" placeholder="Search">
  11. <i class="searchbar-icon"></i>
  12. <span class="input-clear-button"></span>
  13. </div>
  14. <span class="searchbar-disable-button">Cancel</span>
  15. </div>
  16. </form>
  17. </div>
  18. </div>
  19. </div>
  20. ...
  21. </div>
  1. var searchbar = app.searchbar.create({
  2. el: '#searchbar-autocomplete',
  3. customSearch: true,
  4. on: {
  5. search: function (query) {
  6. console.log(query);
  7. }
  8. }
  9. });
  10. var autocompleteSearchbar = app.autocomplete.create({
  11. openIn: 'dropdown',
  12. inputEl: '#searchbar-autocomplete input[type="search"]',
  13. dropdownPlaceholderText: 'Type "Apple"',
  14. source: function (query, render) {
  15. var results = [];
  16. if (query.length === 0) {
  17. render(results);
  18. return;
  19. }
  20. // Find matched items
  21. for (var i = 0; i < fruits.length; i++) {
  22. if (fruits[i].toLowerCase().indexOf(query.toLowerCase()) >= 0) results.push(fruits[i]);
  23. }
  24. // Render items by passing array with result items
  25. render(results);
  26. }
  27. })

Simple Standalone Autocomplete

  1. div class="list">
  2. <div class="block-header">Simple Standalone Autocomplete</div>
  3. <ul>
  4. <li>
  5. <a class="item-link item-content" href="#" id="autocomplete-standalone">
  6. <input type="hidden">
  7. <div class="item-inner">
  8. <div class="item-title">Favorite Fruite</div>
  9. <div class="item-after"></div>
  10. </div>
  11. </a>
  12. </li>
  13. </ul>
  14. </div>
  1. var autocompleteStandaloneSimple = app.autocomplete.create({
  2. openIn: 'page', //open in page
  3. openerEl: '#autocomplete-standalone', //link that opens autocomplete
  4. closeOnSelect: true, //go back after we select something
  5. source: function (query, render) {
  6. var results = [];
  7. if (query.length === 0) {
  8. render(results);
  9. return;
  10. }
  11. // Find matched items
  12. for (var i = 0; i < fruits.length; i++) {
  13. if (fruits[i].toLowerCase().indexOf(query.toLowerCase()) >= 0) results.push(fruits[i]);
  14. }
  15. // Render items by passing array with result items
  16. render(results);
  17. },
  18. on: {
  19. change: function (value) {
  20. console.log(value);
  21. // Add item text value to item-after
  22. $$('#autocomplete-standalone').find('.item-after').text(value[0]);
  23. // Add item value to input value
  24. $$('#autocomplete-standalone').find('input').val(value[0]);
  25. },
  26. },
  27. });

Popup Autocomplete

  1. <div class="list">
  2. <div class="block-header">Popup Autocomplete</div>
  3. <ul>
  4. <li>
  5. <a class="item-link item-content" href="#" id="autocomplete-standalone-popup">
  6. <input type="hidden">
  7. <div class="item-inner">
  8. <div class="item-title">Favorite Fruite</div>
  9. <div class="item-after"></div>
  10. </div>
  11. </a>
  12. </li>
  13. </ul>
  14. </div>
  1. var autocompleteStandalonePopup = app.autocomplete.create({
  2. openIn: 'popup', //open in page
  3. openerEl: '#autocomplete-standalone-popup', //link that opens autocomplete
  4. closeOnSelect: true, //go back after we select something
  5. source: function (query, render) {
  6. var results = [];
  7. if (query.length === 0) {
  8. render(results);
  9. return;
  10. }
  11. // Find matched items
  12. for (var i = 0; i < fruits.length; i++) {
  13. if (fruits[i].toLowerCase().indexOf(query.toLowerCase()) >= 0) results.push(fruits[i]);
  14. }
  15. // Render items by passing array with result items
  16. render(results);
  17. },
  18. on: {
  19. change: function (value) {
  20. // Add item text value to item-after
  21. $$('#autocomplete-standalone-popup').find('.item-after').text(value[0]);
  22. // Add item value to input value
  23. $$('#autocomplete-standalone-popup').find('input').val(value[0]);
  24. },
  25. },
  26. });

Multiple Values

  1. <div class="list">
  2. <div class="block-header">Multiple Values</div>
  3. <ul>
  4. <li>
  5. <a class="item-link item-content" href="#" id="autocomplete-standalone-multiple">
  6. <input type="hidden">
  7. <div class="item-inner">
  8. <div class="item-title">Favorite Fruite</div>
  9. <div class="item-after"></div>
  10. </div>
  11. </a>
  12. </li>
  13. </ul>
  14. </div>
  1. var autocompleteStandaloneMultiple = app.autocomplete.create({
  2. openIn: 'page', //open in page
  3. openerEl: '#autocomplete-standalone-multiple', //link that opens autocomplete
  4. multiple: true, //allow multiple values
  5. source: function (query, render) {
  6. var autocomplete = this;
  7. var results = [];
  8. if (query.length === 0) {
  9. render(results);
  10. return;
  11. }
  12. // Find matched items
  13. for (var i = 0; i < fruits.length; i++) {
  14. if (fruits[i].toLowerCase().indexOf(query.toLowerCase()) >= 0) results.push(fruits[i]);
  15. }
  16. // Render items by passing array with result items
  17. render(results);
  18. },
  19. on: {
  20. change: function (value) {
  21. // Add item text value to item-after
  22. $$('#autocomplete-standalone-multiple').find('.item-after').text(value.join(', '));
  23. // Add item value to input value
  24. $$('#autocomplete-standalone-multiple').find('input').val(value.join(', '));
  25. }
  26. }
  27. });

Standalone With Ajax-Data

  1. <div class="list">
  2. <div class="block-header">With Ajax-Data</div>
  3. <ul>
  4. <li>
  5. <a class="item-link item-content" href="#" id="autocomplete-standalone-ajax">
  6. <input type="hidden">
  7. <div class="item-inner">
  8. <div class="item-title">Language</div>
  9. <div class="item-after"></div>
  10. </div>
  11. </a>
  12. </li>
  13. </ul>
  14. </div>
  1. var autocompleteStandaloneAjax = app.autocomplete.create({
  2. openIn: 'page', //open in page
  3. openerEl: '#autocomplete-standalone-ajax', //link that opens autocomplete
  4. multiple: true, //allow multiple values
  5. valueProperty: 'id', //object's "value" property name
  6. textProperty: 'name', //object's "text" property name
  7. limit: 50,
  8. preloader: true, //enable preloader
  9. source: function (query, render) {
  10. var autocomplete = this;
  11. var results = [];
  12. if (query.length === 0) {
  13. render(results);
  14. return;
  15. }
  16. // Show Preloader
  17. autocomplete.preloaderShow();
  18. // Do Ajax request to Autocomplete data
  19. app.request({
  20. url: 'autocomplete-languages.json',
  21. method: 'GET',
  22. dataType: 'json',
  23. //send "query" to server. Useful in case you generate response dynamically
  24. data: {
  25. query: query
  26. },
  27. success: function (data) {
  28. // Find matched items
  29. for (var i = 0; i < data.length; i++) {
  30. if (data[i].name.toLowerCase().indexOf(query.toLowerCase()) >= 0) results.push(data[i]);
  31. }
  32. // Hide Preoloader
  33. autocomplete.preloaderHide();
  34. // Render items by passing array with result items
  35. render(results);
  36. }
  37. });
  38. },
  39. on: {
  40. change: function (value) {
  41. var itemText = [],
  42. inputValue = [];
  43. for (var i = 0; i < value.length; i++) {
  44. itemText.push(value[i].name);
  45. inputValue.push(value[i].id);
  46. }
  47. // Add item text value to item-after
  48. $$('#autocomplete-standalone-ajax').find('.item-after').text(itemText.join(', '));
  49. // Add item value to input value
  50. $$('#autocomplete-standalone-ajax').find('input').val(inputValue.join(', '));
  51. },
  52. },
  53. });