@@ -74,6 +74,14 @@ <h2 class="mb-6 text-2xl font-semibold text-gray-800 dark:text-white">
74
74
</ select >
75
75
</ div >
76
76
77
+ <!-- Single Ajax Select -->
78
+ < div >
79
+ < label class ="block mb-2 text-sm font-medium text-gray-700 dark:text-white "> Example of single ajax select</ label >
80
+ < select data-placeholder ="Search for github repos " data-allow-clear ="1 " data-fetch ="https://api.github.com/search/repositories ">
81
+ < option > </ option >
82
+ </ select >
83
+ </ div >
84
+
77
85
<!-- Tags Select -->
78
86
< div >
79
87
< label class ="block mb-2 text-sm font-medium text-gray-700 dark:text-white "> Example of dynamic option creation</ label >
@@ -100,6 +108,17 @@ <h2 class="mb-6 text-2xl font-semibold text-gray-800 dark:text-white">
100
108
< select disabled data-placeholder ="Cannot choose " data-allow-clear ="1 "> </ select >
101
109
</ div >
102
110
111
+ <!-- Disabled option -->
112
+ < div >
113
+ < label class ="block mb-2 text-sm font-medium text-gray-700 dark:text-white "> Example of disabled option</ label >
114
+ < select data-placeholder ="Cannot anything ">
115
+ < option > </ option >
116
+ < option value ="one "> First</ option >
117
+ < option value ="two " disabled ="disabled "> Second (disabled)</ option >
118
+ < option value ="three "> Third</ option >
119
+ </ select >
120
+ </ div >
121
+
103
122
<!-- Multiple Disabled Select -->
104
123
< div >
105
124
< label class ="block mb-2 text-sm font-medium text-gray-700 dark:text-white "> Example of disabled multiple select with selected items</ label >
@@ -194,15 +213,72 @@ <h2 class="mb-6 text-2xl font-semibold text-gray-800 dark:text-white">
194
213
195
214
// Select2 init
196
215
$ ( 'select' ) . each ( function ( ) {
197
- $ ( this ) . select2 ( {
216
+ let options = {
198
217
theme : 'tailwindcss-3' ,
199
218
width : $ ( this ) . data ( 'width' ) ? $ ( this ) . data ( 'width' ) : $ ( this ) . hasClass ( 'w-100' ) ? '100%' : 'style' ,
200
219
placeholder : $ ( this ) . data ( 'placeholder' ) || 'Select an option' ,
201
220
allowClear : Boolean ( $ ( this ) . data ( 'allow-clear' ) ) ,
202
221
closeOnSelect : ! $ ( this ) . attr ( 'multiple' ) ,
203
222
tags : Boolean ( $ ( this ) . data ( 'tags' ) ) ,
204
- } ) ;
223
+ } ;
224
+
225
+ if ( $ ( this ) . data ( 'fetch' ) ) {
226
+ options = {
227
+ ...options ,
228
+ ajax : {
229
+ url : $ ( this ) . data ( 'fetch' ) ,
230
+ dataType : 'json' ,
231
+ delay : 250 ,
232
+ data : function ( params ) {
233
+ return {
234
+ q : params . term , // search term
235
+ page : params . page
236
+ } ;
237
+ } ,
238
+ processResults : function ( data , params ) {
239
+ // parse the results into the format expected by Select2
240
+ // since we are using custom formatting functions we do not need to
241
+ // alter the remote JSON data, except to indicate that infinite
242
+ // scrolling can be used
243
+ params . page = params . page || 1 ;
244
+
245
+ return {
246
+ results : data . items ,
247
+ pagination : {
248
+ more : ( params . page * 30 ) < data . total_count
249
+ }
250
+ } ;
251
+ } ,
252
+ cache : true
253
+ } ,
254
+ minimumInputLength : 1 ,
255
+ templateResult : formatRepo ,
256
+ templateSelection : formatRepoSelection
257
+ } ;
258
+ }
259
+
260
+ $ ( this ) . select2 ( options ) ;
205
261
} ) ;
262
+
263
+ function formatRepo ( repo ) {
264
+ if ( repo . loading ) {
265
+ return repo . text ;
266
+ }
267
+
268
+ var $container = $ (
269
+ "<div class='flex'>" +
270
+ "<div class='text-base sm:text-sm/6 dark:text-white select2-result-repository__title'></div>" +
271
+ "</div>"
272
+ ) ;
273
+
274
+ $container . find ( ".select2-result-repository__title" ) . text ( repo . full_name ) ;
275
+
276
+ return $container ;
277
+ }
278
+
279
+ function formatRepoSelection ( repo ) {
280
+ return repo . full_name || repo . text ;
281
+ }
206
282
} ) ;
207
283
</ script >
208
284
0 commit comments