-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTMLPeoplePickerMultiSelect.html
121 lines (115 loc) · 5.41 KB
/
HTMLPeoplePickerMultiSelect.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>People Picker</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
.autocomplete-container {position: relative; display: flex; align-items: center; border: 1px solid #ced4da; border-radius: 4px; padding: 5px;}
.autocomplete-results {display: none; position: absolute; background-color: #fff; border: 1px solid #ccc; border-radius: 4px; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); width: 100%; max-height: 200px; overflow-y: auto; z-index: 1000; top: calc(100% + 5px); left: 0;}
.autocomplete-results .result-item {padding: 8px 12px; cursor: pointer; display: flex; align-items: center;}
.autocomplete-results .result-item .user-avatar, .selected-initials {width: 32px; height: 32px; border-radius: 50%; background-color: #0078d4; color: #fff; display: flex; justify-content: center; align-items: center; font-size: 14px;}
.autocomplete-results .result-item .user-info {flex: 1;}
.autocomplete-results .result-item .user-name {font-weight: bold;}
.autocomplete-results .result-item .user-email {color: #666;}
.autocomplete-results .result-item:hover {background-color: #f0f0f0;}
.selected-user-container {display: flex; align-items: center; flex-wrap: wrap;}
.selected-user {display: inline-flex; align-items: center; background-color: #f0f0f0; border-radius: 4px; padding: 5px 10px; margin-right: 5px; margin-bottom: 5px;}
.selected-user .delete-icon {margin-left: 5px; cursor: pointer;}
.selected-user .delete-icon:hover {color: red;}
.input-search {border: none; outline: none; flex-grow: 1;}
</style>
</head>
<body>
<div class="container mt-5">
<h1>People Picker</h1>
<div class="autocomplete-container">
<div id="selectedUsers" class="selected-user-container"></div>
<input id="inputSearch" class="input-search" placeholder="Enter at least 3 characters">
<div id="autocompleteResults" class="autocomplete-results"></div>
</div>
<div class="container mt-3">
<button id="getSelectedValueBtn" class="btn btn-primary">Get Selected Value</button>
<button id="toggleControlBtn" class="btn btn-secondary ms-2">Toggle Control</button>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$(function () {
const selectedUsersContainer = $('#selectedUsers');
const inputSearch = $('#inputSearch');
const autocompleteResults = $('#autocompleteResults');
inputSearch.on('input', function () {
const searchText = $(this).val().trim();
if (searchText.length >= 3) {
$.ajax({
url: `https://dummyjson.com/users/search?q=${searchText}`,
method: 'GET',
success: function (data) {
displayResults(data.users);
},
error: function (xhr, status, error) {
console.error(status, error);
}
});
} else {
autocompleteResults.empty().hide();
}
});
function displayResults(results) {
autocompleteResults.empty();
if (results.length > 0) {
results.forEach(result => {
const item = $('<div class="result-item"></div>');
const userAvatar = $('<div class="user-avatar"></div>').text(getInitials(result.firstName));
const userInfo = $('<div class="user-info"></div>');
const userName = $('<div class="user-name"></div>').text(result.firstName);
const userEmail = $('<div class="user-email"></div>').text(result.email);
userInfo.append(userName, userEmail);
item.append(userAvatar, userInfo).on('click', () => {
selectUser(result.firstName);
inputSearch.val('').focus();
});
autocompleteResults.append(item);
});
autocompleteResults.show();
} else {
autocompleteResults.append('<div class="result-item">No results found</div>').show();
}
}
function selectUser(name) {
if (!selectedUsersContainer.find('.selected-info:contains("' + name + '")').length) {
const initials = getInitials(name);
const selectedUser = $('<div class="selected-user"></div>');
const selectedInitials = $('<div class="selected-initials"></div>').text(initials);
const selectedInfo = $('<div class="selected-info"></div>').text(name);
selectedUser.append(selectedInitials, selectedInfo);
const deleteIcon = $('<span class="delete-icon">×</span>').on('click', () => {
selectedUser.remove();
});
selectedUser.append(deleteIcon);
selectedUsersContainer.append(selectedUser);
autocompleteResults.hide();
} else {
alert('User already selected');
}
}
$('#getSelectedValueBtn').on('click', () => {
const selectedValues = [];
selectedUsersContainer.find('.selected-info').each(function () {
selectedValues.push($(this).text());
});
alert('Selected Values: ' + selectedValues.join(', '));
});
$('#toggleControlBtn').on('click', () => {
inputSearch.prop('disabled', (i, val) => !val);
$(this).text(inputSearch.prop('disabled') ? 'Enable Control' : 'Disable Control');
});
function getInitials(name) {
return name.split(' ').map(part => part[0]).join('').toUpperCase();
}
});
</script>
</body>
</html>