2013-10-05 21 views
5

Ho bisogno di una semplice funzionalità di ricerca automatica, ma anche di consentire agli utenti di digitare più di un valore. Sto usando il widget di completamento automatico dell'interfaccia utente di jQuery (http://jqueryui.com/autocomplete/) e finora ho impostato l'origine per cercare solo la prima lettera nei suggerimenti. Quello che vorrei aggiungere ora è la possibilità per gli utenti di cercare più oggetti dalla stessa casella di testo. (vale a dire dopo che vengono visualizzati nuovamente i suggerimenti virgola)jQuery UI completamento automatico di più valori nella casella di testo

Ho cercato di cercare su come ciò potrebbe essere fatto. L'unica cosa che sono riuscito a trovare è un'opzione che potrebbe essere aggiunta multiple: true (http://forum.jquery.com/topic/multiple-values-with-autocomplete). Il fatto è che non è nemmeno elencato nella documentazione, quindi non so se l'opzione è cambiata o non esiste più.

Questo è il mio codice:

var items = [ 'France', 'Italy', 'Malta', 'England', 
     'Australia', 'Spain', 'Scotland' ]; 

    $(document).ready(function() { 
     $('#search').autocomplete({ 
      source: function (req, responseFn) { 
       var re = $.ui.autocomplete.escapeRegex(req.term); 
       var matcher = new RegExp('^' + re, 'i'); 
       var a = $.grep(items, function (item, index) { 
        return matcher.test(item); 
       }); 
       responseFn(a); 
      } 
     }); 
    }); 

Quello che ho cercato:

var items = [ 'France', 'Italy', 'Malta', 'England', 
     'Australia', 'Spain', 'Scotland' ]; 

    $(document).ready(function() { 
     $('#search').autocomplete({ 
      source: function (req, responseFn) { 
       var re = $.ui.autocomplete.escapeRegex(req.term); 
       var matcher = new RegExp('^' + re, 'i'); 
       var a = $.grep(items, function (item, index) { 
        return matcher.test(item); 
       }); 
       responseFn(a); 
      }, 
      multiple: true 
     }); 
    }); 

risposta

3

Per risolvere il problema di più stringhe nello stesso casella di testo E includere un'espressione regolare per mostrare solo suggerimenti corrispondenti all'inizio della stringa ho fatto quanto segue:

$('#search').autocomplete({ 
     minLength: 1, 
     source: function (request, response) { 
      var term = request.term; 

      // substring of new string (only when a comma is in string) 
      if (term.indexOf(', ') > 0) { 
       var index = term.lastIndexOf(', '); 
       term = term.substring(index + 2); 
      } 

      // regex to match string entered with start of suggestion strings 
      var re = $.ui.autocomplete.escapeRegex(term); 
      var matcher = new RegExp('^' + re, 'i'); 
      var regex_validated_array = $.grep(items, function (item, index) { 
       return matcher.test(item); 
      }); 

      // pass array `regex_validated_array ` to the response and 
      // `extractLast()` which takes care of the comma separation 

      response($.ui.autocomplete.filter(regex_validated_array, 
       extractLast(term))); 
     }, 
     focus: function() { 
      return false; 
     }, 
     select: function (event, ui) { 
      var terms = split(this.value); 
      terms.pop(); 
      terms.push(ui.item.value); 
      terms.push(''); 
      this.value = terms.join(', '); 
      return false; 
     } 
    }); 

    function split(val) { 
     return val.split(/,\s*/); 
    } 

    function extractLast(term) { 
     return split(term).pop(); 
    } 
9

Prova questo:

function split(val) { 
    return val.split(/,\s*/); 
    } 

    function extractLast(term) { 
    return split(term).pop(); 
    } 

    $("#search") 
     .autocomplete({ 
      minLength: 0, 
      source: function(request, response) { 
       response($.ui.autocomplete.filter(
        items, extractLast(request.term))); 
      }, 
      focus: function() { 
       return false; 
      }, 
      select: function(event, ui) { 
       var terms = split(this.value); 
       terms.pop(); 
       terms.push(ui.item.value); 
       terms.push(""); 
       this.value = terms.join(", "); 
       return false; 
      } 
     }); 

VEDI DEMO

+0

Impressionante che funziona! :) L'unico problema che ho è che non riesco a farlo funzionare con il RegEx che ho nel codice sopra. In questo momento sta ignorando RegEx –

+1

se è corretto, quindi si accetta questa risposta –

+0

È corretto, tuttavia non funziona con il RegEx che ho. Mi piacerebbe che i suggerimenti fossero abbinati alla prima lettera della ricerca non "lettera per lettera". Questo è l'unico problema che ho. Se guardi il codice che ho pubblicato ho un RegEx per far fronte a questo. Anche se il tuo codice funziona, quando aggiungo il codice RegEx ho i suggerimenti sopra riportati solo per la prima parola. –

Problemi correlati