Multi-select widget angular js
HTML
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css">
<script src="http://codeorigin.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://code.angularjs.org/1.0.7/angular.min.js"></script>
<div ng-controller="MyCtrl">
<multi-select ng-model="user.roles" available="roles" selected-label="Current roles" available-label="Available roles" display-attr="roleName"></multi-select>
<div>{{ user.username }}'s roles: </div>
<ul>
<li ng-repeat="role in user.roles">{{ role.roleName }}</li>
</ul>
<button ng-click="changeRoles()">Change Roles</button>
<div label="Console:">
{{roles}}
</div>
</div>
CSS
.multiSelect {
overflow: auto;
}
.multiSelect .select {
margin-right: 20px;
float: left;
}
.multiSelect .text {
clear: both;
font-size: 11px;
}
.multiSelect label::after {
content: ":";
}
.btn.mover {
display: block;
margin-top: 25px;
vertical-align: top;
}
.btn.mover.left {
padding: 3px 8px 2px 6px;
}
.btn.mover.right {
margin-top: 24px;
padding: 3px 7px 2px;
}
.btn.mover i {
margin: 0;
}
/* demo css */
li {
list-style-type: disc;
}
}
JavaScript
var myApp = angular.module('myApp',[]);
myApp.controller('MyCtrl', function($scope) {
$scope.roles = [
{roleId: 1, roleName: "Administrator"},
{roleId: 2, roleName: "Super User"}
];
$scope.consoleMessages ="";
$scope.user = {
userId: 1,
username: "JimBob",
roles: [$scope.roles[0]]
};
$scope.changeRoles = function() {
$scope.roles = [
{roleId: 3, roleName: "New Administrator"},
{roleId: 4, roleName: "New Super User"}
];
console.log("rolses");
}
});
myApp.directive('multiSelect', function($q) {
return {
restrict: 'E',
require: 'ngModel',
scope: {
selectedLabel: "@",
availableLabel: "@",
displayAttr: "@",
available: "=",
model: "=ngModel"
},
template: '<div class="multiSelect">' +
'<div class="select">' +
'<label class="control-label" for="multiSelectSelected">{{ selectedLabel }} ' +
'({{ model.length }})</label>' +
'<select id="currentRoles" ng-model="selected.current" multiple ' +
'class="pull-left" ng-options="e as e[displayAttr] for e in model">' +
'</select>' +
'</div>' +
'<div class="select buttons">' +
'<button class="btn mover left" ng-click="add()" title="Add selected" ' +
'ng-disabled="selected.available.length == 0">' +
'<i class="icon-arrow-left"></i>' +
'</button>' +
'<button class="btn mover right" ng-click="remove()" title="Remove selected" ' +
'ng-disabled="selected.current.length == 0">' +
'<i class="icon-arrow-right"></i>' +
'</button>' +
'</div>' +
'<div class="select">' +
'<label class="control-label"...