selectize.js icon indicating copy to clipboard operation
selectize.js copied to clipboard

searchField with JSON object

Open gabriellebautista opened this issue 12 years ago • 6 comments

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.

gabriellebautista avatar Jun 20 '14 16:06 gabriellebautista

Nested objects are not currently supported with sifter.js (what Selectize uses).

brianreavis avatar Jun 29 '14 05:06 brianreavis

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); }

gabriellebautista avatar Jun 30 '14 14:06 gabriellebautista

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);
}

brianreavis avatar Jun 30 '14 18:06 brianreavis

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?

gabriellebautista avatar Jun 30 '14 19:06 gabriellebautista

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.

brianreavis avatar Jul 01 '14 01:07 brianreavis

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).

rstudner avatar Sep 18 '14 23:09 rstudner