searchField with JSON object
I'm rendering my options with an ajax call...
$.ajax
({
type : 'GET',
dataType: "json",
success: function(res)
{
callback(res);
}
});
...which returns a JSON object: { "id": 1, "key": "ASGARD", "name": { "firstname": "Gab", "lastname": "Bautsta" }, }
I want to search by "key","name.firstname" and "name.lastname" but the following doesn't work...
searchField: ['key','name.firstname','name.lastname']
...anything I'm doing wrong? It's only searching by key.
Nested objects are not currently supported with sifter.js (what Selectize uses).
Do you think this workaround will work then?
success: function(jsonData) { var res = {}; res ["key"] = jsonData.key; res ["firstname"] = jsonData.name.firstname; res ["lastname"] = jsonData.name.lastname; callback(res); }
Yep :) Although you'll want to return an array because it expects multiple results.
success: function(jsonData)
{
var options = [];
var res = {};
res ["key"] = jsonData.key;
res ["firstname"] = jsonData.name.firstname;
res ["lastname"] = jsonData.name.lastname;
options.push(res);
callback(options);
}
Thanks! One last thing, when I click anywhere outside the "search" box (focus out), the value I've entered is being removed. Is this a default behaviour?
This is the default behavior, yes. Selectize assumes the user don't want to select anything unless they actually click on it or press enter while the option is highlighted. While this isn't for all blur events, you might want check out selectOnTab: true.
so, I understand nested objects aren't supported, so I thought i'd ask.. wht about:
{ 'name' : 'john', tags: ['horse', 'cat', 'frog'] }
anyway to have searchFields that can search the array (of simple strings).