Git Product home page Git Product logo

angularmultipleselect's Introduction

angularMultipleSelect

A complete Angularjs directive for multiple select autocomplete http://jagdeep-singh.github.io/angularMultipleSelect/

#Sample View https://cloud.githubusercontent.com/assets/13271120/11739891/b2d3e426-a014-11e5-9b57-a0d758f07bf0.png

#Example http://run.plnkr.co/plunks/XbHaZSiYqEzxjk6TuWyj/

#Event listeners

  1. beforeSelectItem : Listen for event before adding an item
$scope.beforeSelectItem = function(item){
    // perform operation on this item before selecting it.
}
<multiple-autocomplete ng-model="selectedList"
     before-select-item="beforeSelectItem"
     suggestions-arr="optionsList">
</multiple-autocomplete>
  1. afterSelectItem : Listen for event before adding an item
$scope.afterSelectItem = function(item){
    // perform operation on this item after selecting it.
}
<multiple-autocomplete ng-model="selectedList"
     after-select-item="afterSelectItem"
     suggestions-arr="optionsList">
</multiple-autocomplete>
  1. beforeRemoveItem : Listen for event before adding an item
$scope.beforeRemoveItem = function(item){
    // perform operation on this item before removing it.
}
<multiple-autocomplete ng-model="selectedList"
     before-remove-item="beforeRemoveItem"
     suggestions-arr="optionsList">
</multiple-autocomplete>
  1. afterRemoveItem : Listen for event before adding an item
$scope.afterRemoveItem = function(item){
    // perform operation on this item after removing it.
}
<multiple-autocomplete ng-model="selectedList"
     after-remove-item="afterRemoveItem"
     suggestions-arr="optionsList">
</multiple-autocomplete>
  1. close-after-selected : To make the list close after selected an item
    <multiple-autocomplete ng-model="skills2" 
                           name="multipleSelect"
                           required="true"
                           close-after-selected="true"                    
                           suggestions-arr="skillsList1">
    </multiple-autocomplete>
  1. placeholder : Set placeholder
    <multiple-autocomplete ng-model="skills2" 
                           name="multipleSelect"
                           placeholder="Digit here"
                           suggestions-arr="skillsList1">
    </multiple-autocomplete>

#Getting started

Install "angular-multiple-select" from bower or npm and save it in your package.json or bower.json. For Example :

$ npm install --save angular-multiple-select;
$ bower install --save angular-multiple-select

After installation include its

multiple-select.min.css AND
multiple-select.min.js
<script src="/bower_components/angular-multiple-select/build/multiple-select.min.js"></script>
<link href="/bower_components/angular-multiple-select/build/multiple-select.min.css" rel="stylesheet">

in your html. Then,

Include multipleSelect module in your app: For example :

angular.module('yourModuleName', [
    'multipleSelect'
]);

Now angularMultipleSelect module is injected in your module. You are ready to use it.

You can use it in 2 ways in your form :

  1. If your options list is an array of objects, like :
$scope.optionsList = [
  {id: 1,  name : "Java"},
  {id: 2,  name : "C"},
  {id: 3,  name : "C++"},
  {id: 4,  name : "AngularJs"},
  {id: 5,  name : "JavaScript"}
];
<multiple-autocomplete ng-model="selectedList"
     object-property="name"
     suggestions-arr="optionsList">
</multiple-autocomplete>

Here, in "suggestions-arr" you have to provide the options list from which user can select multiple value. and, "object-property" is which you want to show to user. In above example "name" is the property which i want to show.

"ng-model" will give you an array of selected things. For Ex : If user selects Java & C++, then

ng-model will have
selectedList = [
      {id: 1,  name : "Java"},
      {id: 3,  name : "C++"}
  ]
  1. If your options list is an array of strings, like :
$scope.optionsList = [
  "Java",
  "C",
  "C++",
  "AngularJs",
  "JavaScript"
];
<multiple-autocomplete ng-model="selectedList"
     suggestions-arr="optionsList">
</multiple-autocomplete>

Here, in "suggestions-arr" you have to provide the options list from which user can select multiple value.

"ng-model" will give you an array of selected things. For Ex : If user selects Java & C++, then

ng-model will have
selectedList = [
      "Java",
      "C++"
  ]
  1. To make it required Field in a form
<form name="multipleSelectForm" novalidate>
    <div ng-class="{'has-error' : multipleSelectForm.multipleSelect.$invalid && multipleSelectForm.multipleSelect.$dirty, 'has-success' : !multipleSelectForm.multipleSelect.$invalid && multipleSelectForm.multipleSelect.$dirty}">
        <label>3. Making it Required field in a Form</label>
        <multiple-autocomplete ng-model="skills2" name="multipleSelect" required="true"
                               suggestions-arr="skillsList1">
        </multiple-autocomplete>
        <span ng-show="multipleSelectForm.multipleSelect.$invalid && multipleSelectForm.multipleSelect.$dirty" class="ng-hide">
            <p class="error-msg" ng-show="multipleSelectForm.multipleSelect.$error.required">Please select something from multiple select field</p>
        </span>
    </div>
    <br/>
    <button type="button" class="btn btn-default" ng-click="onSubmit()">Submit Form</button>
</form>
  1. Fetching options list from 3rd party api/url

    You can specify "api-url-option" and you can process response before we capture it. Below is the Example :

        $scope.apiUrlOption = {
            method : "GET",
            responseInterceptor : function (response) {
                /*
                *
                * Process response acc. to your requirement.
                * After processing we are assuming "response.data" as "suggestionsArr".
                *
                * */
            }
        };
     Then in html no need to specify property in "object-property" attribute in directive
    
        <multiple-autocomplete ng-model="skillsFromApi"
                               api-url="{{apiPath}}"
                               suggestions-arr=""
                               api-url-option="apiUrlOption">
        </multiple-autocomplete>

    Part 1. If your Api return an array of strings like :

    [
      "Java",
      "C",
      "C++",
      "AngularJs",
      "JavaScript"
    ]
Then in html no need to specify property in "object-property" attribute in directive
    <multiple-autocomplete ng-model="skillsFromApi"
                           api-url="{{apiPath}}"
                           suggestions-arr="">
    </multiple-autocomplete>
Part 2. If your Api return an array of objects like :
    [
      {id: 1,  name : "Java"},
      {id: 2,  name : "C"},
      {id: 3,  name : "C++"},
      {id: 4,  name : "AngularJs"},
      {id: 5,  name : "JavaScript"}
    ]
Then in html you need to specify property in "object-property" attribute in directive
Here in this case, you have to do like this :
    <multiple-autocomplete ng-model="skillsFromApi"
                            object-property="name"
                           api-url="{{apiPath}}"
                           suggestions-arr="">
    </multiple-autocomplete>

For any suggestions, issues, Query, etc. Please feel free to let me know. Thanks :)

The MIT License (MIT)

Copyright (c) 2015 Jagdeep Singh

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

angularmultipleselect's People

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

angularmultipleselect's Issues

Templating

Seguestion: if its not possible it would be nice to provide our own template, for example i would like to integrate it with angular material library so it would be nice if i could provide my own template

Remove dependencies

The actual multi-select doesn't require bootstrap or jquery to run as far as i am aware, i know its needed for the web, but should it really be a dependency?

Why package.json dependencies are not all devDependencies?

We are attempting to use your package and when applied with the "--production" flag we get a ton of devDep code from this package. It seems all your "dependencies" should be "devDependencies", is there are reason this is not the case? If not, is there any way this could be changed? Thanks for the time.

TypeError: Cannot read property 'push' of undefined

Hello,

I appreciate this work.

I have managed to use this in my project but when I select any value from available items.

Then I get an error in console: "TypeError: Cannot read property 'push' of undefined" at line number 132 of directive.

It seems like scope.modelArr is not getting initialized to an array.

Below is my code:

object-property="flatName" api-url="{{vm.flatsApiPath}}" suggestions-arr="">

Can you please provide any solution for this?

Raising Events

Great little plugin works fantastically!

A great addition to this would be assigning some kind of callback to the directive which is called when the onSuggestedItemsClick() event is raised.

In my case I want to do additional work each time my ng-model passed to the directive changes.

Have you thought about making this into a .component() also?

more property

Hi,
how i can show two properties (example Name and Lastname of my object) ?
Thanks
Nicola

All inputs with errors have green border on focus

According to bootstrap when a parent of an input has has-error class the input must have red border, but angular-multiple-select has a general css rule which breaks that:

.has-error .form-control:focus {
    border-color: #1cba7d;  /* green color */
    -webkit-box-shadow: none;
    box-shadow: none;
}

Please remove this global rule from css and don't change any standard bootstrap classes globally. You must use your own classes to avoid changing view of other elements on website.

Already select items with an existing array

I am trying to select a few items from the suggestions-arr with items that are already in the ng-model. Couldn't find a use-case so I created this issue.

Getting the following error:
Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: item in modelArr, Duplicate key: string:", Duplicate value: "

When I use track by I'll get the next error:
Error: [$parse:syntax] Syntax Error: Token 'track' is an unexpected token at column 13 of the expression [$ctrl.types track by name] starting at [track by name].

My select code:

<multiple-autocomplete
    ng-model="$ctrl.acceptedTypes"
    object-property="name"
    suggestions-arr="$ctrl.types"
    id="acceptedTypes">
</multiple-autocomplete>

$ctrl is used because I work with component instead of directives

Anybody an idea how to solve this?

Too many NPM packages

Hi, I am still a little new to diving into NPM packages and Webpack. I am using this module for some time and really loving it.

However, when I use npm i angular-multiple-select --save-dev it installs around 480 modules which are around 70MB in my node_modules folder.

If I remove all those modules that I don't need, but keep 'angular-multiple-select' which is 70KB, it still works!

So my question is, is it possible to download only the necessary module when using npm install?

Thanks in advance.

Cannot limit selections in MultipleSelect

Hi Guys,

I've got MutlipleSelect working, but I need to limit the selections a user can make from the available selections (i.e. I set the field to invalid, so that the user cannot save the form, when the maximum selections are exceeded). I've tried using:

  • after-select-item
  • ng-change
  • custom validation directive

None of these work. I can get ng-change & custom validation directive to work on other input fields, but not on the tag. Nothing is triggered when the user makes selections from the dropdown list.
Here is a simple sample of the code used with after-select-item method. I'm trying to limit the user to 3 or less selections from the available options in the 'cats' array:

HTML:

Controller.js:
$scope.afterSelectItem = function(selectedCats) {
var catLength = $scope.selectedCats.length;
var valid = (catLength <= 3);
$scope.myStoryForm.selectedCats.$setValidity("maxLength", valid);
};

Again, nothing get triggered in this controller when selections are been made (checked at console).

Is there any issue/defect here, or is it something I'm not doing right?
Thanks.

Where are the api-url options?

Hello,

How do I manage the api-URL options

        <multiple-autocomplete ng-model="peopleFromApi"
                               object-property="name"
                               api-url="/api/myApi"
                               api-url-options="???"

And where there is a codepen/jsfiddle for the documentation (the link provided in the doc does not display sources) ?

Validate bower.json

Hi,

I have downloaded you package through bower and I am encountering some issues with the dependency to angular. There is a bower.json which lists angular as a dependency, and there's also a .bower.json (notice the . in front) that lists angularjs. Now, when doing bower install, I have both angular (from my project's dependencies) and angularjs (which was probably found in the .bower.json file)
Could you take a look into why is angularjs listed as a dependency instead of angular?

Thanks for looking into this,
Andrei

Bug! Do not support mobile device

Hi,
I found this module do not support mobile device as u click the dropdown botton, yes it would dropdown all the options. But U cannot cancel the dropdown after U finished the selection. Normally when U finish selection, user should click anywhere in mobile device and then the module would not be in dropdown state.
Hope can be fixed ASAP. Cheers.

We cannot select nested objects?

I have an nested object as follows:

testObj = {
user : {
email : 'akshay'
}
};

When I put user.email in object-property, it gives undefined.

Web browser autocomplete turn off.

If I click in the input field a second time, the browser field autocomplete pops up on top of the one for this control, resulting in a very awkward interaction.

Binding ng-model to an existing array of values does not auto-remove those items from suggestions-arr

Right now the default is to allow a single instance of each item from suggestions-arr to be selected, after which the control removes the selected item from suggestions-arr. The control should have the option to allow or disallow multiple items from the suggestions-arr to be added to the ng-model, and auto-remove on first binding those items in ng-model from suggestions-arr. At this time it does not, and there will be duplicates between the ng-model and suggestions-arr on rebinding unless I remove items from suggestions-arr manually.

Please provide suggestion array list or url???

Hi, really a good work with multiple autocomplete select, but when i try it on my project console returns
*****Angular-multiple-select **** ----- Please provide suggestion array list or url

In my HTML

In my Controller in JS
$scope.optionsList = [
{id: 1, name : "Java"},
{id: 2, name : "C"},
{id: 3, name : "C++"},
{id: 4, name : "AngularJs"},
{id: 5, name : "JavaScript"}
];

What would be wrong? Thanks

No way to put value and option different

Hello Jagdeep,
Your tool is pretty useful and elegant. I am using in most of my projects. However, I have one issue.

var myList= {
{'id':'different key1','value':'Different value 1'},
{'id':'different key2','value':'Different value 2'},
{'id':'different key3','value':'Different value 3'},
};

Can we make it possible to fill like below from multiple-autocomplete?
Different value 1 Different value 2 Different value 3

Thanks!

Updates:
Thank you for the quick response. Yes, I think the one that I was asking is already there. However, it is little strange that I cannot use some of the ng directives with it. Like ng-disabled

FYI: I am appending the updates here because it looks like I cannot reply your comment.

Event listeners

Event listeners are not working?
before-select-item
after-select-item etc

set placeholder

I think it should be possible to set a placeholder like I can do for 'normal input-text-fields'.

e.g.: "click here to add items"

It seems like the current behaviour is to always set it to "" (empty).

ngDisabled with multiple-autocomplete

I need to disable the multiple-autocomplete input based on another one. But ng-disable directive simply doesn't do anything. Is there a way to use ngDisable with this?

Alternatively, I'm using ng-if for now until I can make ng-disabled work.

Thanks!

Is grouping possible ?

I have a list of books on different categories. I need not show the category name above it. Is it possible ?

Not able to use any of the directive listed.

Hello @jagdeep-singh @jagdeepsingh91
I think your angularMultipleSelect is quite popular. I have been using it in most of my projects these days.
However, I have one issue.

I am not able to use any of the directives listed such as before-select-item="beforeSelectItem" after-select-item="afterSelectItem" before-remove-item="beforeRemoveItem" after-remove-item="afterRemoveItem"
To demonstrate, I have used the Plunker with your reference.

Also, it will be really better if you can dynamically display Clear All button to clear the list.
Thank you!

Plunker: https://plnkr.co/edit/W8fCTfgwoTCG9RFjEM4F

Events not raised in minified version

I am using the afterSelectItem and afterRemoveItem listeners. I have found my listeners are not invoked when I use the minified version of the library. When I use the regular version the listeners work fine.

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.