i have autocomplete box (for purposes of example, since it's simple example) returns list including social security numbers. these have dashes in them readability. want modify autocomplete if put in "123456789" or "123-45-6789", find same entry in autocomplete, without having add both styles autocomplete source. i've been looking @ adding callback search, i'm drawing blank on how accomplish this.
if using search pulled ajax source, trim input server-side , return whatever results wanted. however, speed's sake, i've pre-loaded of data javascript, option's out.
basically, want know how trim dashes off autocomplete input before comparing stored data (and, preferably, comparing copy of stored data dashes trimmed). there's 0 documentation on how use search: option, i'm hoping can help.
one way provide function source option of autocomplete:
var ssn = ['123-45-6789', '333-44-5543', '111-34-9696', '555-99-5323']; $("#ssn").autocomplete({ source: function(request, response) { /* remove dashes search term: */ var term = request.term.replace(/-/g, ''); var matcher = new regexp($.ui.autocomplete.escaperegex(term), "i"); response($.map(ssn, function(el) { /* remove dashes source , compare term: */ if (matcher.test(el.replace(/-/g, ''))) { return el; } })); } }); here's what's going on:
the
sourceoption takesrequest,responseparameter. basically, manipulaterequest.term(replace dashes empty string) before comparing list of valid values.then, call
responsefunction correct results. example uses$.mapreturn list of matches, comparing term each item in list (without "-").
here's working example: http://jsfiddle.net/andrewwhitaker/r4szc/
Comments
Post a Comment