EvanOxfeld / angular-selectize.js

Angular Selectize directive for a hybrid textbox + <select> box. Supports ngOptions and two-way bindings.
MIT License
34 stars 16 forks source link

Undefined ngModel adds a null dropdown option, doesn't diplay the placeholder, and breaks typing #20

Open conoremclaughlin opened 10 years ago

conoremclaughlin commented 10 years ago

appreciate your work on this!

it looks like the behaviour requires an initial value must be set on the model. this isn't ideal if we're trying to show off a placeholder first as well as force the user to make an explicit choice.

an example secnariowould be choosing their country for a telephone number.

following code:

select(selectize, placeholder="Choose a country", ng-model="user.nationality", ng-options="country.name for country in countries")

Unless user.nationality has been initialised to one of the choices, this breaks the typeahead of selectize and adds an empty row. Any advice on how to proceed?

conoremclaughlin commented 10 years ago

after looking it over - this follows angular.js's standard select behaviour (which is silly IMO but oh well!). sorry to be a bother!

EvanOxfeld commented 10 years ago

@conoremclaughlin Thanks for the feedback -- it's not a bother at all. Even if I wasn't reopening this issue.

Angular-selectize supports the placeholder attribute, however, the model has to be explicitly initialized to null for the placeholder to be displayed. An undefined model should display the placeholder and not add an empty row. In this instance I don't think it makes sense for the selectize directive to follow Angular's standard behavior.

kernel-io commented 9 years ago

this was really pissing me off, so I hacked up a solution which fixes it for me.

haven't tested all edge cases but it works so far for me

              $timeout(function(){
                selectize.removeOption('? object:null ?');
                selectize.removeOption('? undefined:undefined ?');
              },0);

put this on line 155, right after syncInputClasses();

screen shot 2015-05-18 at 3 55 14 am

Here is how it looks in action, it will work if the model is undefined or null, i use ng-repeat to generate my options so YMMV

screen shot 2015-05-18 at 3 56 05 am