Files
safekat/httpdocs/assets/js/safekat/components/select2.js
2024-10-14 13:58:50 +02:00

73 lines
1.8 KiB
JavaScript

/**
*
* @param {DOM} domItem
* @param {String} url
* @param {String} placeholder
*/
let ClassSelect = function (domItem, url, placeholder, allowClear = false) {
this.url = url;
this.item = domItem;
this.config = {
placeholder: placeholder,
allowClear: allowClear,
dropdownParent: domItem.parent(),
ajax: {
url: () => {
return this.url;
},
data: function (params) {
return {
q: $.trim(params.term),
};
},
processResults: function (data) {
return {
results: $.map(data, function (obj) {
return {
id: obj.id,
text: obj.nombre ?? obj.name,
desc: obj.description,
};
}),
};
},
cache: true,
},
};
this.init = function () {
if (this.item.length) {
this.item = this.item.select2(this.config);
$.fn.modal.Constructor.prototype.enforceFocus = function () {};
}
};
this.setOption = function (id, nombre) {
var newOption = new Option(nombre , id, false, false);
this.item.append(newOption);
this.item.val(id).trigger("change");
};
this.reset = function () {
this.item.val(null).trigger("change");
};
this.getVal = function () {
return this.item.val();
};
this.setVal = function (val) {
return this.item.val(val).trigger("change");
};
this.empty = function () {
return this.item.empty().trigger("change");
};
this.readOnly = function () {
this.item.enable(false);
};
this.enable = () => {
this.item.enable(true);
};
this.fixWithScroll = function () {};
this.getText = () => {
return this.item.find(":selected").text();
};
};
export default ClassSelect;