7

Se si dà un'occhiata al seguente JS: (Live: http://jsfiddle.net/RyanWalters/dE6T3/2/)Interfaccia utente jQuery: completamento automatico: come faccio a cercare più valori all'interno di un array?

var projects = [ 
    { 
     value: "jquery", 
     label: "jQuery", 
     desc: "the write less, do more, JavaScript library", 
     icon: "jquery_32x32.png" 
    }, 
    { 
     value: "jquery-ui", 
     label: "jQuery UI", 
     desc: "the official user interface library for jQuery", 
     icon: "jqueryui_32x32.png" 
    }, 
    { 
     value: "sizzlejs", 
     label: "Sizzle JS", 
     desc: "a pure-JavaScript CSS selector engine", 
     icon: "sizzlejs_32x32.png" 
    } 
]; 


$("#autocomplete").autocomplete({ 
    source: function(request, response){ 
     var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i"); 
     response($.grep(projects, function(value) { 
      value = value.value || value.desc || value.icon; 
      return matcher.test(value); 
     })); 
    } 
}); 

che sto cercando di fare il completamento automatico cercare i value, desc, e icon campi nella matrice projects. Tuttavia, quando inserisco i valori nella casella di ricerca, posso cercare solo nel campo value. I campi desc e icon vengono completamente ignorati.

Come posso fare in modo che possa cercare il testo in uno dei tre campi?

+3

potrebbe non solo "ritorno matcher.test (value.value) || matcher.test (value.desc) || ​​matcher.test (value.icon) ;" ? – dinjas

risposta

11
value = value.value || value.desc || value.icon; 

Questo imposterà il valore al 1 ° "thuthy" value (che sarà sempre value.value).

provare qualcosa di simile:

response($.grep(projects, function(value) { 
    return matcher.test(value.value) || matcher.test(value.desc) || matcher.test(value.icon); 
})); 
+1

Ha funzionato come un fascino, grazie! – Ryan

+0

@Ryan: Prego :-) –

Problemi correlati