2012-07-22 11 views
5

Sono a questo punto :)Come posso cercare oggetti su più proprietà (knockout.js + twitter bootstrap typeahead)?

http://blogs.msdn.com/b/rebond/archive/2012/07/18/knockout-js-binding-for-bootstrap-typeahead-plugin.aspx

// Bootstrap.Typeahead binding: presently requires custom version from gist: https://gist.github.com/1866577. 
// Use like so: data-bind="typeahead: { target: selectedNamespace, source: namespaces }" 
ko.bindingHandlers.typeahead = { 
    init: function(element, valueAccessor) { 
    var binding = this; 
    var elem = $(element); 
    var value = valueAccessor(); 

    // Setup Bootstrap Typeahead for this element. 
    elem.typeahead(
    { 
    source: function() { return ko.utils.unwrapObservable(value.source); }, 
    onselect: function(val) { value.target(val); } 
    }); 

// Set the value of the target when the field is blurred. 
elem.blur(function() { value.target(elem.val()); }); 
    }, 
    update: function(element, valueAccessor) { 
var elem = $(element); 
var value = valueAccessor(); 
elem.val(value.target()); 
    } 
}; 

ho classe X con 4 proprietà.

Voglio cercare X object array sulle sue 3 proprietà. (L'altra proprietà è id)

Qualche idea?

+0

penso che bisogna implementare matcher personalizzato per typeahead. –

risposta

4

nella chiamata .typeahead, passare un matcher function che guardare le altre proprietà:

elem.typeahead({ 
    source: function() { return ko.utils.unwrapObservable(value.source); }, 
    onselect: function(val) { value.target(val); }, 
    matcher: function(item) { 
     // Check if it matches Foo, Bar, or Baz properties. 
     return item.Foo.indexOf(this.query) >= 0 || item.Bar.indexOf(this.query) >= 0 || item.Baz.indexOf(this.query) >= 0; 
    } 
}); 
+0

Giuda, posso supporre che funzioni con il Bootstrap 2.2 appena estratto dalla confezione? –

+0

Non l'ho provato su 2.2, ma non vedo alcun motivo per cui non funzionerebbe fuori dalla scatola. Stai vedendo qualcosa di diverso? –

Problemi correlati