ajax - Bootstrap typeahead suggestions replaced when navigation -


I'm using Bootstrap Typeahead to suggest some search results. The results are returned from an Ajax resource, and since this resource is delayed, I am having an unfortunate effect. Example: If you type 4 letter words, then suggestions will appear after two letters. I can move keys up / down through results, but sudden suggestions will be reloaded because the last request has ended.

Is there a way to "cancel" any remaining, if the user is currently using it to go through the suggestions key / up key?

  ('# query'). Taipahed ({item: 4 Source: function (query process) {map = {}; $ getJSON ( '/ app_dev.php / ajax / autosuggest /' + query, function (data) {cars = []; $. Push (vehicle.full);}); Process (vehicle);});}, Updater: Function (item) {each] (Data, Function (i, Vehicle) {Map [vehicle.full] = Vehicles, / There is some item where the item is selected}, Highlighter: function (item) {return item;}, micht: function (item) {returns true;}});    

I think will satisfy your needs (Its

There is no easy way to cancel the response by any delay, but you can expand the typed (without modifying bootstrap.js)

concept Keydown , detect if the event is KEY_UP or KEY_DOWN , set a flag is_browsing , and then interrupt Do process if is_browsing is correct (that is, if the user hitted KEY_UP or KEY_DOWN and no other key is later). < p> type expansion type :

  // original function object Save var _superTypeahead = $ .fn.typeahead; // a new flag $ .extend (_superTypeahead. Defaults, Add is_browsing as {is_browsing: False}); // to expand a new manufacturer bridesmaid Typeahead = function (element, options) to {_superTypeahead.Constructor.apply (this argument)} // prototype and add A _super function Typeahead.proto type = $ .extend ({}, _superTypeahead.Constructor .prototype, {Manufacturer: Typeahead, _super: function () {var args = $ .makeArray (logic) Bootstrap core _superTypeahead.Constructor.prototype call // [args.shift ( )] Applied (this, args)} // Ovrdraid Taipahed original Kunjeedaun, Kunjeedaun: function (e) {this._super ( 'keydown', e) this.options.is_browsing = ($ .inArray (e.keyCode, [40,38]) & gt; -1)} / / Override process, if the user is browsing, then stop: process (function) (item) {if (this.options.is_browsing) this return._super ('process', item)}}; // New Maker $ .fn.typeahead = $ .extend (function) (option) {var arg = $ .makeArray (argument), option = args.shift () // This is executed with each element Year Start Override . modal () return called Thiskeach (function () {var $ this = $ (this) var data = $ this.data ( 'Typeahead'), option = $ .extend ({}, _superTypeahead.defaults, $ This .data (), type of option == 'object' and option if (! Data) {$ this.data ('typeahead', (data = new typedhead (this, option)) if (optionof Option) == 'string') {data [options] .apply (data, args)}});}, $ .fn.typea Head);   

This typeahead extension can be placed anywhere, for example & lt; Script type = "text / javascript" & gt; -section

Testing Extensions :

  & lt; Input type = "text" id = "test" name = "test" placeholder = "write some text" data-provide = "typeahead" & gt; & Lt; Script type = "text / javascript" & gt; $ (Document) .ready (function () {var url = 'typeahead.php'; $ ("# test"). TypeHead ({items: 10, source: function (query, process) {return $ .get (url) ), {Query: query}, function (data) {return process (data option);})}}}};}); & Lt; / Script & gt;   

A "serverside" PHP script that provides many random options with mandatory delays, typeahead.php:

  & lt;? Header ('content-type: application / jason'); $ JSON = ''; Sleep (3); Execution of delay in 3 seconds for $ 3 ($ count = 0; $ count & lt; 30000; $ count ++) {if ($ JSON! = '') $ JSON = ','; // Make random wire $ s = str_shuffle ("abcdefghijklmnopq"); $ JSON = '' '$ s.' "'.} $ JSON =' {" Option ": ['. $. JSON.']} '; $ JSON echo;? & Gt;   

This really works for me It seems, but I can not make sure that it will work in your case. If you have had success or not, then let me now.

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -