2

I am trying to use bootstrap-multiselect in my AngularJS app.

I've added the following in my master page (index.html).

<script src="/bower_components/jquery/dist/jquery.min.js"></script>
<script src="/bower_components/angular/angular.min.js"></script>
<script src="/bower_components/angular-ui-router/release/angular-ui-router.min.js"></script>
<script src="/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>

<script src="/bower_components/bootstrap-multiselect/dist/js/bootstrap-multiselect.js"></script>
<link rel="stylesheet" href="bower_components/bootstrap-multiselect/dist/css/bootstrap-multiselect.css">

The multiselect dropdown appears, but I am unable to select the multiple options available.

In html template view I added the following.

   <select id="example-getting-started" multiple="multiple">
        <option value="cheese">Cheese</option>
        <option value="tomatoes">Tomatoes</option>
        <option value="mozarella">Mozzarella</option>
        <option value="mushrooms">Mushrooms</option>
        <option value="pepperoni">Pepperoni</option>
        <option value="onions">Onions</option>
    </select>

and added the following script in the template.

 $(document).ready(function () {
       $('#example-getting-started').multiselect();
 });

My list appears, but I'm unable to select from them.

enter image description here

Am I missing something here?

3
  • What jQuery version are you using? Commented Oct 23, 2015 at 17:57
  • 2
    It will not work like this. I suggest to read about proper usage of jQuery plugins in Angular app. You need to use directives. Commented Oct 23, 2015 at 18:21
  • @dfsq Could you please post this as an answer so that I can accept it? It worked like a charm Commented Oct 23, 2015 at 18:35

2 Answers 2

2

You cannot use the Bootstrap-Multiselect plugin directly, what you will have to do is define a Directive to use the multiselect options.

HTML Code

<div ng-controller="MainCtrl">
<select id="example-getting-started" multiple="multiple"  name="multiselect[]" data-dropdownmultiselect>    
    <option data-ng-repeat="option in options" value="{{getOptionId(option)}}" ng-selected="isOptionSelected(option)" data-ng-bind-template="{{option.name}}">
    </option> 
</select>
</div>

App.js

app.controller('MainCtrl', function($scope, $timeout) {

  $scope.options = [{
        "name": "Option 1",
        "id": "option1",
        "selected": false
      }, {
        "name": "Option 2",
        "id": "option2",
        "selected": true
      }, {
        "name": "Option 3",
        "id": "option3",
        "selected": true
      }, {
        "name": "Option 4",
        "id": "option4",
        "selected": false
      }, {
        "name": "Option 5",
        "id": "option5",
        "selected": false
      }, {
        "name": "Option 6",
        "id": "option6",
        "selected": false
      }];

  // You might need this timeout to be sure its run after DOM render.
  $timeout(function() { 

      $('#example-getting-started').multiselect({
        disableIfEmpty: true,
        maxHeight: 400,
        includeSelectAllOption: true,
        selectAllText: 'Select All',
        enableFiltering: true,
        enableCaseInsensitiveFiltering: true,
        filterPlaceholder: 'Search',
        onChange: function(element, checked) {
          alert("selected");
        }
      });
  }, 2, false);

  $scope.getOptionId = function(option) {
      return option.id;
  };

  $scope.isOptionSelected = function(option) {
      var selected;
      if (option.selected) {
        selected = "selected"
      }
      return selected;
  };
});

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

0

Angular doesn't accept link tags without href. So somewhere around line 330 add href='#' and try again.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.