Limit the number of rows of autocomplete result , and match strings with starting letters only -
i'm using jquery autocomplete in project,
<div class="ui-widget"> <label for="tags">tags: </label> <input id="tags"> </div>
json file
[ { "value": "saree", "url": "/collection/saree" }, { "value": "lehangas", "url": "/collection/lehangas" }, { "value": "dresses", "url": "/collection/dresses" }, { "value": "tunics", "url": "/collection/tunics" }, { "value": "kurtis", "url": "/collection/kurtis" }, { "value": "blouses", "url": "/collection/blouses" }, { "value": "duppattas", "url": "/collection/duppattas" }, { "value": "shawls", "url": "/collection/shawls" }, { "value": "plazos", "url": "/collection/plazos" }, { "value": "skirts", "url": "/collection/skirts" }, { "value": "patiala", "url": "/collection/patiala" } ]
my js file:
$( function (){ $( "#tags" ).autocomplete({ source: "/static/admin/json/search.json", select: function (event, ui) { window.location = ui.item.url; } }); });
it displays results no matter character enter. want string matched according first letter , following letters. , also, want limit number of rows displayed 10.
so , please 1 me this. in advance.
html
<div class="ui-widget"> <label for="tags">tags: </label> <input id="tags"> </div>
here have no idea whether getting json data in js file or not , in case json data available in js file , accessing json data in own way hope can manage things.
js file
var data = [ { "value": "saree", "url": "/collection/saree" }, { "value": "lehangas", "url": "/collection/lehangas" }, { "value": "dresses", "url": "/collection/dresses" }, { "value": "tunics", "url": "/collection/tunics" }, { "value": "kurtis", "url": "/collection/kurtis" }, { "value": "blouses", "url": "/collection/blouses" }, { "value": "duppattas", "url": "/collection/duppattas" }, { "value": "shawls", "url": "/collection/shawls" }, { "value": "plazos", "url": "/collection/plazos" }, { "value": "skirts", "url": "/collection/skirts" }, { "value": "patiala", "url": "/collection/patiala" } ] $( function (){ $( "#tags" ).autocomplete({ source: function(request, response){ var lengthofsearch= request.term.length; var arr = jquery.map(data, function( element, index ) { if(element.value.substr(0,lengthofsearch).tolowercase() === request.term.tolowercase()){ return element; } }); response(arr.slice(0,10)); }, select: function (event, ui) { window.location = ui.item.url; } }); });
<link href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script> <div class="ui-widget"> <label for="tags">tags: </label> <input id="tags"> </div>
Comments
Post a Comment