You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+53-61Lines changed: 53 additions & 61 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,17 +1,15 @@
1
1
# Filter.js
2
2
3
-
Filter.js is client-side JSON objects filter and render html elements.
4
-
Multiple filter criteria can be specified and used in conjunction with
5
-
each other.
3
+
Filter.js is client-side JSON objects filter which can render html elements. Multiple filter criteria can be specified and used in conjunction with each other.
6
4
7
5
## Usage
8
6
9
-
Basic requirement to implement filtering using filter.js are JSON data, View template and filter criteria.
7
+
Basic requirement to implement filtering using filter.js are JSON data, a 'View' template and a filter criteria.
10
8
11
9
### Filter Initialisation
12
10
13
-
It takes three arguments one is movies, second is container in which html element going to append, third one is options. Options must have template element selector.
14
-
Others are in options like criteria, callbacks, search.
11
+
It takes three arguments one is movies, second is 'container' in which html elements are to be to appended and the third one is options.
12
+
You can set options such as template, criteria, callbacks and search but only `template` is compulsory.
15
13
16
14
```javascript
17
15
varFJS=FilterJS(movies, '#movies', {
@@ -24,12 +22,13 @@ var FJS = FilterJS(movies, '#movies', {
24
22
});
25
23
```
26
24
27
-
To append in each item in different container use option `appendToContainer`. This option is a function with two arguments, one is html element content and second is record object.
25
+
To append each item in different container use option `appendToContainer`.
26
+
This option is a function with two arguments, one is html element content and second is record object.
28
27
29
28
```javascript
30
29
31
30
//This will append elements to specific year.
32
-
varappendFn=function(html_ele, record){
31
+
varappendFn=function(html_ele, record){
33
32
$("#"+record.year).append(html_ele);
34
33
}
35
34
@@ -41,7 +40,7 @@ var FJS = FilterJS(movies, '#movies', {
41
40
42
41
### JSON data
43
42
44
-
Capture the JSON data (maybe using @movies.to_json). i.e
43
+
Capture the JSON data (maybe using @movies.to_json).
45
44
46
45
```javascript
47
46
var movies = [
@@ -69,7 +68,7 @@ Capture the JSON data (maybe using @movies.to_json). i.e
69
68
70
69
### View
71
70
72
-
Rendering JSON objects requires a view template. In filter.js micro-templating module inspired by Underscore.js.
71
+
Rendering JSON objects requires a view template. The micro-templating module in filter.js is inspired by Underscore.js.
73
72
74
73
```javascript
75
74
<script id="movie-template" type="text/html">
@@ -101,13 +100,14 @@ Rendering JSON objects requires a view template. In filter.js micro-templating m
101
100
102
101
### Filter Criteria
103
102
104
-
It required two mandatory options are `field` which is name of any property from JSON data and other is HTML `ele` element on which filter will be trigger by click,change etc events.
105
-
Other options are filter`type`, `event` and `selector`.
103
+
The two mandatory options required are `field` which is name of any property from JSON data and other is HTML `ele` element on which filter will be triggered by an event(e.g. click,change, etc.).
104
+
Other filter options are `type`, `event` and `selector`.
106
105
107
-
- filter `type`, by default it is equal but if you want to search in range you can set it `range`. For `range` html element value should be in format of `val1-val2`. i.e `100-200`.
108
-
-`delimiter`, by default hyphen '-' is used as range separator `val1-val2`. If you want to use a different separator (if data contains hyphen e.g: ' 2012-02-02 ') it can be specified using `delimiter: ','` and html element value should be in format `val1<delimiter>val2`. i.e.`2012-02-02,2015-02-02`.
109
-
-`event` by default for checkbox, radio button is `click`, for text input, select box is `change`.
110
-
-`selector` by default for checkbox and radio button is `:checked`, for input field `input` and for select box is `select`.'#genre_criteria input:checkbox' will collect the checkboxes values in html element with `id="genre_criteria"`
106
+
- filter `type`: by default it is equal but if you want to search within a range you can set it to `range`.
107
+
For `range`, html element value should be in format of `val1-val2`(e.g. `100-200`).
108
+
By default hyphen '-' is used as `delimiter` or range separator `val1-val2`. If you want to use a different separator (if data contains hyphen e.g: '2012-02-02') it can be specified using `delimiter: ','` and html element value should be in format `val1<delimiter>val2`. i.e.`2012-02-02,2015-02-02`.
109
+
-`event` by default for checkbox and radio button is `click` and for text input and select box it is `change`.
110
+
-`selector` by default for checkbox and radio button is `:checked`, for input field is `input` and for select box is `select`. '#genre_criteria input:checkbox' will collect the checkboxes values in html element with `id="genre_criteria"`
111
111
-`all` option : if selected values of specific filter criteria contains `all` option value then all record selected for that criteria.
112
112
113
113
There are two way to add criteria. One is add at time of filter object initialisation and other is add when required.
@@ -136,20 +136,19 @@ There are two way to add criteria. One is add at time of filter object initialis
136
136
137
137
More detail for `range` filter. It is expected to set ranges as values like '20-30'
For nested field selection. In below object to select filter on name `field` option value would be `detail.name`, for city `detail.address.city`.
145
+
**For nested field selection**: In the below object, to select filter on name `field` option value would be `detail.name` and for city `detail.address.city`.
146
146
147
147
JSON object:
148
148
149
149
```json
150
-
151
150
{
152
-
detail: { name:'Jiren', address: {city:'Pune'} }
151
+
detail: { name:'Jiren', address: {city:'Pune'} }
153
152
}
154
153
```
155
154
@@ -165,40 +164,38 @@ fjs.removeCriteria('year')
165
164
166
165
Define callback in settings. Callbacks execute on different events.
167
166
168
-
- `beforeAddRecords` : Trigger before adding records to filter.
169
-
- `afterAddRecords`
170
-
- `beforeRender` : Trigger before rendering going to call.
171
-
- `beforeRecordRender` : Trigger for each JSON object record at time of rendering.
172
-
- `afterFilter` : Trigger after filtering event.
167
+
- `beforeAddRecords` : Triggered before adding records to filter.
168
+
- `afterAddRecords` : Triggered after all records are added.
169
+
- `beforeRender`: Triggered before rendering going to call.
170
+
- `beforeRecordRender` : Triggered for each JSON object record at time of rendering.
171
+
- `afterFilter` : Triggered after filtering event.
173
172
174
173
i.e.,
175
174
176
175
```javascript
177
-
178
-
var filter_callbacks = {
179
-
beforeAddRecords:function(records){
180
-
// Process new JSON data records.
181
-
// i.e Process data before adding to filter while streaming.
182
-
},
183
-
afterAddRecords:function(records){
184
-
// i.e Update google markers or update sorting.
185
-
},
186
-
beforeRender:function(records){
176
+
var filter_callbacks = {
177
+
beforeAddRecords:function(records){
178
+
// Process new JSON data records.
179
+
// i.e Process data before adding to filter while streaming.
180
+
},
181
+
afterAddRecords:function(records){
182
+
// i.e Update google markers or update sorting.
183
+
},
184
+
beforeRender:function(records){
187
185
//
188
-
},
189
-
beforeRecordRender:function(record){
186
+
},
187
+
beforeRecordRender:function(record){
190
188
//i.e Add/Update record fields
191
-
},
192
-
afterFilter:function(result){
193
-
// i.e Update result counter, update google map markers.
194
-
}
195
-
};
189
+
},
190
+
afterFilter:function(result){
191
+
// i.e Update result counter, update google map markers.
192
+
}
193
+
};
196
194
```
197
195
198
196
#### Init Filter object with above callbacks
199
197
200
198
```javascript
201
-
202
199
var fjs =FilterJS(movies, '#movies', {
203
200
template:'#movie-template',
204
201
callbacks: filter_callbacks
@@ -212,41 +209,36 @@ var fjs = FilterJS(movies, '#movies', {
212
209
213
210
### Instant Search integration
214
211
215
-
For search needed textbox element selector. By default search will work on all JSON object fields. If needed search in particular fields then set `fields` option.
212
+
To enable search, add a textbox element and set the selector in options. By default search will work on all JSON object fields. If you want to search on some particular fields then set the`fields` option.
216
213
217
214
```javascript
215
+
// Init with search
216
+
FilterJS(movies, '#movies', {
217
+
template:'#movie-template',
218
+
search: { ele:'#searchbox' } // Search in all fields of JSON object.
219
+
}
218
220
219
-
// Init with search
220
-
FilterJS(movies, '#movies', {
221
-
template:'#movie-template',
222
-
search: {ele:'#searchbox'} // Search in all fields of JSON object.
0 commit comments