passesFrontend/static/scripts/tso-table.js

135 lines
3.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const createTable = () => {
const table = document.getElementById("passesTable");
const tbody = table.querySelector("tbody");
// Очищаем таблицу
tbody.innerHTML = "";
passes.forEach((pass) => {
const row = document.createElement("tr");
row.setAttribute("onclick", `openPass(${pass.id});`);
// Добавляем ячейки с данными
const id = document.createElement("td");
id.textContent = pass.id;
row.appendChild(id);
const status = document.createElement("td");
status.textContent = pass.status;
row.appendChild(status);
const date = document.createElement("td");
const rawDate = new Date(pass.date);
const formattedDate = `${rawDate.getDate().toString().padStart(2, "0")}/${(
rawDate.getMonth() + 1
)
.toString()
.padStart(2, "0")}/${rawDate.getFullYear()}`;
date.textContent = formattedDate;
row.appendChild(date);
const type = document.createElement("td");
type.innerHTML = pass.type;
row.appendChild(type);
const object = document.createElement("td");
object.innerHTML = pass.object;
row.appendChild(object);
const editdate = document.createElement("td");
if (pass.editdate) {
const rawFinalDate = new Date(pass.editdate);
const formattedfinalDate = `${rawFinalDate
.getDate()
.toString()
.padStart(2, "0")}/${(rawFinalDate.getMonth() + 1)
.toString()
.padStart(2, "0")}/${rawFinalDate.getFullYear()}`;
editdate.textContent = formattedfinalDate;
} else {
editdate.textContent = "";
}
row.appendChild(editdate);
const executor = document.createElement("td");
executor.innerHTML = pass.executor;
row.appendChild(executor);
tbody.appendChild(row);
});
};
document.addEventListener("DOMContentLoaded", function () {
createTable();
});
function getCookie(name) {
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i].trim();
if (cookie.startsWith(name + "=")) {
return cookie.substring(name.length + 1);
}
}
return null;
}
function requestUpdate() {
document.getElementById("passesTable").style.filter = "brightness(0.85)";
const formData = new FormData();
// formData.append("searchText", document.getElementById("table-search").value);
formData.append(
"page",
parseInt(document.getElementById("page-number").textContent)
);
const requestOptions = {
method: "POST",
headers: {
Authorization: getCookie("token"),
},
body: formData,
};
fetch(API_SERVER + "/passes/gettso", requestOptions)
.then((response) => {
document.getElementById("passesTable").style.filter = "brightness(1)";
if (response.ok) {
return response.json();
} else {
console.error("Ошибка при отправке POST запроса.");
return "Ошибка при отправке запроса.";
}
})
.then((data) => {
passes.splice(0, passes.length);
passes.push(
...data.passes.map((item) => ({
id: item.id,
status: item.Состояние,
object: item.Объект,
fabula: item.Фабула,
type: item.Видеисправности,
date: item.Дата_подачи,
editdate: item.Дата_изменения,
who: item.Кто_подал,
executor: item.Исполнитель,
events: item.Мероприятия,
}))
);
createTable();
totalMax = Math.ceil(data.totalCount / 15);
if (totalMax === 0) {
totalMax = 1;
}
var currentPage = parseInt(pageNumberInput.textContent, 10);
if (currentPage > totalMax) {
pageNumberInput.textContent = totalMax;
requestUpdate();
}
})
.catch((error) => {
console.error("Ошибка при отправке запроса:", error);
});
}