A Plugin to make search friendly
This plugin is useful for search
- KnockoutJS (2.2.0+) - http://knockoutjs.com/
- Knockout Mapping Plugin
Create a variable and pass the observable array to database object
var myobject = ko.search.setData(self.Books()) self.Search(myobject)
Now the object is available. we can easily search.
Three types of inputs can be provided.
- Observable Array
- Array of objects
- JSON String of array of objects
var myobject = ko.search.setData(self.Books()) // obervableArray var myobject = ko.search.setData(books) // Array of objects var myobject = ko.search.setData(booksJson) // json string of array of objects
Search results are easy to obtain in three ways.
- Observable Array
- Array of objects
- JSON String
self.Search().get() // returns observable array self.Search().get(true) // returns array of objects self.Search().stringify() // returns array of objects in json string
Note : If you pass array of objects as parameter you will get array of objects instead of observable array in first example using .get()
Note : I will be using `.stringify()` in the later example but you can choose any
self.Search().get() // Or self.Search().get(true) // Or self.Search().stringify()
self.Search().first().stringify()
self.Search().last().stringify()
self.Search().start(2).stringify()
self.Search().limit(2).stringify()
self.Search().start(8).limit(2).stringify()
self.Search() .filter({Price:200}) .stringify()
self.Search() .filter({Price:200,Rate:5}) .stringify()
self.Search() .filter({Rate:[4,5]}) .stringify()
self.Search() .filter({Price:200,Id:[1,16]}) .stringify() // Or self.Search() .filter({Id:[1,16],Price:200}) .stringify()
self.Search() .filter({Rate:{value:4,condition:'>'}}) .stringify()
self.Search() .filter({Rate:[4,5]}) .filter({Id:{value:8,condition:'>'}}) .stringify()
self.Search() .filter({Price:{value:200,condition:'>'}}) .filter({Id:{value:15,condition:'<'}}) .stringify()
self.Search() .filter({Price:{value:200,condition:'>'},Id:{value:15,condition:'<'}}) .stringify() self.Search() .filter({Price:{value:200,condition:'>'}},{Id:{value:15,condition:'<'}}) .stringify()
self.Search().select(['Name','Price']).stringify()
If you need any column in filter you must use it in select. With filter, use select after filter for fast search
self.Search() .filter({Price:{value:200,condition:'>'}}) .select(['Name','Price']) .stringify()
Note : If you need any column in filter you must use it in select
self.Search().max('Rate').stringify()
self.Search().max('Rate').index(2).stringify()
self.Search().min('Rate').stringify()
self.Search().min('Rate').index(2).stringify()
self.Search().order('Rate').stringify() self.Search().order('Rate',true).stringify()
Sort Records with multiple properties, Pass true as second parameter if you want the results reversed
self.Search().order('Rate').order('Id').stringify() self.Search().order('Rate',true).order('Id',true).stringify()
self.Search() .callback(function(){ self.db().userData().push({Id:21,Rate:8,Price:400,Name:'Myths'}) }) .stringify()
self.Search() .map(function(item){ item.Action = true }) .stringify()
var func = [ function(item){return item.Action = item.Price > 200}, function(item){item.Level = 'High'}, ] self.Search().each(func).stringify()```html self.Search() .supplant('{Name}') .stringify() ``` ```html item.template = 'History' ```