|
1 | 1 | /* |
2 | 2 | * Filter.js |
3 | | - * version: 1.2 (10/01/2012) |
| 3 | + * version: 1.3 (24/08/2012) |
4 | 4 | * |
5 | 5 | * Licensed under the MIT: |
6 | 6 | * http://www.opensource.org/licenses/mit-license.php |
|
18 | 18 | this.view = view; |
19 | 19 | this.parentNode = parentNode; |
20 | 20 | this.settings = settings || {}; |
| 21 | + this.last_result = null; |
21 | 22 |
|
22 | 23 | if (this.dataModel.constructor == Object){ |
23 | 24 | this.dataModel = [this.dataModel]; |
|
40 | 41 | return new _filterJS(dataModel, parentNode, view, settings); |
41 | 42 | }; |
42 | 43 |
|
43 | | - FilterJS.VERSION = 1.2; |
| 44 | + FilterJS.VERSION = 1.3; |
44 | 45 |
|
45 | 46 | //Register new html tag |
46 | 47 | FilterJS.registerHtmlElement = function(tag_name){ |
|
113 | 114 | bindEvents: function() { |
114 | 115 | var base = this; |
115 | 116 | base.settings.selector.forEach(function(selector, index) { |
116 | | - $(selector.element).live(selector.events, |
117 | | - function(e) { |
| 117 | + $(selector.element).live(selector.events, function(e) { |
118 | 118 | base.filter(); |
119 | 119 | }); |
120 | 120 | }) |
| 121 | + |
| 122 | + if(base.settings.search){ |
| 123 | + $(base.settings.search.input).live('keyup', function(e){ |
| 124 | + //var search_result = base.search(this, base.last_result || base.settings.all_objects); |
| 125 | + //base.hideShow(search_result); |
| 126 | + base.filter(); |
| 127 | + }); |
| 128 | + } |
121 | 129 | }, |
122 | 130 |
|
123 | 131 | //Unbind fileter events |
|
153 | 161 |
|
154 | 162 | if(select_none && base.settings.and_filter_on) filter_out = []; |
155 | 163 |
|
| 164 | + //Search |
| 165 | + if(base.settings.search){ |
| 166 | + filter_out = base.search(base.settings.search.input, filter_out); |
| 167 | + } |
156 | 168 | base.hideShow(filter_out); |
157 | 169 |
|
158 | 170 | //Callbacks |
159 | 171 | base.execCallBacks(filter_out); |
| 172 | + |
| 173 | + //base.last_result = filter_out; |
160 | 174 | }, |
161 | 175 |
|
162 | 176 | //Compare and collect objects |
|
284 | 298 | }); |
285 | 299 | }, |
286 | 300 |
|
| 301 | + search: function(ele, filter_result){ |
| 302 | + var base = this; |
| 303 | + var val = $(ele).val().trim(); |
| 304 | + |
| 305 | + if(!val.length) return filter_result; |
| 306 | + |
| 307 | + var id_str = "#" + base.settings.root + '_'; |
| 308 | + |
| 309 | + return $.map(filter_result, function(id){ |
| 310 | + if($(id_str + id).text().toUpperCase().indexOf(val.toUpperCase()) >= 0){ |
| 311 | + return id; |
| 312 | + } |
| 313 | + }); |
| 314 | + |
| 315 | + }, |
| 316 | + |
287 | 317 | execCallBacks: function(result){ |
288 | 318 | var base = this; |
289 | 319 | if(!base.settings.callbacks) return; |
|
301 | 331 | ); |
302 | 332 | } |
303 | 333 |
|
| 334 | + |
304 | 335 | }; |
305 | 336 |
|
306 | 337 | window.FilterJS = FilterJS; |
|
0 commit comments