Git Product home page Git Product logo

angularmultipleselect's Introduction

angularMultipleSelect

A complete Angularjs directive for multiple select autocomplete

#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>

#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 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

Contributors

arrow7000 avatar doughayward avatar jagdeep-singh avatar jagdeepsingh91 avatar

Watchers

 avatar  avatar

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.