Skip to content

Commit f2b3827

Browse files
committed
feat(ui): enable Select2 AJAX loading via data-endpoint or ajaxify attributes
Enhances Select2 initialization to support AJAX-loaded options using existing CSK attributes (`data-endpoint` or `ajaxify`). This allows large datasets to be queried dynamically without preloading options, improving performance and enabling seamless integration with admin search endpoints. Also bumped version from `1.6.1` to `1.6.2`.
1 parent 9b7b249 commit f2b3827

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

js/admin.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*!
22
* Skeleton Back-End - Admin JS (https://www.ianhub.net/)
3-
* Copyright 2025 Kader Bouyakoub (https://github.com/bkader)
3+
* Copyright 2026 Kader Bouyakoub (https://github.com/bkader)
44
*/
55
(function($, window, document, undefined) {
66
"use strict";
@@ -637,6 +637,7 @@
637637
}
638638
}
639639
};
640+
640641
/**
641642
* HTTP method shortcuts
642643
* @since 3.11.1
@@ -888,7 +889,8 @@
888889
$("select.select2").each(function () {
889890
var $that = $(this),
890891
config = { width: "100%" },
891-
ph = $that.data("placeholder") || $that.attr("placeholder");
892+
ph = $that.data("placeholder") || $that.attr("placeholder"),
893+
href = $that.data("endpoint") || $that.attr("ajaxify");
892894

893895
if (ph?.length) {
894896
config.placeholder = ph;
@@ -903,6 +905,28 @@
903905
config.dropdownParent = $(".modal");
904906
}
905907

908+
if (href?.length) {
909+
config.minimumInputLength = 1;
910+
config.ajax = {
911+
delay: 250,
912+
transport: function (params, success, failure) {
913+
csk.ajax.request(href, {
914+
data: {
915+
q: params.data.term || "",
916+
page: params.data.page || 1
917+
},
918+
success: function (data) {
919+
success(data);
920+
},
921+
error: failure
922+
});
923+
},
924+
processResults: function (data) {
925+
return data;
926+
}
927+
};
928+
}
929+
906930
$that.select2(config);
907931
});
908932
}

0 commit comments

Comments
 (0)