9

Uzak URL'imi giriş değerinin son kısmı ile aramaya çalışıyorum. Açılan öğe seçildiğindeBootstrap 3 typeahead.js - typeahead val dosyasının bir parçası tarafından sorgulama

enter image description here

hattı

enter image description here

herhangi bir fikir sonuna kadar yeni bir değer katmak,

$('#typeahead').typeahead({ 
    remote: { 
     url: '/ajax/tags/get/?name=%QUERY', 
     replace: function (url, query) { 
      var last = query.split(','); 
      last = $.trim(last[last.length-1]); 
      return url.replace('%QUERY', last); 
     } 
    }, 
    limit : 10 
}); 

ve: ben böyle bir şey yapmak istiyorum Bu iş nasıl yapılır? "Son bölümü ile sorgu" sizin için çalıştığını varsayarsak

+0

c Ayrıca, ilk önce girişinizdeki tüm virgülle ayrılmış değerleri almanız ve bunları elde ettiğiniz veri kümesinden kaldırmanız gerektiğine de dikkat edin. Yinelenen izinleri planlamadığınız sürece öneriler listesini göstermek için son giriş değerini kullanmadan önce. Büyük olasılıkla otomatik tamamlamayı/ipucunu da devre dışı bırakmanız gerekecek mi? Tahminimden bu yana girişinizi etkileyecektir. – Pricey

cevap

0

Bootstrap 3 için Bootstrap-3-Typeahead'u kullanabilirsiniz.

updater ve matcher işlevlerinin üzerine yazmak zorunda kalacaksınız.

updater: function (item) { 
     return this.$element.val().replace(new RegExp(this.query + '$'),'') + item + ','; 
    } 

(eşleştirebiliriniz son bir oluşum: JavaScript: replace last occurrence of text in a string)

matcher: function (item) { 
     var last = this.query.split(','); 
     this.query = $.trim(last[last.length-1]); 

     if(this.query.length) return ~item.toLowerCase().indexOf(this.query.toLowerCase()); 
} 

sizin update için şu kodu kullanın: aşağıdaki gibi matcher fonksiyonu için size değiştirmek işlevinden kodu kullanabilirsiniz Komple örnek:

$('.typeahead').typeahead (
{ 
items: 4, 
source: function (query, process) { 
    states = []; 
    map = {}; 


    var data = [ 
     {"stateCode": "CA", "stateName": "California"}, 
     {"stateCode": "AZ", "stateName": "Arizona"}, 
     {"stateCode": "NY", "stateName": "New York"}, 
     {"stateCode": "NV", "stateName": "Nevada"}, 
     {"stateCode": "OH", "stateName": "Ohio"} 
    ]; 

    $.each(data, function (i, state) { 
     map[state.stateName] = state; 
     states.push(state.stateName); 
    }); 

    process(states); 

} 

    , updater: function (item) { 
     return this.$element.val().replace(new RegExp(this.query + '$'),'') + item + ','; 
    } 
    , matcher: function (item) { 
     var last = this.query.split(','); 
     this.query = $.trim(last[last.length-1]); 

     if(this.query.length) return ~item.toLowerCase().indexOf(this.query.toLowerCase()); 
    } 
    } 

); 
0

, istediğini yapmak value ve title uygun olan verilerle ilgili bir dizi doldurmak ve geri dönmek için filter: function(response) {...} kullanabilirsiniz.

İlgili konular