skip to Main Content

I found this example wand I want to use it in my tutorial project.

The problem is that in this

'use strict';

var app = angular.module('myApp', ['app.directives']);

app.controller('AppCtrl', function($scope){                     
    $scope.roles = [
          {"id": 1, "name": "Manager", "assignable": true},
          {"id": 2, "name": "Developer", "assignable": true},
          {"id": 3, "name": "Reporter", "assignable": true}
    ];
    
    $scope.member = {roles: []};
    $scope.selected_items = [];
});

var app_directives = angular.module('app.directives', []);

app_directives.directive('dropdownMultiselect', function(){
   return {
       restrict: 'E',
       scope:{           
            model: '=',
            options: '=',
            pre_selected: '=preSelected'
       },
       template: "<div class='btn-group' data-ng-class='{open: open}'>"+
        "<button class='btn btn-small'>Select</button>"+
                "<button class='btn btn-small dropdown-toggle' data-ng-click='open=!open;openDropdown()'><span class='caret'></span></button>"+
                "<ul class='dropdown-menu' aria-labelledby='dropdownMenu'>" + 
                    "<li><a data-ng-click='selectAll()'><i class='icon-ok-sign'></i>  Check All</a></li>" +
                    "<li><a data-ng-click='deselectAll();'><i class='icon-remove-sign'></i>  Uncheck All</a></li>" +                    
                    "<li class='divider'></li>" +
                    "<li data-ng-repeat='option in options'> <a data-ng-click='setSelectedItem()'>{{option.name}}<span data-ng-class='isChecked(option.id)'></span></a></li>" +                                        
                "</ul>" +
            "</div>" ,
       controller: function($scope){
           
           $scope.openDropdown = function(){        
                    $scope.selected_items = [];
                    for(var i=0; i<$scope.pre_selected.length; i++){                        $scope.selected_items.push($scope.pre_selected[i].id);
                    }                                        
            };
           
            $scope.selectAll = function () {
                $scope.model = _.pluck($scope.options, 'id');
                console.log($scope.model);
            };            
            $scope.deselectAll = function() {
                $scope.model=[];
                console.log($scope.model);
            };
            $scope.setSelectedItem = function(){
                var id = this.option.id;
                if (_.contains($scope.model, id)) {
                    $scope.model = _.without($scope.model, id);
                } else {
                    $scope.model.push(id);
                }
                console.log($scope.model);
                return false;
            };
            $scope.isChecked = function (id) {                 
                if (_.contains($scope.model, id)) {
                    return 'icon-ok pull-right';
                }
                return false;
            };                                 
       }
   } 
});
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js"></script>
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.0/css/bootstrap-combined.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="AppCtrl">    
    <dropdown-multiselect pre-selected="member.roles" model="selected_items" options="roles"></dropdown-multiselect>
    
    
    <pre>selected roles = {{selected_items | json}}</pre>
</div>

the property defined hard-coded:

  name
    id

So if name of my fields called another the example will not work.

Any idea what have I to change in my code to make the example work properly if fields called diffrent names?

2

Answers


  1. While I think it’s perfectly fine with forcing input to use determined key as part of the API myself, it’s entirely possible to customize that, by passing additional parameters into the directive.

    .directive(function(){
        return {
            scope: {
                options: '=',
                keys: '='
            },
            template: "<div ng-repeat='option in options'> 
                    {{option[key.id]}} - {{option[key.name]}} 
                <div>",
            controller: function($scope){
                // default if key is not specified
                if (!$scope.keys) $scope.keys = {id: 'id', name: 'name'};
    
                angular.forEach($scope.options, function(option){
                    // refer to id by option[$scope.key.id]
                    // refer to name by option[$scope.key.name]
                });
            }
        }
    })
    

    When you pass into directive you’ll need to tell it which key is id and name respectively

    $scope.test = [
        {serial: 123, product: 'apple'},
        {serial: 124, product: 'orange'}
    ]
    
    $scope.keys = {id: 'serial', name: 'product'};
    
    <dropdown-multiselect model="selected_items" options="test" keys="keys"></dropdown-multiselect>
    
    Login or Signup to reply.
  2. 'use strict';
    
    var app = angular.module('myApp', ['app.directives']);
    
    app.controller('AppCtrl', function($scope){                     
        $scope.roles = [
              {"id": 1, "name1": "Manager", "assignable": true},
              {"id": 2, "name1": "Developer", "assignable": true},
              {"id": 3, "name1": "Reporter", "assignable": true}
        ];
        
        $scope.member = {roles: []};
        $scope.selected_items = [];
        $scope.displayname = "name1";
    });
    
    var app_directives = angular.module('app.directives', []);
    
    app_directives.directive('dropdownMultiselect', function(){
       return {
           restrict: 'E',
           scope:{           
                model: '=',
                options: '=',
                displayname: '=',
                pre_selected: '=preSelected'
           },
           template: "<div class='btn-group' data-ng-class='{open: open}'>"+
            "<button class='btn btn-small'>Select</button>"+
                    "<button class='btn btn-small dropdown-toggle' data-ng-click='open=!open;openDropdown()'><span class='caret'></span></button>"+
                    "<ul class='dropdown-menu' aria-labelledby='dropdownMenu'>" + 
                        "<li><a data-ng-click='selectAll()'><i class='icon-ok-sign'></i>  Check All</a></li>" +
                        "<li><a data-ng-click='deselectAll();'><i class='icon-remove-sign'></i>  Uncheck All</a></li>" +                    
                        "<li class='divider'></li>" +
                        "<li data-ng-repeat='option in options'> <a data-ng-click='setSelectedItem()'>{{option[displayname ]}}<span data-ng-class='isChecked(option.id)'></span></a></li>" +                                        
                    "</ul>" +
                "</div>" ,
           controller: function($scope){
               console.log($scope.displayname );
               $scope.openDropdown = function(){        
                        $scope.selected_items = [];
                        for(var i=0; i<$scope.pre_selected.length; i++){                        $scope.selected_items.push($scope.pre_selected[i].id);
                        }                                        
                };
               
                $scope.selectAll = function () {
                    $scope.model = _.pluck($scope.options, 'id');
                    console.log($scope.model);
                };            
                $scope.deselectAll = function() {
                    $scope.model=[];
                    console.log($scope.model);
                };
                $scope.setSelectedItem = function(){
                    var id = this.option.id;
                    if (_.contains($scope.model, id)) {
                        $scope.model = _.without($scope.model, id);
                    } else {
                        $scope.model.push(id);
                    }
                    console.log($scope.model);
                    return false;
                };
                $scope.isChecked = function (id) {                 
                    if (_.contains($scope.model, id)) {
                        return 'icon-ok pull-right';
                    }
                    return false;
                };                                 
           }
       } 
    });
    <script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js"></script>
    <link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.0/css/bootstrap-combined.min.css" rel="stylesheet"/>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <div ng-app="myApp" ng-controller="AppCtrl">    
        <dropdown-multiselect pre-selected="member.roles" model="selected_items"  displayname ="displayname" options="roles"></dropdown-multiselect>
        
        
        <pre>selected roles = {{selected_items | json}}</pre>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search