2012-07-22 6 views
5

estoy en este momento :)¿Cómo puedo buscar objetos en varias propiedades (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()); 
    } 
}; 

tengo clase X con 4 propiedades.

Quiero buscar matriz X objeto sobre sus 3 propiedades. (Otra propiedad es id)

Alguna idea?

+0

creo que hay que poner en práctica matcher personalizado para typeahead. –

Respuesta

4

En su llamada .typeahead, pase un matcher function que se verá en sus otras propiedades:

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

Judah, ¿puedo suponer que funciona con el Bootstrap 2.2 de fábrica? –

+0

No lo he probado en 2.2, pero no veo ninguna razón por la que no funcione de la caja. ¿Estás viendo algo diferente? –

Cuestiones relacionadas