Skip to content
This repository was archived by the owner on Feb 7, 2023. It is now read-only.

Protips #32

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 52 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,55 @@
angular-multiselect
===================
# A Native AngularJS multiselect directive

Native AngularJS multiselect directive - Work in progress. Contributions welcome!

Example: http://plnkr.co/edit/LPGYIf?p=preview
#### index.html
```html
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<script src="app.js"></script>
<script src="multiselect.js"></script>
</head>
<body ng-controller="MainCtrl">

License
=======
The MIT License
<multiselect multiple="true" ng-model="selectedCar" options="car.id as car.name for car in cars" />
<div class="well well-small">
{{selectedCar}}
</div>

</body>
</html>
```


#### app.js
```js
var app = angular.module('plunker', ['ui.multiselect']);

app.controller('MainCtrl', function($scope) {
$scope.cars = [{id:1, name: 'Audi'}, {id:2, name: 'BMW'}, {id:3, name: 'Honda'}];
$scope.selectedCar = [];
});
```


#### After selecting 'Audi' and 'Honda', the model will look like:
```
$scope.selectedCar = [1, 3];
```


#### Change options to `car.name for car in cars` for a data model like:
```
$scope.selectedCar = [{id:1, name: 'Audi'}, {id:3, name: 'Honda'}];
```

#### Changing options to `car.id for car in cars` will change the options labels in the UI:
`'Audi', 'BMW', 'Honda'` -> `'1', '2', '3'`


## Example
http://plnkr.co/edit/LPGYIf?p=preview

## License

MIT
53 changes: 38 additions & 15 deletions src/multiselect.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ angular.module('ui.multiselect', [])
scope.$destroy();
});

var popUpEl = angular.element('<multiselect-popup></multiselect-popup>');
var popUpEl = angular.element('<multiselect-popup multiple="'+scope.multiple+'"></multiselect-popup>');

//required validator
if (attrs.required || attrs.ngRequired) {
Expand Down Expand Up @@ -236,37 +236,60 @@ angular.module('ui.multiselect', [])
link: function (scope, element, attrs) {

scope.isVisible = false;
scope.eventHandlerIsBound = false;
scope.multiple = attrs.multiple ? true : false;

scope.toggleSelect = function () {
if (element.hasClass('open')) {
element.removeClass('open');
$document.unbind('click', clickHandler);
} else {
element.addClass('open');
$document.bind('click', clickHandler);
scope.bindEventHandler();
scope.resize();
scope.focus();
}
};

function clickHandler(event) {
if (elementMatchesAnyInArray(event.target, element.find(event.target.tagName)))
scope.bindEventHandler = function() {
if (scope.eventHandlerIsBound) {
return;
element.removeClass('open');
$document.unbind('click', clickHandler);
scope.$apply();
}
element.find('ul, ul *').bind('click', function(event) {
if ((scope.multiple && 'A' == event.target.tagName) || (!scope.multiple && 'A' != event.target.tagName)) {
event.stopPropagation();
}
else {
var inSelection = false
, aTags = element.find('a');
for (var i = 0; i < aTags.length; i++) {
if ($(event.target).parents('a:first')[0] == aTags[i]) {
inSelection = true;
break;
}
}
if (scope.multiple && inSelection) {
$(event.target).parents('a').click();
event.stopPropagation();
}
}
});
scope.eventHandlerIsBound = true;
}

scope.resize = function() {
var $ul = element.find('ul')
, margin = 50
, top = $ul.position().top
, maxHeight = $(window).innerHeight();
if (($ul.height() + top) > maxHeight) {
$ul.css({height: (maxHeight - top - margin), overflow: 'scroll'});
}
}

scope.focus = function focus(){
var searchBox = element.find('input')[0];
searchBox.focus();
}

var elementMatchesAnyInArray = function (element, elementArray) {
for (var i = 0; i < elementArray.length; i++)
if (element == elementArray[i])
return true;
return false;
}
}
}
}]);