/knockout-search

A plugin that makes search friendly for observable arrays in knockoutjs

Primary LanguageJavaScript

knockout-search-js

A Plugin to make search friendly

This plugin is useful for search

Dependencies:

Usage

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.

How to provide input

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    

How to obtain search results

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

Function Reference

Getting Records

Get All Records

	self.Search().get()  // Or     
	self.Search().get(true) // Or    
	self.Search().stringify()    

Get first record

	self.Search().first().stringify()

Get last record

	self.Search().last().stringify()

Get records in array format starting from index 2

	self.Search().start(2).stringify()

Get limited records in array format starting from 0 index

	self.Search().limit(2).stringify()

Get from specific index with limited records in array format

	self.Search().start(8).limit(2).stringify()

Searching Records

Simple codition to filter records

Search where Price = 200
	self.Search()
			.filter({Price:200})
			.stringify()    

Simple and codition to filter records

Search where Price = 200 and Rate = 5
	self.Search()
			.filter({Price:200,Rate:5})
			.stringify()    

Simple or codition to filter records

Search where Rate = 4 or Rate = 5
	self.Search()
			.filter({Rate:[4,5]})
			.stringify()    

And and Or codition to filter records

Search where Price = 200 and Id = 1 Or Id = 16
	self.Search()
			.filter({Price:200,Id:[1,16]})
			.stringify() // Or    
	self.Search()
			.filter({Id:[1,16],Price:200})
			.stringify()    

Filter with custom value

Search where Rate > 4
	self.Search()
			.filter({Rate:{value:4,condition:'>'}})
			.stringify()    

Filter Chaining

And and Or coditions can be used using chaining method
	self.Search()
			.filter({Rate:[4,5]})
			.filter({Id:{value:8,condition:'>'}})
			.stringify()    

Filter with multiple custom values

For multiple custom values only chaining method will work
	self.Search()
			.filter({Price:{value:200,condition:'>'}})
			.filter({Id:{value:15,condition:'<'}})
			.stringify()    
For multiple custom values this will not work and bring undesired results
	self.Search()
			.filter({Price:{value:200,condition:'>'},Id:{value:15,condition:'<'}})
			.stringify()    
	self.Search()
			.filter({Price:{value:200,condition:'>'}},{Id:{value:15,condition:'<'}})
			.stringify()   		

Selecting Record Columns

Select specific columns. Note the Order

Select Only Name and Price columns
	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

Get Records with Max Value

Get records with MAX Rate
	self.Search().max('Rate').stringify()    
Get only second record with MAX Rate. Although you can use start() and limit()
	self.Search().max('Rate').index(2).stringify()    

Get Records with Min Value

Get records with Min Rate
	self.Search().min('Rate').stringify()    
Get only second record with MIN Rate. Although you can use start() and limit()
	self.Search().min('Rate').index(2).stringify()    

Sort Records

Sort Records with property, Pass true as second parameter if you want the results reversed
	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()    

Applying Functions

Apply callback

userData() is the data you provided
	self.Search()
		.callback(function(){
			self.db().userData().push({Id:21,Rate:8,Price:400,Name:'Myths'})
		})
		.stringify()    

Apply Map Function

map() function will be applied on each object of data. Same like javascript map
	self.Search()
		.map(function(item){
			item.Action = true
		})
		.stringify()    

Apply multiple Functions

each() function will be applied on each object of data. Takes array of functions as parameter
	var func = [
			function(item){return item.Action = item.Price > 200},
			function(item){item.Level = 'High'},
	]
	self.Search().each(func).stringify()    

Tempalte Functions

supplant() function will be applied on each object of data. Takes html template string
```html self.Search() .supplant('{Name}') .stringify() ```
In each object a new property template will be added which will have html string
```html item.template = 'History' ```