Skip to content

Commit f4d6f5f

Browse files
author
Jiren Patel
committed
Added id_field option
1 parent b36c7ec commit f4d6f5f

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,11 @@ Add streaming option to above define 'settings'.
290290

291291
persons.json?offset=0&limit=50&q='search text'
292292

293+
Options
294+
-------
295+
296+
- Default primary key for indexing array is 'id' field or json object. To change this field set 'id_field' option.
297+
293298
Demo
294299
----
295300
To see the sample demo, clone this repo and open demo/filterjs.html in your browser

filter.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
this.root = 'fjs';
5454
}
5555

56+
this.id_field = this.options.id_field || 'id';
5657
this.render(this.data);
5758
this.parseOptions();
5859
this.buildCategoryMap(this.data);
@@ -87,7 +88,7 @@
8788
for (var i = 0, l = data.length; i < l; i++){
8889
record = this.getRecord(i, data);
8990
el = $(this.view(record));
90-
el.attr({id: this.root + '_' + record.id, 'data-fjs': true});
91+
el.attr({id: this.root + '_' + record[this.id_field], 'data-fjs': true});
9192
el = $container.append(el);
9293
}
9394
},
@@ -256,7 +257,7 @@
256257

257258
for (var i = 0, l = data.length; i < l; i++){
258259
record = this.getRecord(i, data);
259-
this.record_ids.push(record.id);
260+
this.record_ids.push(record[this.id_field]);
260261

261262
for (name in filter_criteria) {
262263
categories = eval('record.' + filter_criteria[name][2]);
@@ -265,10 +266,10 @@
265266
if (categories && categories.constructor == Array) {
266267
for (var j = 0, lj = categories.length; j < lj; j++){
267268
x = categories[j];
268-
obj[x] ? obj[x].push(record.id) : obj[x] = [record.id];
269+
obj[x] ? obj[x].push(record[this.id_field]) : obj[x] = [record[this.id_field]];
269270
}
270271
} else {
271-
obj[categories] ? obj[categories].push(record.id) : obj[categories] = [record.id];
272+
obj[categories] ? obj[categories].push(record[this.id_field]) : obj[categories] = [record[this.id_field]];
272273
}
273274
}
274275
}
@@ -325,7 +326,7 @@
325326

326327
for (i; i < l; i++){
327328
r = this.getRecord(i, this.data);
328-
if (ids.indexOf(r.id) != -1) records.push(r)
329+
if (ids.indexOf(r[this.id_field]) != -1) records.push(r)
329330
}
330331

331332
return records;

0 commit comments

Comments
 (0)