<!DOCTYPE html>
<html lang="en" ng-app="multiSelectApp">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AngularJS Multi-Select Autocomplete</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
<script type="module" src="https://unpkg.com/ionicons@7.1.0/dist/ionicons/ionicons.esm.js"></script>
<script nomodule src="https://unpkg.com/ionicons@7.1.0/dist/ionicons/ionicons.js"></script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
background: linear-gradient(135deg, #ef233c 0%, #d90429 100%);
min-height: 100vh;
padding: 20px;
display: flex;
align-items: center;
justify-content: center;
}
.container {
background: white;
border-radius: 16px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
padding: 40px;
width: 100%;
max-width: 700px;
}
h1 {
font-size: 28px;
color: #333;
margin-bottom: 8px;
text-align: center;
font-weight: 600;
}
.subtitle {
color: #666;
font-size: 14px;
margin-bottom: 30px;
text-align: center;
}
.multiselect-container {
position: relative;
margin-bottom: 30px;
}
.multiselect-label {
display: block;
font-size: 14px;
font-weight: 600;
color: #333;
margin-bottom: 8px;
}
.multiselect-input-wrapper {
border: 2px solid #e0e0e0;
border-radius: 12px;
padding: 10px;
min-height: 50px;
cursor: text;
transition: border-color 0.3s ease;
background: white;
}
.multiselect-input-wrapper:focus-within {
border-color: #ef233c;
box-shadow: 0 0 0 3px rgba(239, 35, 60, 0.1);
}
.selected-items {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-bottom: 8px;
}
.selected-item {
background: linear-gradient(135deg, #ef233c 0%, #d90429 100%);
color: white;
padding: 6px 12px;
border-radius: 20px;
font-size: 14px;
display: flex;
align-items: center;
gap: 8px;
animation: slideIn 0.2s ease;
}
@keyframes slideIn {
from {
opacity: 0;
transform: scale(0.8);
}
to {
opacity: 1;
transform: scale(1);
}
}
.selected-item-remove {
background: none;
border: none;
color: white;
cursor: pointer;
padding: 0;
display: flex;
align-items: center;
font-size: 18px;
transition: transform 0.2s ease;
}
.selected-item-remove:hover {
transform: scale(1.2);
}
.search-input {
border: none;
outline: none;
font-size: 15px;
padding: 8px;
flex: 1;
min-width: 150px;
}
.dropdown {
position: absolute;
top: 100%;
left: 0;
right: 0;
background: white;
border: 2px solid #ef233c;
border-radius: 12px;
margin-top: 8px;
max-height: 300px;
overflow-y: auto;
box-shadow: 0 8px 24px rgba(239, 35, 60, 0.2);
z-index: 1000;
animation: dropdownSlide 0.2s ease;
}
@keyframes dropdownSlide {
from {
opacity: 0;
transform: translateY(-10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.dropdown-item {
padding: 12px 16px;
cursor: pointer;
transition: background 0.2s ease;
display: flex;
align-items: center;
gap: 10px;
border-bottom: 1px solid #f0f0f0;
}
.dropdown-item:last-child {
border-bottom: none;
}
.dropdown-item:hover {
background: #fff5f5;
}
.dropdown-item.selected {
background: #ffeef0;
color: #ef233c;
font-weight: 600;
}
.dropdown-item ion-icon {
font-size: 20px;
color: #ef233c;
}
.no-results {
padding: 20px;
text-align: center;
color: #999;
font-size: 14px;
}
.info-section {
background: #f8f9fa;
border-radius: 12px;
padding: 20px;
margin-top: 30px;
}
.info-section h3 {
font-size: 18px;
color: #ef233c;
margin-bottom: 15px;
display: flex;
align-items: center;
gap: 8px;
}
.selected-count {
background: #ef233c;
color: white;
padding: 4px 10px;
border-radius: 12px;
font-size: 13px;
font-weight: 600;
}
.result-list {
list-style: none;
padding: 0;
}
.result-list li {
padding: 8px 0;
color: #666;
font-size: 14px;
border-bottom: 1px solid #e0e0e0;
}
.result-list li:last-child {
border-bottom: none;
}
.empty-state {
text-align: center;
color: #999;
font-size: 14px;
padding: 20px 0;
}
.btn-clear {
background: linear-gradient(135deg, #ef233c 0%, #d90429 100%);
color: white;
border: none;
border-radius: 8px;
padding: 10px 20px;
font-size: 14px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
display: flex;
align-items: center;
gap: 8px;
margin-top: 15px;
}
.btn-clear:hover {
transform: translateY(-2px);
box-shadow: 0 6px 15px rgba(239, 35, 60, 0.3);
}
.code-section {
background: #f8f9fa;
border-radius: 12px;
padding: 25px;
margin-top: 30px;
}
.code-section h3 {
font-size: 18px;
color: #ef233c;
margin-bottom: 15px;
display: flex;
align-items: center;
gap: 8px;
}
.code-block {
background: #2d2d2d;
color: #f8f8f2;
padding: 20px;
border-radius: 10px;
font-family: 'Courier New', monospace;
font-size: 13px;
overflow-x: auto;
line-height: 1.8;
}
.code-block .keyword {
color: #ff79c6;
}
.code-block .string {
color: #f1fa8c;
}
.code-block .comment {
color: #6272a4;
}
@media (max-width: 768px) {
.container {
padding: 25px;
}
h1 {
font-size: 24px;
}
.search-input {
min-width: 100px;
}
}
</style>
</head>
<body ng-controller="MultiSelectController">
<div class="container">
<h1>AngularJS Multi-Select Autocomplete</h1>
<p class="subtitle">Search and select multiple items with autocomplete</p>
<div class="multiselect-container">
<label class="multiselect-label">Select Programming Languages</label>
<div class="multiselect-input-wrapper" ng-click="focusInput()">
<div class="selected-items">
<span class="selected-item" ng-repeat="item in selectedItems">
{{item.name}}
<button class="selected-item-remove" ng-click="removeItem(item)">
<ion-icon name="close"></ion-icon>
</button>
</span>
</div>
<input
type="text"
class="search-input"
placeholder="{{selectedItems.length === 0 ? 'Search languages...' : 'Add more...'}}"
ng-model="searchQuery"
ng-focus="showDropdown = true"
ng-blur="hideDropdown()"
id="searchInput">
</div>
<div class="dropdown" ng-show="showDropdown && (filteredItems.length > 0 || searchQuery)">
<div class="dropdown-item"
ng-repeat="item in filteredItems = (availableItems | filter:searchQuery | limitTo:10)"
ng-class="{selected: isSelected(item)}"
ng-mousedown="toggleItem(item)">
<ion-icon name="{{isSelected(item) ? 'checkmark-circle' : 'add-circle-outline'}}"></ion-icon>
{{item.name}}
<span style="margin-left: auto; font-size: 12px; color: #999;">{{item.category}}</span>
</div>
<div class="no-results" ng-show="filteredItems.length === 0 && searchQuery">
No results found for "{{searchQuery}}"
</div>
</div>
</div>
<div class="info-section">
<h3>
<ion-icon name="list-outline"></ion-icon>
Selected Items
<span class="selected-count">{{selectedItems.length}}</span>
</h3>
<ul class="result-list" ng-show="selectedItems.length > 0">
<li ng-repeat="item in selectedItems">
<strong>{{item.name}}</strong> - {{item.category}}
</li>
</ul>
<div class="empty-state" ng-show="selectedItems.length === 0">
No items selected yet. Start typing to search and select items.
</div>
<button class="btn-clear" ng-click="clearAll()" ng-show="selectedItems.length > 0">
<ion-icon name="trash-outline"></ion-icon>
Clear All Selections
</button>
</div>
<div class="code-section">
<h3>
<ion-icon name="code-slash-outline"></ion-icon>
AngularJS Code
</h3>
<div class="code-block">
<span class="comment">// AngularJS Controller</span>
<span class="keyword">angular</span>.module(<span class="string">'multiSelectApp'</span>, [])
.controller(<span class="string">'MultiSelectController'</span>, <span class="keyword">function</span>($scope) {
$scope.selectedItems = [];
$scope.availableItems = [
{ id: <span class="string">1</span>, name: <span class="string">'JavaScript'</span>, category: <span class="string">'Web'</span> },
{ id: <span class="string">2</span>, name: <span class="string">'Python'</span>, category: <span class="string">'General'</span> },
<span class="comment">// ... more items</span>
];
$scope.toggleItem = <span class="keyword">function</span>(item) {
<span class="keyword">if</span> ($scope.isSelected(item)) {
$scope.removeItem(item);
} <span class="keyword">else</span> {
$scope.selectedItems.push(item);
}
};
});
</div>
</div>
</div>
<script>
angular.module('multiSelectApp', [])
.controller('MultiSelectController', function($scope, $timeout) {
$scope.selectedItems = [];
$scope.searchQuery = '';
$scope.showDropdown = false;
$scope.availableItems = [
{ id: 1, name: 'JavaScript', category: 'Web' },
{ id: 2, name: 'Python', category: 'General' },
{ id: 3, name: 'Java', category: 'Enterprise' },
{ id: 4, name: 'TypeScript', category: 'Web' },
{ id: 5, name: 'C++', category: 'System' },
{ id: 6, name: 'C#', category: 'Enterprise' },
{ id: 7, name: 'PHP', category: 'Web' },
{ id: 8, name: 'Ruby', category: 'Web' },
{ id: 9, name: 'Swift', category: 'Mobile' },
{ id: 10, name: 'Kotlin', category: 'Mobile' },
{ id: 11, name: 'Go', category: 'System' },
{ id: 12, name: 'Rust', category: 'System' },
{ id: 13, name: 'R', category: 'Data Science' },
{ id: 14, name: 'MATLAB', category: 'Scientific' },
{ id: 15, name: 'Scala', category: 'Functional' },
{ id: 16, name: 'Dart', category: 'Mobile' },
{ id: 17, name: 'Perl', category: 'Scripting' },
{ id: 18, name: 'Haskell', category: 'Functional' },
{ id: 19, name: 'Elixir', category: 'Functional' },
{ id: 20, name: 'Lua', category: 'Scripting' }
];
$scope.isSelected = function(item) {
return $scope.selectedItems.some(function(selected) {
return selected.id === item.id;
});
};
$scope.toggleItem = function(item) {
if ($scope.isSelected(item)) {
$scope.removeItem(item);
} else {
$scope.selectedItems.push(item);
$scope.searchQuery = '';
}
};
$scope.removeItem = function(item) {
var index = $scope.selectedItems.findIndex(function(selected) {
return selected.id === item.id;
});
if (index !== -1) {
$scope.selectedItems.splice(index, 1);
}
};
$scope.clearAll = function() {
$scope.selectedItems = [];
$scope.searchQuery = '';
};
$scope.hideDropdown = function() {
$timeout(function() {
$scope.showDropdown = false;
}, 200);
};
$scope.focusInput = function() {
document.getElementById('searchInput').focus();
};
});
</script>
</body>
</html>
No comments yet. Be the first!