mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
64 lines
1.6 KiB
JavaScript
64 lines
1.6 KiB
JavaScript
class ClassSelect2 {
|
|
|
|
constructor(domItem, url, placeholder = "", delay = 60, text_field = 'nombre', id_field = 'id') {
|
|
this.domItem = domItem;
|
|
this.url = url;
|
|
this.placeholder = placeholder;
|
|
this.delay = delay;
|
|
this.text_field = text_field;
|
|
this.id_field = id_field;
|
|
}
|
|
|
|
init() {
|
|
|
|
this.domItem.select2({
|
|
allowClear: false,
|
|
placeholder: this.placeholder,
|
|
ajax: {
|
|
url: this.url,
|
|
type: 'post',
|
|
dataType: 'json',
|
|
data: (params) => {
|
|
|
|
return {
|
|
id: this.id_field,
|
|
text: this.text_field,
|
|
searchTerm: params.term,
|
|
};
|
|
},
|
|
delay: this.delay,
|
|
processResults: function (response) {
|
|
|
|
return {
|
|
results: response.menu
|
|
};
|
|
},
|
|
cache: true
|
|
}
|
|
});
|
|
}
|
|
|
|
// Método para obtener el valor seleccionado
|
|
getValue() {
|
|
return this.domItem.val();
|
|
}
|
|
|
|
// Método para establecer el valor seleccionado
|
|
setValue(value) {
|
|
this.domItem.val(value).trigger('change');
|
|
}
|
|
|
|
// Método para oculatar el select2
|
|
hide() {
|
|
this.domItem.select2('close');
|
|
this.domItem.next('.select2-container').hide();
|
|
}
|
|
|
|
// Método para mostrar el select2
|
|
show() {
|
|
this.domItem.next('.select2-container').show();
|
|
}
|
|
}
|
|
|
|
export default ClassSelect2;
|