machineboy2045 / angular-selectize

angular-selectize
MIT License
201 stars 115 forks source link

Allow dynamic loading of individual selectize options #122

Closed zeiphon closed 8 years ago

zeiphon commented 8 years ago

There may be a better way to do this than the way I have in this pull request, but it would be extremely useful for angular-selectize to be able to fetch unloaded options via a callback.

This enables users of angular-selectize to (optionally) provide default values or to change the selectize model externally and have the options loaded as needed - sorely needed when you're using ajax.

machineboy2045 commented 8 years ago

You should be able to use an angular controller to update the options. something like $scope.myOptions = $http.get('my-url.com');

zeiphon commented 8 years ago

I'm not quite sure I follow you. When you have selectize set up to load/autocomplete options automatically from an ajax call (by specifying the 'load' callback), the options are generally handled internally but the problem you come up against is that if there is an initial value set (such as an Id of an option), selectize won't have the option loaded and won't query for it and so it won't display.

machineboy2045 commented 8 years ago

I think i misunderstood the first time. My usual suggestion is to wrap your selectize in ng-if='options.length' to prevent it from initializing until your options are loaded.

On Thu, Jan 14, 2016 at 6:44 PM, Jonathon T notifications@github.com wrote:

I'm not quite sure I follow you. When you have selectize set up to load/autocomplete options automatically from an ajax call (by specifying the 'load' callback), the options are generally handled internally but the problem you come up against is that if there is an initial value set (such as an Id of an option), selectize won't have the option loaded and won't query for it and so it won't display.

— Reply to this email directly or view it on GitHub https://github.com/machineboy2045/angular-selectize/pull/122#issuecomment-171852084 .

zeiphon commented 8 years ago

I see, that sound like it'd certainly work if you were able to load all of your options at once, but when you only want to load options selectively from an ajax query callback (e.g. when the user begins typing), then you'd still need a way to figure out which option to load. For a default value this might be easy enough - you could just perform an ajax call on startup for that specific option. When you have the possibility of the option Id (which angular-selectize is watching) being set externally however, you would also have to watch that variable and load in a new option whenever it doesn't exist in selectize's option list. Possible of course, but you'd also have to re-do this logic in every instance, which is why I was suggesting it would be nice to have a slightly more reusable way to do it.