multiselectable dropdown checkboxes using angular js

All we need is an easy explanation of the problem, so here it is.

I want to design a dropdownlist of checkboxes and make the checkboxes multi-selectable. I have used the below code,but I am unable to make multiple selections as the template refreshes each time I click on a checkbox,please suggest some ideas?

{   <ul class="status-select" class="status-select" ng-if="$index == selectedFilterIndex">
     <li ng-repeat="DataValue in filter.Data.Value">
        <input type="checkbox" ng-click="OnDropDownSelectionChanged(filter,DataValue)">
                        {{DataValue.displayText}}
     </li>
     </ul> 
}

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

You can use directive like angularjs-dropdown-multiselect, which you can find very easily on internet

Here are some example:

  1. angularjs-dropdown-multiselectFiddle

  2. multiselectDropdownFiddle

  3. Another example of angularjs-dropdown-multiselectFiddle

Code Snippet:

var myApp = angular.module('exampleApp', ['angularjs-dropdown-multiselect']);

myApp.controller('appController', ['$scope',
  function($scope) {

    $scope.example13model = [];
    $scope.example13data = [{
      id: 1,
      label: "David"
    }, {
      id: 2,
      label: "Jhon"
    }, {
      id: 3,
      label: "Lisa"
    }, {
      id: 4,
      label: "Nicole"
    }, {
      id: 5,
      label: "Danny"
    }];

    $scope.example13settings = {
      smartButtonMaxItems: 3,
      smartButtonTextConverter: function(itemText, originalItem) {
        if (itemText === 'Jhon') {
          return 'Jhonny!';
        }

        return itemText;
      }
    };
  }
]);
div, h1, a {
  padding: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.20/angular.min.js"></script>
<link href="https://bootswatch.com/slate/bootstrap.css" rel="nofollow noreferrer noopener" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.0/lodash.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angularjs-dropdown-multiselect/1.2.0/angularjs-dropdown-multiselect.min.js"></script>

<h2>
    Example of sytled angularjs-dropdown-multiselect  
</h2>

<a href="http://dotansimha.github.io/angularjs-dropdown-multiselect/#/" rel="nofollow noreferrer noopener">Source documentation</a>

<div ng-app="exampleApp" ng-controller="appController">

  <div ng-dropdown-multiselect="" options="example13data" selected-model="example13model" extra-settings="example13settings"></div>

</div>

Method 2

a simple workaround that I’ve found is using Angular Directives for Bootstrap using the auto-close="outsideClick" and the code should be like this:

<div class="dropdown" uib-dropdown auto-close="outsideClick" on-toggle="toggled(open)">
  <a class="btn btn-primary" uib-dropdown-toggle>Toggle</a>
  <ul class="dropdown-menu status-select" class="status-select" ng-if="$index == selectedFilterIndex" uib-dropdown-menu>
    <li ng-repeat="DataValue in filter.Data.Value">
      <div class="checkbox">
        <label>
          <input type="checkbox" ng-click="OnDropDownSelectionChanged(filter,DataValue)">
                      {{DataValue.displayText}}
        </label>
      </div>
    </li>
  </ul>
</div>

Method 3

You could go for Angular Material Design Library

Select with Option Groups

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply