From 5187c6f78494e787a9d18cac706396d531090407 Mon Sep 17 00:00:00 2001 From: mirivlad Date: Thu, 2 Apr 2026 09:03:33 +0800 Subject: [PATCH] =?UTF-8?q?Fix:=20TomSelect=20=D0=B4=D0=BB=D1=8F=20=D0=BC?= =?UTF-8?q?=D1=83=D0=BB=D1=8C=D1=82=D0=B8=D0=B2=D1=8B=D0=B1=D0=BE=D1=80?= =?UTF-8?q?=D0=B0=20=D0=B2=20modal?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ✅ И
-
+
@@ -124,81 +124,84 @@ let items = []; let elementCounter = 0; // TomSelect экземпляры -let courseSelect, orgSelect, groupTags, userTags; +let courseSelect = null, orgSelect = null, groupTags = null, userTags = null; +let modalInitialized = false; -// Инициализация TomSelect при открытии modal +// Инициализация TomSelect при ПЕРВОМ открытии modal document.getElementById('addElementModal').addEventListener('show.bs.modal', function() { + if (modalInitialized) return; + // Курсы - if (!courseSelect) { - courseSelect = new TomSelect('#element_course_id', { - valueField: 'id', - labelField: 'text', - searchField: 'text', - placeholder: 'Выберите курс...', - preload: false, - load: function(query, callback) { - if (query.length < 2) return callback(); - fetch('/api/courses/search?q=' + encodeURIComponent(query)) - .then(response => response.json()) - .then(json => callback(json)) - .catch(() => callback()); - } - }); - } + courseSelect = new TomSelect('#element_course_id', { + valueField: 'id', + labelField: 'text', + searchField: 'text', + placeholder: 'Выберите курс...', + preload: false, + maxOptions: null, + load: function(query, callback) { + if (query.length < 2) return callback(); + fetch('/api/courses/search?q=' + encodeURIComponent(query)) + .then(response => response.json()) + .then(json => callback(json)) + .catch(() => callback()); + } + }); // Организации - if (!orgSelect) { - orgSelect = new TomSelect('#element_organization_id', { - valueField: 'id', - labelField: 'text', - searchField: 'text', - placeholder: 'Не выбрано', - preload: false, - load: function(query, callback) { - if (query.length < 2) return callback(); - fetch('/api/organizations/search?q=' + encodeURIComponent(query)) - .then(response => response.json()) - .then(json => callback(json)) - .catch(() => callback()); - } - }); - } + orgSelect = new TomSelect('#element_organization_id', { + valueField: 'id', + labelField: 'text', + searchField: 'text', + placeholder: 'Не выбрано', + preload: false, + maxOptions: null, + load: function(query, callback) { + if (query.length < 2) return callback(); + fetch('/api/organizations/search?q=' + encodeURIComponent(query)) + .then(response => response.json()) + .then(json => callback(json)) + .catch(() => callback()); + } + }); - // Группы - if (!groupTags) { - groupTags = new TomSelect('#element_group_ids', { - valueField: 'id', - labelField: 'text', - searchField: 'text', - placeholder: 'Выберите группы...', - preload: false, - load: function(query, callback) { - if (query.length < 2) return callback(); - fetch('/api/groups/search?q=' + encodeURIComponent(query)) - .then(response => response.json()) - .then(json => callback(json)) - .catch(() => callback()); - } - }); - } + // Группы (мультивыбор) + groupTags = new TomSelect('#element_group_ids', { + valueField: 'id', + labelField: 'text', + searchField: 'text', + placeholder: 'Выберите группы...', + preload: false, + maxOptions: null, + plugins: ['remove_button'], + load: function(query, callback) { + if (query.length < 2) return callback(); + fetch('/api/groups/search?q=' + encodeURIComponent(query)) + .then(response => response.json()) + .then(json => callback(json)) + .catch(() => callback()); + } + }); - // Пользователи - if (!userTags) { - userTags = new TomSelect('#element_user_ids', { - valueField: 'id', - labelField: 'text', - searchField: 'text', - placeholder: 'Выберите пользователей...', - preload: false, - load: function(query, callback) { - if (query.length < 2) return callback(); - fetch('/api/users/search?q=' + encodeURIComponent(query)) - .then(response => response.json()) - .then(json => callback(json)) - .catch(() => callback()); - } - }); - } + // Пользователи (мультивыбор) + userTags = new TomSelect('#element_user_ids', { + valueField: 'id', + labelField: 'text', + searchField: 'text', + placeholder: 'Выберите пользователей...', + preload: false, + maxOptions: null, + plugins: ['remove_button'], + load: function(query, callback) { + if (query.length < 2) return callback(); + fetch('/api/users/search?q=' + encodeURIComponent(query)) + .then(response => response.json()) + .then(json => callback(json)) + .catch(() => callback()); + } + }); + + modalInitialized = true; }); // Сброс формы при закрытии modal @@ -215,8 +218,8 @@ document.getElementById('addElementModal').addEventListener('hidden.bs.modal', f document.getElementById('addElementBtn').addEventListener('click', function() { const courseId = courseSelect.getValue(); const organizationId = orgSelect.getValue(); - const groupIds = groupTags.getValue(); - const userIds = userTags.getValue(); + const groupIds = groupTags.getValue() || []; + const userIds = userTags.getValue() || []; const startDate = document.getElementById('element_start_date').value; const endDate = document.getElementById('element_end_date').value; @@ -237,10 +240,10 @@ document.getElementById('addElementBtn').addEventListener('click', function() { course_name: courseSelect.options[courseId]?.text || 'Курс', organization_id: organizationId || null, organization_name: organizationId ? orgSelect.options[organizationId]?.text : null, - group_ids: Array.isArray(groupIds) ? groupIds : (groupIds ? [groupIds] : []), - group_names: Array.isArray(groupIds) ? groupIds.map(id => groupTags.options[id]?.text) : (groupIds ? [groupTags.options[groupIds]?.text] : []), - user_ids: Array.isArray(userIds) ? userIds : (userIds ? [userIds] : []), - user_names: Array.isArray(userIds) ? userIds.map(id => userTags.options[id]?.text) : (userIds ? [userTags.options[userIds]?.text] : []), + group_ids: Array.isArray(groupIds) ? groupIds : [groupIds], + group_names: (Array.isArray(groupIds) ? groupIds : [groupIds]).map(id => groupTags.options[id]?.text).filter(Boolean), + user_ids: Array.isArray(userIds) ? userIds : [userIds], + user_names: (Array.isArray(userIds) ? userIds : [userIds]).map(id => userTags.options[id]?.text).filter(Boolean), start_date: startDate, end_date: endDate || null }); diff --git a/resources/views/admin/course-requests/edit.blade.php b/resources/views/admin/course-requests/edit.blade.php index 39dd712..3221ea7 100644 --- a/resources/views/admin/course-requests/edit.blade.php +++ b/resources/views/admin/course-requests/edit.blade.php @@ -74,12 +74,12 @@
-
+ И
-
+
@@ -120,54 +120,56 @@ let items = @json($courseRequest->items->map(fn($item) => [ ]) ?? []); let elementCounter = items.length; -let courseSelect, orgSelect, groupTags, userTags; +let courseSelect = null, orgSelect = null, groupTags = null, userTags = null; +let modalInitialized = false; -// Инициализация TomSelect при открытии modal +// Инициализация TomSelect при ПЕРВОМ открытии modal document.getElementById('addElementModal').addEventListener('show.bs.modal', function() { - if (!courseSelect) { - courseSelect = new TomSelect('#element_course_id', { - valueField: 'id', labelField: 'text', searchField: 'text', - placeholder: 'Выберите курс...', preload: false, - load: function(query, callback) { - if (query.length < 2) return callback(); - fetch('/api/courses/search?q=' + encodeURIComponent(query)) - .then(response => response.json()).then(json => callback(json)).catch(() => callback()); - } - }); - } - if (!orgSelect) { - orgSelect = new TomSelect('#element_organization_id', { - valueField: 'id', labelField: 'text', searchField: 'text', - placeholder: 'Не выбрано', preload: false, - load: function(query, callback) { - if (query.length < 2) return callback(); - fetch('/api/organizations/search?q=' + encodeURIComponent(query)) - .then(response => response.json()).then(json => callback(json)).catch(() => callback()); - } - }); - } - if (!groupTags) { - groupTags = new TomSelect('#element_group_ids', { - valueField: 'id', labelField: 'text', searchField: 'text', - placeholder: 'Выберите группы...', preload: false, - load: function(query, callback) { - if (query.length < 2) return callback(); - fetch('/api/groups/search?q=' + encodeURIComponent(query)) - .then(response => response.json()).then(json => callback(json)).catch(() => callback()); - } - }); - } - if (!userTags) { - userTags = new TomSelect('#element_user_ids', { - valueField: 'id', labelField: 'text', searchField: 'text', - placeholder: 'Выберите пользователей...', preload: false, - load: function(query, callback) { - if (query.length < 2) return callback(); - fetch('/api/users/search?q=' + encodeURIComponent(query)) - .then(response => response.json()).then(json => callback(json)).catch(() => callback()); - } - }); - } + if (modalInitialized) return; + + courseSelect = new TomSelect('#element_course_id', { + valueField: 'id', labelField: 'text', searchField: 'text', + placeholder: 'Выберите курс...', preload: false, maxOptions: null, + load: function(query, callback) { + if (query.length < 2) return callback(); + fetch('/api/courses/search?q=' + encodeURIComponent(query)) + .then(response => response.json()).then(json => callback(json)).catch(() => callback()); + } + }); + + orgSelect = new TomSelect('#element_organization_id', { + valueField: 'id', labelField: 'text', searchField: 'text', + placeholder: 'Не выбрано', preload: false, maxOptions: null, + load: function(query, callback) { + if (query.length < 2) return callback(); + fetch('/api/organizations/search?q=' + encodeURIComponent(query)) + .then(response => response.json()).then(json => callback(json)).catch(() => callback()); + } + }); + + groupTags = new TomSelect('#element_group_ids', { + valueField: 'id', labelField: 'text', searchField: 'text', + placeholder: 'Выберите группы...', preload: false, maxOptions: null, + plugins: ['remove_button'], + load: function(query, callback) { + if (query.length < 2) return callback(); + fetch('/api/groups/search?q=' + encodeURIComponent(query)) + .then(response => response.json()).then(json => callback(json)).catch(() => callback()); + } + }); + + userTags = new TomSelect('#element_user_ids', { + valueField: 'id', labelField: 'text', searchField: 'text', + placeholder: 'Выберите пользователей...', preload: false, maxOptions: null, + plugins: ['remove_button'], + load: function(query, callback) { + if (query.length < 2) return callback(); + fetch('/api/users/search?q=' + encodeURIComponent(query)) + .then(response => response.json()).then(json => callback(json)).catch(() => callback()); + } + }); + + modalInitialized = true; }); document.getElementById('addElementModal').addEventListener('hidden.bs.modal', function() { @@ -182,8 +184,8 @@ document.getElementById('addElementModal').addEventListener('hidden.bs.modal', f document.getElementById('addElementBtn').addEventListener('click', function() { const courseId = courseSelect.getValue(); const organizationId = orgSelect.getValue(); - const groupIds = groupTags.getValue(); - const userIds = userTags.getValue(); + const groupIds = groupTags.getValue() || []; + const userIds = userTags.getValue() || []; const startDate = document.getElementById('element_start_date').value; const endDate = document.getElementById('element_end_date').value; @@ -196,10 +198,10 @@ document.getElementById('addElementBtn').addEventListener('click', function() { course_name: courseSelect.options[courseId]?.text || 'Курс', organization_id: organizationId || null, organization_name: organizationId ? orgSelect.options[organizationId]?.text : null, - group_ids: Array.isArray(groupIds) ? groupIds : (groupIds ? [groupIds] : []), - group_names: Array.isArray(groupIds) ? groupIds.map(id => groupTags.options[id]?.text) : (groupIds ? [groupTags.options[groupIds]?.text] : []), - user_ids: Array.isArray(userIds) ? userIds : (userIds ? [userIds] : []), - user_names: Array.isArray(userIds) ? userIds.map(id => userTags.options[id]?.text) : (userIds ? [userTags.options[userIds]?.text] : []), + group_ids: Array.isArray(groupIds) ? groupIds : [groupIds], + group_names: (Array.isArray(groupIds) ? groupIds : [groupIds]).map(id => groupTags.options[id]?.text).filter(Boolean), + user_ids: Array.isArray(userIds) ? userIds : [userIds], + user_names: (Array.isArray(userIds) ? userIds : [userIds]).map(id => userTags.options[id]?.text).filter(Boolean), start_date: startDate, end_date: endDate || null });