Jquery UI Autocomplete: search from multiple attributes of one array -
hi i'm trying jquery ui autocomplete widget work searches matches multiple attributes of array (not 1 default).
i've messed around example, i'm still unsure how solve this.
here's array format in script
var projects = [ { value: "jquery", label: "jquery", desc: "the write less, more, javascript library", other: "9834275 9847598023 753425828975340 82974598823" }, { value: "jquery-ui", label: "jquery ui", desc: "the official user interface library jquery", other: "98 83475 9358 949078 8 40287089754 345 2345" }, { value: "sizzlejs", label: "sizzle js", desc: "a pure-javascript css selector engine", other: "49857 2389442 573489057 89024375 928037890" } what i'm seeking if type "write", first element should pop in autocomplete, if type "jq" first 2 elements should pop up.
according documentation:
array: array can used local data. there 2 supported formats:
an array of strings:
[ "choice1", "choice2" ]an array of objects label , value properties:
[ { label: "choice1", value: "value1" }, ... ]the label property displayed in suggestion menu. value inserted input element when user selects item. if 1 property specified, used both, e.g., if provide value properties, value used label.
how (hard)code source uses 2 labels (label , desc?) instead of 1 label?
(sorry i've searched many similar questions, aim @ multiple sources, not here since have 1 array .. it?)
autocomplete accepts third type of source, function, can provide data way see fit:
the third variation, callback, provides flexibility , can used connect data source autocomplete. callback gets 2 arguments:
- a request object, single term property, refers value in text input. example, if user enters "new yo" in city field, autocomplete term equal "new yo".
- a response callback, expects single argument: data suggest user. data should filtered based on provided term, , can in of formats described above simple local data. it's important when providing custom source callback handle errors during request. must call response callback if encounter error. ensures widget has correct state.
this means can write this
$( "#project" ).autocomplete({ source: function (request, response) { // request.term typed console.log(request.term); //call response array containing filtered data response([...]); } }); a simple solution problem:
function lightwell(request, response) { function hasmatch(s) { return s.tolowercase().indexof(request.term.tolowercase())!==-1; } var i, l, obj, matches = []; if (request.term==="") { response([]); return; } (i = 0, l = projects.length; i<l; i++) { obj = projects[i]; if (hasmatch(obj.label) || hasmatch(obj.desc)) { matches.push(obj); } } response(matches); } $( "#project").autocomplete({ source: lightwell }); and fiddle http://jsfiddle.net/fuzpn/5/
Comments
Post a Comment