Skip to content

Commit f47f6b2

Browse files
author
Jiren Patel
committed
Added text index function option for fields for searching complex nested json records
1 parent 7463586 commit f47f6b2

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,23 @@ If you have already searcbox then you can set the selector.
122122

123123
search_box: '#my-searchb0x'
124124

125-
You can enable search on specific fields
125+
StreamTable make index out of data for searching text. Default it get all top level fields and make index.
126+
If you want to enable search on specific fields or you have complex nested fields.
126127

127-
fields: ['id', 'name'] //if each record is an object, then mention the field names. i.e {user: {id: 1, name: 'user', amount: 2}}
128+
fields: ['id', 'name'] //if each record is an object, then mention the field names. i.e {id: 1, name: 'user', amount: 2}
128129
fields: [0, 2] //if each record is an array, then mention the index of fields. i.e [1, 'User', '2']
130+
131+
// If in data you have nested json object i.e { address: {pin: 23123}}
132+
fields: ['address.pin']
133+
134+
// If in data you have nested json object array. i.e {name: 'jiren', contacts: [{email: '[email protected] }, {email: '[email protected]' }]}
135+
// In this case define text function which return text string.
136+
fields: function(record) {
137+
return [
138+
record.name,
139+
$.map(record.contacts, function(contact) { return contact.email } )
140+
].join(' ')
141+
}
129142

130143
Pagination Options:
131144
-------------------

examples/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,22 @@ $(document).ready(function() {
2929
callbacks: callbacks,
3030
pagination: {span: 5, next_text: 'Next →', prev_text: '← Previous'}
3131
},
32+
data
33+
);
34+
35+
/*
36+
NOTE: Search only for year. If you define fields function then it must return text.
37+
You have complex nested record data then use function.
38+
st = StreamTable('#stream_table',
39+
{ view: view,
40+
per_page: 10,
41+
callbacks: callbacks,
42+
pagination: {span: 5, next_text: 'Next →', prev_text: '← Previous'},
43+
fields: function(r){ return [ r.year, r.rating].join(' ')} // OR fields: ['year' , 'rating']
44+
},
3245
data
3346
);
47+
*/
3448

3549
// Jquery plugin
3650
//$('#stream_table').stream_table({view: view}, data)

stream_table.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* StreamTable.js
3-
* version: 1.1.0 (28/7/2013)
3+
* version: 1.1.1 (17/12/2013)
44
*
55
* Licensed under the MIT:
66
* http://www.opensource.org/licenses/mit-license.php
@@ -157,22 +157,24 @@
157157
_F._makeTextFunc = function(record){
158158
var fields = this.opts.fields, cond_str = [], textFunc, is_array = false;
159159

160-
if (record.constructor == Object){
160+
if(typeof fields == 'function'){
161+
textFunc = fields;
162+
} else if (record.constructor == Object){
161163
fields = fields || Object.keys(record)
162164

163165
for (var i = 0, l = fields.length; i < l; i++){
164166
cond_str.push("d."+ fields[i]);
165167
}
166-
eval("textFunc = function(d) { return (" + cond_str.join(" + ' ' + ") + ").toUpperCase(); }");
168+
eval("textFunc = function(d) { return (" + cond_str.join(" + ' ' + ") + "); }");
167169
}else{
168170
if (fields){
169171
for(var i = 0, l = fields.length; i < l ; i++){
170172
cond_str.push("d["+ fields[i] + "]");
171173
}
172-
eval("textFunc = function(d) { return (" + cond_str.join(" + ' ' + ") + ").toUpperCase(); }");
174+
eval("textFunc = function(d) { return (" + cond_str.join(" + ' ' + ") + "); }");
173175
}else{
174176
textFunc = function(d) {
175-
return d.join(' ').toUpperCase();
177+
return d.join(' ');
176178
}
177179
}
178180
}
@@ -183,10 +185,14 @@
183185
_F.buildTextIndex = function(data){
184186
var i = 0, l = data.length;
185187

186-
if (!this.textFunc) this.textFunc = this._makeTextFunc(data[0]);
188+
if (!this.textFunc) {
189+
this.textFunc = this._makeTextFunc(data[0]);
190+
}
191+
console.log(this.textFunc)
187192

188-
for(i; i < l; i++)
189-
this.text_index.push(this.textFunc(data[i]));
193+
for(i; i < l; i++){
194+
this.text_index.push(this.textFunc(data[i]).toUpperCase());
195+
}
190196
};
191197

192198
_F.render = function(page){

0 commit comments

Comments
 (0)