-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexample.html
More file actions
166 lines (148 loc) · 6.79 KB
/
Copy pathexample.html
File metadata and controls
166 lines (148 loc) · 6.79 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>TsSelect2 Test</title>
<link href="./example.min.css" rel="stylesheet"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.2.2/css/bootstrap.min.css"
integrity="sha512-CpIKUSyh9QX2+zSdfGP+eWLx23C8Dj9/XmHjZY2uDtfkdLGo0uY12jgcnkX9vXOgYajEKb/jiw67EYm+kBf+6g=="
crossorigin="anonymous" referrerpolicy="no-referrer"/>
<script src="https://unpkg.com/ts-select2@0.2.5/dist/ts-select2.min.js"></script>
<!-- <script src="./dist/ts-select2.min.js"></script>-->
</head>
<body>
<div class="page-content">
<div class="content-wrapper">
<div class="content-inner">
<div class="card-body">
<div class="card">
<div class="content">
<div class="mb-4">
<!-- Basic -->
<div class="mb-3 row">
<label class="col-form-label col-lg-3">Basic select</label>
<div class="col-lg-9">
<select class="form-control select-simple"
data-minimum-results-for-search="Infinity">
</select>
</div>
</div>
<!-- Multiple -->
<div class="mb-3 row">
<label class="col-form-label col-lg-3">Multiple select</label>
<div class="col-lg-9">
<select data-placeholder="Select something"
multiple="multiple"
class="form-control select-multiple">
<option value="AK">Alaska</option>
<option value="CA">California</option>
<option value="AZ" selected>Arizona</option>
<option value="CO">Colorado</option>
<option value="ID">Idaho</option>
<option value="WY" selected>Wyoming</option>
<option value="CT">Connecticut</option>
</select>
</div>
</div>
<!-- Remote -->
<div class="mb-3 row">
<label class="col-form-label col-lg-3">Loading remote data</label>
<div class="col-lg-9">
<select class="form-control select-remote-data">
<option value="318991746" selected>ivkan/ts-select2</option>
</select>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
<script>
document.addEventListener("DOMContentLoaded", function () {
// Data
const array_data = [
{id: 0, text: 'enhancement'},
{id: 1, text: 'bug'},
{id: 2, text: 'duplicate'},
{id: 3, text: 'invalid'},
{id: 4, text: 'wontfix'}
];
// Basic select
new TsSelect2(document.querySelector('select.select-simple'), {
// minimumResultsForSearch: Infinity,
// width: `250px`
placeholder: 'Click to load data',
minimumResultsForSearch: Infinity,
data: array_data
});
// Multiple select
new TsSelect2(document.querySelector('select.select-multiple'), {
minimumResultsForSearch: Infinity,
width: `250px`
});
//
// Loading remote data
//
// Format displayed data
function formatRepo(repo) {
if (repo.loading) return repo.text;
let markup = '<div class="select2-result-repository clearfix">' +
'<div class="select2-result-repository__avatar"><img src="' + repo.owner.avatar_url + '" /></div>' +
'<div class="select2-result-repository__meta">' +
'<div class="select2-result-repository__title">' + repo.full_name + '</div>';
if (repo.description) {
markup += '<div class="select2-result-repository__description">' + repo.description + '</div>';
}
markup += '<div class="select2-result-repository__statistics">' +
'<div class="select2-result-repository__forks">' + repo.forks_count + ' Forks</div>' +
'<div class="select2-result-repository__stargazers">' + repo.stargazers_count + ' Stars</div>' +
'<div class="select2-result-repository__watchers">' + repo.watchers_count + ' Watchers</div>' +
'</div>' +
'</div></div>';
return markup;
}
// Format selection
function formatRepoSelection(repo) {
return repo.full_name || repo.text;
}
// Remote data select
new TsSelect2(document.querySelector('select.select-remote-data'), {
ajax: {
url: 'https://api.github.com/search/repositories',
dataType: 'json',
delay: 250,
data: function (params) {
return {
q: params.term, // search term
page: params.page
};
},
processResults: function (data, params) {
// parse the results into the format expected by Select2
// since we are using custom formatting functions we do not need to
// alter the remote JSON data, except to indicate that infinite
// scrolling can be used
params.page = params.page || 1;
return {
results: data.items,
pagination: {
more: (params.page * 30) < data.total_count
}
};
},
cache: true
},
escapeMarkup: function (markup) {
return markup;
}, // let our custom formatter work
minimumInputLength: 1,
templateResult: formatRepo, // omitted for brevity, see the source of this page
templateSelection: formatRepoSelection // omitted for brevity, see the source of this page
});
});
</script>
</html>