Skip to content

Commit 5620574

Browse files
committed
Fix search filter
1 parent 232989f commit 5620574

10 files changed

Lines changed: 1689 additions & 45 deletions

File tree

dist/filter.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* filter.js
3-
* 2.0.0 (2015-08-04)
3+
* 2.1.0 (2015-08-06)
44
*
55
* Released under the MIT license
66
* http://opensource.org/licenses/MIT
@@ -13,7 +13,7 @@
1313

1414
/*
1515
* JsonQuery
16-
* 0.0.2 (2015-05-20)
16+
* 0.0.2 (2015-08-06)
1717
*
1818
* Released under the MIT license
1919
* http://opensource.org/licenses/MIT
@@ -83,7 +83,11 @@
8383
this.lng = opts.longitude || Config.longitude;
8484
this.id = opts.id;
8585

86-
if(this.records.length){
86+
if(opts.schema){
87+
this.schema = opts.schema;
88+
}
89+
90+
if(this.records.length && !this.schema){
8791
initSchema(this, records[0], opts.schema);
8892
}
8993
};
@@ -1162,8 +1166,8 @@
11621166
}
11631167

11641168
this.show(this.last_result);
1165-
this.execCallback('afterFilter', this.last_result, JsonQuery.blankClone(this.last_result, this.Model));
11661169
this.renderPagination(this.last_result.length);
1170+
this.execCallback('afterFilter', this.last_result, JsonQuery.blankClone(this.Model, this.last_result));
11671171

11681172
return query;
11691173
};
@@ -1280,10 +1284,10 @@
12801284

12811285
result = this.search(this.search_text, records || this.lastResult());
12821286

1287+
this.search_result = result;
12831288
this.show(result);
12841289
this.renderPagination(result.length);
1285-
this.execCallback('afterFilter', result);
1286-
this.search_result = result;
1290+
this.execCallback('afterFilter', result, JsonQuery.blankClone(this.Model, result));
12871291

12881292
return true;
12891293
};
@@ -1430,8 +1434,6 @@
14301434

14311435

14321436

1433-
1434-
14351437
var Paginator = function(recordsCount, opts, onPagination) {
14361438
var paginationView;
14371439

@@ -1614,6 +1616,7 @@
16141616
};
16151617

16161618
FilterJS.list = list;
1619+
FilterJS.templateBuilder = templateBuilder;
16171620

16181621
window.FilterJS = FilterJS;
16191622

dist/filter.min.js

Lines changed: 1604 additions & 22 deletions
Large diffs are not rendered by default.

examples/pagination.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ <h4 class='col-md-6'>Movies (<span id="total_movies">0</span>)</h4>
2323
</div>
2424
<div>
2525
<label class="sr-only" for="searchbox">Search</label>
26-
<input type="text" class="form-control" id="searchbox" placeholder="Search &hellip;">
26+
<input type="text" class="form-control" id="searchbox" placeholder="Search &hellip;" autocomplete="off">
2727
<span class="glyphicon glyphicon-search search-icon"></span>
2828
</div>
2929
<br>

examples/pagination.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ $(document).ready(function(){
99

1010
var afterFilter = function(result, jQ){
1111
$('#total_movies').text(result.length);
12+
1213
var checkboxes = $("#genre_criteria :input:gt(0)");
13-
var qResult = JsonQuery(result);
1414

1515
checkboxes.each(function(){
1616
var c = $(this), count = 0
1717

1818
if(result.length > 0){
19-
count = qResult.where({ 'genre': c.val() }).count;
19+
count = jQ.where({ 'genre': c.val() }).count;
2020
}
2121
c.next().text(c.val() + '(' + count + ')')
2222
});
@@ -37,8 +37,6 @@ $(document).ready(function(){
3737
container: '#per_page'
3838
},
3939
}
40-
41-
//appendToContainer: appendToContainer
4240
});
4341

4442
FJS.addCallback('beforeAddRecords', function(){
@@ -57,13 +55,11 @@ $(document).ready(function(){
5755
}
5856
});
5957

60-
/*
6158
FJS.setStreaming({
6259
data_url: 'data/stream_movies.json',
6360
stream_after: 1,
6461
batch_size: 50
6562
});
66-
*/
6763

6864
FJS.addCriteria({field: 'year', ele: '#year_filter', type: 'range', all: 'all'});
6965
FJS.addCriteria({field: 'rating', ele: '#rating_filter', type: 'range'});

gulpfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ gulp.task('scripts', function() {
7070
.pipe(header(banner, { pkg: pkg } ))
7171
.pipe(gulp.dest(paths.dist))
7272
.pipe(sourcemaps.init())
73-
.pipe(uglify({preserveComments: 'all'}))
73+
//.pipe(uglify({preserveComments: 'all'}))
7474
.pipe(rename(compressedJs))
7575
.pipe(gulp.dest(paths.dist))
7676

lib/json_query.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* JsonQuery
3-
* 0.0.2 (2015-05-20)
3+
* 0.0.2 (2015-08-06)
44
*
55
* Released under the MIT license
66
* http://opensource.org/licenses/MIT
@@ -70,7 +70,11 @@
7070
this.lng = opts.longitude || Config.longitude;
7171
this.id = opts.id;
7272

73-
if(this.records.length){
73+
if(opts.schema){
74+
this.schema = opts.schema;
75+
}
76+
77+
if(this.records.length && !this.schema){
7478
initSchema(this, records[0], opts.schema);
7579
}
7680
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "filter.js",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "A javascript library for filtering json object and rendering html",
55
"homepage": "https://github.com/jiren/filter.js",
66
"licenses": [

src/filter.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,8 @@ F.filter = function(){
307307
}
308308

309309
this.show(this.last_result);
310-
this.execCallback('afterFilter', this.last_result, JsonQuery.blankClone(this.last_result, this.Model));
311310
this.renderPagination(this.last_result.length);
311+
this.execCallback('afterFilter', this.last_result, JsonQuery.blankClone(this.Model, this.last_result));
312312

313313
return query;
314314
};
@@ -425,10 +425,10 @@ F.searchFilter = function(records) {
425425

426426
result = this.search(this.search_text, records || this.lastResult());
427427

428+
this.search_result = result;
428429
this.show(result);
429430
this.renderPagination(result.length);
430-
this.execCallback('afterFilter', result);
431-
this.search_result = result;
431+
this.execCallback('afterFilter', result, JsonQuery.blankClone(this.Model, result));
432432

433433
return true;
434434
};

src/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
/* inject src/template.js */
66
/* inject src/util.js */
77
/* inject src/filter.js */
8-
/* inject src/sorting.js */
98
/* inject src/paginator.js */
109
/* inject src/jquery_fn.js */
1110

@@ -19,6 +18,7 @@
1918
};
2019

2120
FilterJS.list = list;
21+
FilterJS.templateBuilder = templateBuilder;
2222

2323
window.FilterJS = FilterJS;
2424

src/sorting.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
var Sort = function(opts, onSortEvent) {
2+
this.opts = opts;
3+
4+
if(!this.opts.default_sort){
5+
this.opts.default_sort = 'desc';
6+
}
7+
8+
this.fields = {};
9+
this.onSortEvent = onSortEvent;
10+
};
11+
12+
var S = Sort.prototype;
13+
14+
S.addSorting = function(selector, field, order){
15+
if(!order){
16+
order = this.order.default_sort;
17+
}
18+
19+
var opts = {
20+
field: field,
21+
selector: selector,
22+
order: order,
23+
toggle: false,
24+
enable: true
25+
};
26+
27+
this.fields[fields] = opts;
28+
this.bindEvent(opts);
29+
};
30+
31+
32+
S.bindEvents = function(opts){
33+
var self = this,
34+
event_type,
35+
$ele = $(opts.selector);
36+
37+
if(!opts.event_type){
38+
event_type = $ele.tagName == 'SELECT' ? 'change' : 'click';
39+
}
40+
41+
$ele.data('sort', opts.field);
42+
43+
$ele.on(event_type, { field: opts.field }, function(e){
44+
var f = self.field[e.data.field]
45+
46+
if(f.toggle){
47+
f.order = f.order == 'desc' ? 'asc' : 'desc';
48+
}
49+
50+
self.onSortEvent(f);
51+
e.preventDefault();
52+
});
53+
};
54+
55+
S.disableAll = function(){
56+
$.each(this.field, function(f, opts){
57+
opts.enable = false;
58+
});
59+
}

0 commit comments

Comments
 (0)