fix(frontend): remove duplicate options from datalist and select

This commit is contained in:
Mariano Riefolo 2024-09-04 14:01:18 +02:00
parent 2df5352c1c
commit 73091f29ed

View File

@ -20,11 +20,12 @@ function load_teachers(id) {
.then((data) => { .then((data) => {
sessionStorage.setItem("teachers", JSON.stringify(data)); sessionStorage.setItem("teachers", JSON.stringify(data));
let i = 0; let i = 0;
const list = document.getElementById("teachers");
list.innerHTML = "";
data.forEach((el) => { data.forEach((el) => {
const select = document.getElementById("teachers");
const option = document.createElement("option"); const option = document.createElement("option");
option.value = el; option.value = el;
select.appendChild(option); list.appendChild(option);
i++; i++;
}); });
}); });
@ -34,8 +35,14 @@ function load_classes(id) {
fetch(`/api/classi?id=${id}`) fetch(`/api/classi?id=${id}`)
.then((response) => response.json()) .then((response) => response.json())
.then((data) => { .then((data) => {
const select = document.getElementById("year");
for (let i = 0; i < select.children.length; i++) {
if (!select.children[i].hasAttribute("disabled")) {
select.removeChild(select.children[i]);
i--;
}
}
for (let i = 0; i < data.length; i++) { for (let i = 0; i < data.length; i++) {
const select = document.getElementById("year");
const option = document.createElement("option"); const option = document.createElement("option");
option.value = i; option.value = i;
option.text = i + 1; option.text = i + 1;
@ -129,8 +136,14 @@ function select_type() {
function load_section() { function load_section() {
let data = JSON.parse(sessionStorage.getItem("classes")); let data = JSON.parse(sessionStorage.getItem("classes"));
let year = document.getElementById("year").value; let year = document.getElementById("year").value;
const select = document.getElementById("section");
for (let i = 0; i < select.children.length; i++) {
if (!select.children[i].hasAttribute("disabled")) {
select.removeChild(select.children[i]);
i--;
}
}
Object.keys(data[year]).forEach((el) => { Object.keys(data[year]).forEach((el) => {
const select = document.getElementById("section");
const option = document.createElement("option"); const option = document.createElement("option");
option.value = el; option.value = el;
option.text = el; option.text = el;
@ -142,8 +155,14 @@ function load_major() {
let data = JSON.parse(sessionStorage.getItem("classes")); let data = JSON.parse(sessionStorage.getItem("classes"));
let year = document.getElementById("year").value; let year = document.getElementById("year").value;
let section = document.getElementById("section").value; let section = document.getElementById("section").value;
let select = document.getElementById("major");
for (let i = 0; i < select.children.length; i++) {
if (!select.children[i].hasAttribute("disabled")) {
select.removeChild(select.children[i]);
i--;
}
}
data[year][section].forEach((el) => { data[year][section].forEach((el) => {
const select = document.getElementById("major");
const option = document.createElement("option"); const option = document.createElement("option");
option.value = el; option.value = el;
option.text = el; option.text = el;