Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I search objects over multiple properties (knockout.js+ twitter bootstrap typeahead)?

I'm at this point :)

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());
  }
 };

I have X class with 4 properties.

I want to search X object array over its 3 properties. (Other property is id)

Any idea?

like image 203
Oguz Karadenizli Avatar asked Oct 08 '22 04:10

Oguz Karadenizli


1 Answers

In your .typeahead call, pass a matcher function that will look at your other properties:

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;
   }
});
like image 86
Judah Gabriel Himango Avatar answered Oct 12 '22 19:10

Judah Gabriel Himango