Giter Club home page Giter Club logo

angular-chosen's Introduction

Angular Chosen Localytics Bower npm

AngularJS Chosen directive

This directive brings the Chosen jQuery plugin into AngularJS with ngModel and ngOptions integration.

To use, include localytics.directives as a dependency in your Angular module. You can now use the chosen directive as an attribute on any select element. Angular version 1.3+ is required, but recommended 1.4.9+.

Installation (npm or bower)

Via bower

$ bower install angular-chosen-localytics --save

Via npm

$ npm install angular-chosen-localytics --save

Via cdn

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-chosen-localytics/1.9.2/angular-chosen.min.js"></script>

Or download zip file

Download v1.9.2

Yeoman or Bower install

If you use Yeoman or Bower install, you need to rename the chosen.jquery.js or the chosen.proto.js to chosen.js. Otherwise Chosen won't be included in your index.html.

Features

  • Works with ngModel and ngOptions
  • Supports usage of promises in ngOptions
  • Provides a 'loading' animation when 'ngOptions' collection is a promise backed by a remote source
  • Pass options to Chosen via attributes or by passing an object to the Chosen directive
  • Provider with default values with chosenProvider (read: Added config-provider) [since 1.6.0]

Usage

Simple example

Similar to $("#states").chosen()

<select chosen multiple id="states">
  <option value="AK">Alaska</option>
  <option value="AZ">Arizona</option>
  <option value="AR">Arkansas</option>
  <option value="CA">California</option>
</select>

Pass any chosen options as attributes

<select chosen
        data-placeholder-text-single="'Pick one of these'"
        disable-search="true"
        allow-single-deselect="true">
  <option value=""></option>
  <option>This is fun</option>
  <option>I like Chosen so much</option>
  <option>I also like bunny rabbits</option>
</select>

Note: the empty option element is mandatory when using allow-single-deselect

Integration with ngModel and ngOptions

<select multiple
        chosen
        ng-model="state"
        ng-options="s for s in states">
</select>

Note: don't try to use ngModel with ngRepeat. It won't work. Use ngOptions. It's better that way.

Also important: if your ngModel is null or undefined, you must manually include an empty option inside your <select>, otherwise you'll encounter strange off-by-one errors:

<select multiple
        chosen
        ng-model="state"
        ng-options="s for s in states">
  <option value=""></option>
</select>

This annoying behavior is alluded to in the examples in the documentation for ngOptions.

Works well with other AngularJS directives

<select chosen
        ng-model="state"
        ng-options="s for s in states"
        ng-disabled="editable">
  <option value=""></option>
</select>

Loading from a remote data source

Include chosen-spinner.css and spinner.gif to show an Ajax spinner icon while your data is loading. If the collection comes back empty, the directive will disable the element and show a default "No values available" message. You can customize this message by passing in noResultsText in your options.

app.js
angular.module('App', ['ngResource', 'localytics.directives'])
    .controller('BeerCtrl', function($scope, $resource) {
      $scope.beers = $resource('api/beers').query()
    });
index.html
<div ng-controller="BeerCtrl">
  <select chosen
          data-placeholder-text-single="'Choose a beer'"
          no-results-text="'Could not find any beers :('"
          ng-model="beer"
          ng-options="b for b in beers">
      <option value=""></option>
  </select>
</div>

Image of select defined above in loading state: <img src="https://raw.github.com/localytics/angular-chosen/master/example/choose-a-beer.png">

Note: Assigning promises directly to scope is now deprecated in Angular 1.2+. Assign the results of the promise to scope once the promise returns. The loader animation will still work as long as the collection expression evaluates to undefined while your data is loading!

Default values with chosenProvider (thanks @zlodes) [since 1.6.0]

angular.module('chosenExampleApp', ['localytics.directives'])
    .config(['chosenProvider', function (chosenProvider) {
        chosenProvider.setOption({
            no_results_text: 'There is no results!',
            placeholder_text_multiple: 'Choose one or more!'
        });
    }]);

angular-chosen's People

Contributors

abuggia avatar abyx avatar adrianboimvaser avatar akotlar avatar andreialecu avatar andrewfreese avatar charandas avatar darlanalves avatar dougludlow avatar failpunk avatar gaui avatar iamnewspecies avatar jakob-lundberg avatar jamiearonson avatar jason-cooke avatar jr314159 avatar leocaseiro avatar liorcode avatar lukemason avatar nike-17 avatar odi55555 avatar paulpflug avatar pkarl avatar simison avatar slobo avatar vantanev avatar zlodes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

angular-chosen's Issues

allow-single-deselect not compatible with angular filter

When using allow-single-deselect, the ng-model gets set to null by deselecting which is not compatible with angular filter.

I worked around by manually deleting the property

for k,v of factory.filter
      if !v
        delete factory.filter[k]

How do I get the selected element?

Sorry if this is not the place to ask this, but how do I get the chosen option that I am acting upon? For instance, if I have an option called "Item 1" and I click the "x" to remove it, I want to tell my ng-change event to make an ajax call to remove that item. Right now the model returns an array of items that remain selected, but not the one that was removed.

Want to put angular-chosen inside my directive

HI,

When I try to move angular-chosen inside of my own directive html template I have some issues:

  • when construction is made within ng-hide chosen width is 0. (ng-if works fine)
  • ng-model field is not updated. (first time it is read fine)

disable multiple

Hi,

there are some way to disable the control? I have tryed to put disabled in original select
but doesnt works

thanks

ng-model bug: Options shifted by one after selecting the first time

If you use the ng-model attribute, all the data-option-array-index attributes for li the elements under the chosen-results list are shifted by one (1, 2, 3... instead of 0, 1, 2...). Selection works the first time -- I'm guessing because the placeholder option is in the array and shifting all the other elements -- but every subsequent selection actually assigns the value after the selected value to the model (click on the second option, and the model is assigned the value for the third option). Picking the last value causes JQuery to throw an error.

How to reproduce:
Open angular-chosen/example/index.html
Pick a value from "Chosen with static options"
Pick another one and watch the model update to the value after it (or if the value selected is the last one, see JQuery throw an error in the console)

I'll try to see if I can fix it.

Error: 'undefined' is not an object (evaluating 'chosen.default_text') (only in mobile browsers)

I just got this error only when using iPhone/iPod touch.. You can see it when using Safari with iPhone selected as User-Agent

[Error] Error: 'undefined' is not an object (evaluating 'chosen.default_text')
initOrUpdate@http://..../angular-chosen-localytics/chosen.js:60:40
$render@http://..../angular-chosen-localytics/chosen.js:75:32
https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js:189:12
$digest@https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js:109:81
$apply@https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js:112:180
h@https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js:72:306
w@https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js:77:446
onreadystatechange@https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js:78:376
    (anonyme Funktion) (angular.min.js, line 92)
    error (logging.js, line 38)
    $digest (angular.min.js, line 109)
    $apply (angular.min.js, line 112)
    h (angular.min.js, line 72)
    w (angular.min.js, line 77)
    onreadystatechange (angular.min.js, line 78)

Bower install gives the wrong version

Like the title says; bower install angular-chosen-localytics` gives us the wrong version ( at least not the latest version ). Might want to push the new version of the plugin to bower.

Cheers.

Focus show

How I can do that when it has focus display options?

Lexer error when trying to set no-results-text attribute to a string

When I try to set the html attribute "no-results-text" to a special character (like 'á' or 'é'), angular throws a lexer error since it tries to evaluate it as an expression at line 41:

return options[snakeCase(key)] = scope.$eval(value);

I could solve it by specifically filter this attribute separately but the issue should be addressed IMO

How to show default selected for single?

given the example : Changing the ngModel externally: {{ myPets }}

i removed 'multiple' from the selected and commented out
return $scope.myPets.push('hamster');
so to only have one selected item. i expected the select to default to 'cat' but it's still on 'Select an Option'. is this a bug? how can i get it to default to 'cat'?

Disabled elements with empty option issues

I'm attempting to have a list of elements for my chosen field and disabled a few based on the disabled attribute of their object. This works great, however, when I switch between elements, the next value for ng-model is undefined. I read to add the empty option HTML element to the chosen element, however, upon doing so, my disabled options are offset by 1. Is there a way to have the proper values disabled while still swapping between ng-model values?

Thanks!

angularjs: filtering with chosen control

Original jsfiddle with no filters, works as expected. http://jsfiddle.net/wKMZM/5/

Updated jsfiddle fails even to render the control. http://jsfiddle.net/wKMZM/3/

In the updated version, I have updated the ng-model to be associated with an array of objects. I have added a directive to convert it to an array of string in the filter directive.

Any idea, what I am missing?

Posted also on SO also: http://stackoverflow.com/questions/24909938/angularjs-filtering-with-chosen-control

Support ngDisabled

Hi,

angular-chosen doesn't work well with ngDisabled. I add the following codes:

          attr.$observe('disabled', function (value) {
            setTimeout(function() {
                element.attr('disabled', value).trigger("chosen:updated");
            });
          });

Don't scroll to top when selecting multiple items with ctrl

When selecting multiple items from a dropdown and holding ctrl, it would be more user friendly to prevent the default action to scroll to the top of the dropdown menu. I'm dealing with a dropdown of 200+ items and this is an annoying feature, to say at least.

Dynamic options

Hello!

I am having trouble with options mapped to a standard filter - the options stay in the chosen dropdown, just don't work while the model is changed. Is there a way to support this?

select(chosen, ng-model='data.item', ng-options='item.value as item.name for option in data.items | filter: search')

Thanks!

Google chrome emulate breaks angular-chosen plugin

I am using Chosen v1.1.0 with angular-chosen plugin on Google Chrome Version 33.0.1750.149. It works fine until I open browser console to emulate a mobile version and refresh the page. Is there a particular reason for this ?

Dynamic IDs in chosen generated HTML

Hello,
I have a question regarding dynamic IDs.
I'm generating many boxes and they get their ID by id="{{id}}". e.g. id="test" When chosen is applied to the select element, the ID of the chosen generated HTML is id="__test___chosen". Is there a way to get the ID to be id="test_chosen" by not setting it hardcoded in the HTML? I hope you understand my problem and might know an answer.

the select doesn't refill once the collection became empty

hi, i'm using ng-options together with chosen ... once my collection goes empty i get the message that there are no items. But if my collection has items again it doesn't add them, it stays empty. I'm using 1.1.5, so i'm not sure if it has anything to do with that or that there is a bug. Do you have an idea what this could be? Also the select element isn't updated either after it went empty ... if i remove the chosen directive from my select element, then it works.

Chosen select do not show value in ng-model

Hi!
I have 2 chosen selects. One with parent and other with children values.I am populating parent model based on the model of child.Everything works fine as long as each parent has only one child.
But if parent contains multiple children and 2nd child of the certain parent is selected in the chosen select box,corresponding parent chosen select is not showing the model value.

These are my chosen select

This is my angular code for populating the umbrellas model according to fund model

if ($scope.childrenModel.length == 1) {
$scope.parentsModel.push($scope.someName);
}
When i am checking the value of $scope.parentsModel its showing correct value in it.But its still not getting displayed on chosen Select as selected option.

No license

Without a license, people can't legally use this directive in their projects. Please consider adding one!

ng-change on single deselect

I've been using ng-change on my chosen select box and it's been working quite well; however, the ng-change event doesn't get triggered when using the single deselect feature. Does this need to be implemented on the angular-chosen side?

Chosen directive doesn't watch underlying model for changes

If the model is updated externally to this directive (ex. from a rest call) the directive doesn't update chosen. Something like

    require: 'ngModel',
        link: function(scope, element, attr, ctrl) {
            ctrl.$render = function () {
                $timeout(function() {element.trigger('chosen:updated');});
             };

should work.

<optgroup> Support?

Assuming that I have a collection of elements with a group property, is there a way to use 'ng-options' in such a way that it will produce correct '' tags? As I can't think of a way of doing this...

chosen/select2 abstraction

It would be great to use this library with chosen or select2.

(I believed select2 was based on chosen but I didn't find any official confirmation).

I know there's already a angular-ui ui-select2 but I prefer this one :

  • $translate 2 support (translations with promises)
  • ngOptions support
  • ...

Default_text property error only in chrome

Hi
I get this error only in Chrome not in any other browser could any one point me in the right direction this is might be my own fault but the error msg could be better:

Chrome stacktrace:
TypeError: Cannot read property 'default_text' of undefined
at angular.module.directive.link.initOrUpdate (http://localhost:49904/Content/js/lib/chosen.js:59:40)
at angular.module.directive.link.ngModel.$render (http://localhost:49904/Content/js/lib/chosen.js:74:20)
at Object.ngModelWatch (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.11/angular.js:18352:14)
at Object.ng.config.$provide.decorator.$delegate.proto.$watch.watchExpression (:754:29)
at Scope.$get.Scope.$digest (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.11/angular.js:12443:40)
at Scope.ng.config.$provide.decorator.$delegate.proto.$digest (:844:31)
at Scope.$get.Scope.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.11/angular.js:12724:24)
at Scope.ng.config.$provide.decorator.$delegate.proto.$apply (:855:30)
at done (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.11/angular.js:8328:45)
at completeRequest (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.11/angular.js:8533:7)

Code:

$scope.load = function () {
$scope.konton = //fetch data
}

Html:

When i Debug Firefox never enter this method only Chrome and the chosen var get undefined:

    initOrUpdate = function() {
      if (chosen) {
        return element.trigger('chosen:updated');
      } else {
        chosen = element.chosen(options).data('chosen'); <-- this is undefined
        return defaultText = chosen.default_text;
      }

Empty list

For some reason the list isn't populated with my ng-options. If I use the select without the chosen directive I can see everything, but whenever I add the chosen directive the list is empty.

I'm using ajax loading.

jade:

select(chosen, ng-options='c._id as c.name for c in courses', ng-model='selectedCourse')

in the controller:

$scope.courses = Course.query();

Again, without the chosen directive the list is fine.

Multiple Chosen Responsive: Width is Fixed

Besides I set the witdh:90% the directive convert to number XXXpx cause to multiple select not be responsive.

and the css is element.style { width: 294px; } .chosen-container { position: relative; display: inline-block; vertical-align: middle; font-size: 13px; zoom: 1; -webkit-user-select: none; -moz-user-select: none; user-select: none; }

Cannot just pass any chosen attributes

Per readme here, allow-single-deselect is supported, and it used to be until 1.0.3. 1.0.4 regressed in that it no longer works.

I couldn't figure out from the commits, or else would have made a PR.

please add support of "ng-init" directive

example:

<select name="subject" ng-model="contactForm.subject" ng-init="contactForm.subject='value1'" required chosen disable-search="true" width="260">
      <option value="value1">test1</option>
      <option value="value2">test2</option>
      <option value="value3">test3</option>
</select>

with 'chosen' directive in hidden (original) select tag present bad option like this (before any changes):

<option value="? string:value1 ?"></option>

and I got javascript error if I checked last option in chosen select.

P.S. http://jsfiddle.net/darkfisk/UvzZf/
P.S. it seems to me that the initialization of the Chosen happens before the execution ng-init, which causes an error.

README error

The module to be registered as a dependency is angular-chosen not localytics.directives.

Failed to use

When using ng-options on a single select after selecting an option attempting to select the last option will throw this error: "Uncaught TypeError: Cannot set property 'selected' of undefined"

It's an off by 1 error. ng-options is generating the list with n+1 options(the +1 being an empty option). The solution is to watch the select.length and on change throw an updated call.

Here is the fix, jam it in the linker function:

scope.$watch(function(){
    return element[0].length; 
}, function(newvalue, oldvalue){
    if (newvalue !== oldvalue) {
        element.trigger("chosen:updated");
    }
});

How would the spinner be useful when the select is to be populated conditionally

I was able to get the spinner to work but the undefined-checking makes it spin as soon as the page loads. In my case, the $resource.get doesn't fire until a condition is met. The spinner should start only at that moment.

I am working with the classic location selector, and its only the selection of country, that states are populated, and on selection of state, that cities are populated.

Any suggestions would be appreciated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.