-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselect-relations.js
121 lines (92 loc) · 4.45 KB
/
select-relations.js
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
/*
--------------------------------------------------
@ Select Relations JS @
Version: 1.1.2
Author: Kamran Gasimov
Created: 09.04.2024
Updated: 23.04.2024
© All rights are reserved Deirvlon Technologies.
--------------------------------------------------
*/
function SelectRelations() {
document.addEventListener('DOMContentLoaded', function () {
const selectRelations = document.querySelectorAll('.select-relations');
selectRelations.forEach(select => {
// Check if Select2 is initialized
const isSelect2Initialized = $(select).hasClass('select2') !== undefined;
// Add event listener for change event
select.addEventListener('change', function () {
updateFiltering(this);
});
// If Select2 is initialized, also listen to its change event
if (isSelect2Initialized) {
$(select).on('change.select2', function () {
updateFiltering(this);
});
}
});
initializeFiltering();
});
function initializeFiltering() {
const selectRelations = document.querySelectorAll('.select-relations');
selectRelations.forEach(select => {
updateFiltering(select, false);
});
// Reset parent selects' selected options if they are hidden due to filtering
resetParentSelects();
}
function updateFiltering(select, restart = true) {
const selectedOption = select.options[select.selectedIndex];
const parentId = select.getAttribute('id');
const parentValues = parentId.split(',').map(parentId => document.getElementById(parentId).value).join(',');
document.querySelectorAll(`[data-sf-parent*="${parentId}"]`).forEach(childSelect => {
if (childSelect.tagName == "SELECT")
// SELECT INPUT
childSelect.querySelectorAll('option').forEach(option => {
const relationData = option.getAttribute('data-pr');
if (relationData) {
const displayOption = relationData.split('&').some(pair => {
const [selectId, selectValues] = pair.split(':');
var el_parent = document.getElementById(selectId);
var ids = selectValues.split(',');
return ids.includes(el_parent.value) || (el_parent.options[el_parent.selectedIndex]!=undefined && ids.includes(el_parent.options[el_parent.selectedIndex].dataset.alt));
});
option.disabled = displayOption ? false : true;
option.hidden = displayOption ? false : true;
option.ariaHidden = displayOption ? false : true;
}
});
else {
//NORMAL INPUTS
const relationData = childSelect.getAttribute('data-pr');
if (relationData) {
const displayOption = relationData.split('&').some(pair => {
const [selectId, selectValues] = pair.split(':');
var el_parent = document.getElementById(selectId);
var ids = selectValues.split(',');
return ids.includes(el_parent.value) || (el_parent.options[el_parent.selectedIndex]!=undefined && ids.includes(el_parent.options[el_parent.selectedIndex].dataset.alt));
});
childSelect.style.display = (displayOption) ? '' : 'none';
}
}
});
// Reset parent selects' selected options if they are hidden due to filtering
if (restart)
resetParentSelects();
}
function resetParentSelects() {
document.querySelectorAll(`[data-sf-parent]`).forEach(childSelect => {
if (childSelect.tagName == "SELECT") {
// SELECT INPUT
if (childSelect.selectedIndex == null || childSelect.selectedIndex == -1)
return;
let selectOption = childSelect.options[childSelect.selectedIndex];
if (selectOption.disabled) {
$(childSelect).val(null).trigger('change.select2');
}
}
});
}
}
// autorun
SelectRelations(); //Init