skip to Main Content

I am new to web programming and especially to css and bootstrap.

I found implemintation of multiselect-dropbox in angularjs:

'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="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<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"/>

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

I want to use it in my project.
But I need to replace the appearance of the images.

Here how it looks now:

enter image description here

And I want the delete and checked signs will appear on another side:

enter image description here

Any idea how to implement it?
What changes I have to make changes on external resources files?

2

Answers


  1. Not sure, is this right way or not, but its work for you

    Change position of Text Check All and Uncheck All

    "<li><a data-ng-click='selectAll()'>Check All <i class='icon-ok-sign'></i></a></li>" +
    "<li><a data-ng-click='deselectAll();'>  Uncheck All<i class='icon-remove-sign'></i></a></li>"
    
    '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()'>Check All <i class='icon-ok-sign'></i></a></li>" +
                        "<li><a data-ng-click='deselectAll();'>Uncheck All <i class='icon-remove-sign'></i></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="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    
    <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"/>
    
    <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>
    Login or Signup to reply.
  2. For the first 2, you need to change the template to move the icons to after the text.

    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()'>Check All <i class='icon-ok-sign'></i></a></li>" +
                    "<li><a data-ng-click='deselectAll();'>Uncheck All <i class='icon-remove-sign'></i></a></li>" +                    
                    "<li class='divider'></li>" +
                    "<li data-ng-repeat='option in options'> <a data-ng-click='setSelectedItem()'>&nbsp;&nbsp;{{option.name}}<span data-ng-class='isChecked(option.id)'></span></a></li>" +                                        
                "</ul>" +
            "</div>" ,
    

    For the options list you need to change the isChecked method and replace pull-right by pull-left

    $scope.isChecked = function (id) {
        if (_.contains($scope.model, id)) {
            return 'icon-ok pull-left';
        }
        return false;
    };
    

    Notice that I added a couple of &nbsp; to the template to give some gap between the icons and the text. You could do the same thing in CSS too.

    '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()'>Check All <i class='icon-ok-sign'></i></a></li>" +
                        "<li><a data-ng-click='deselectAll();'>Uncheck All <i class='icon-remove-sign'></i></a></li>" +                    
                        "<li class='divider'></li>" +
                        "<li data-ng-repeat='option in options'> <a data-ng-click='setSelectedItem()'>&nbsp;&nbsp;{{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-left';
            }
            return false;
        };                                
           }
       } 
    });
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    
    <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"/>
    
    <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>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search