120 lines
3.6 KiB
HTML
120 lines
3.6 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Paper-Like Website</title>
|
|
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/styles.css') }}">
|
|
</head>
|
|
|
|
<body>
|
|
|
|
|
|
<div class="container">
|
|
<h1>Conditions</h1>
|
|
<button onclick="openModal()">Add Item</button>
|
|
<table id="dataTable">
|
|
<thead>
|
|
<tr>
|
|
<th>ID</th>
|
|
<th>Name</th>
|
|
<th>Description</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<!-- Data will be inserted here dynamically -->
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
<div id="myModal" class="modal">
|
|
<div class="modal-content">
|
|
<span class="close" onclick="closeModal()">×</span>
|
|
<h2>Add Item</h2>
|
|
<form id="addItemForm" method="POST" action="/conditions">
|
|
<label for="itemName">Name:</label><br>
|
|
<input type="text" id="itemName" name="itemName" required><br>
|
|
<label for="itemDescription">Description:</label><br>
|
|
<textarea id="itemDescription" name="itemDescription" required></textarea><br><br>
|
|
<button type="submit">Submit</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<dialog id="editConditionDialog">
|
|
<form id="editItemForm" method="PUT" action="/conditions">
|
|
<label for="itemName">Name:</label><br>
|
|
<input type="text" id="itemName" name="itemName" required><br>
|
|
<label for="itemDescription">Description:</label><br>
|
|
<textarea id="itemDescription" name="itemDescription" required></textarea><br><br>
|
|
<button value="cancel" formmethod="dialog">Cancel</button>
|
|
<button type="submit">Submit</button>
|
|
</form>
|
|
</dialog>
|
|
|
|
<script>
|
|
// Sample data to populate the table
|
|
const data = `{{ data.conditions | tojson }}`;
|
|
const sampleData = JSON.parse(data)
|
|
|
|
|
|
// Function to populate the table with data
|
|
function populateTable(data) {
|
|
const tableBody = document.querySelector('#dataTable tbody');
|
|
tableBody.innerHTML = '';
|
|
data.forEach(item => {
|
|
const row = `<tr onclick="onRowClick('${item.id}')">
|
|
<td title="${item.id}">${item.id.substr(0,6)}</td>
|
|
<td>${item.itemName}</td>
|
|
<td>${item.itemDescription}</td>
|
|
</tr>`;
|
|
tableBody.insertAdjacentHTML('beforeend', row);
|
|
});
|
|
}
|
|
|
|
// Open modal function
|
|
function openModal() {
|
|
document.getElementById('myModal').style.display = 'block';
|
|
}
|
|
|
|
// Close modal function
|
|
function closeModal() {
|
|
document.getElementById('myModal').style.display = 'none';
|
|
}
|
|
|
|
function onRowClick(id) {
|
|
console.log("row id", id)
|
|
document.getElementById("editConditionDialog").showModal();
|
|
const form = document.getElementById("editItemForm");
|
|
form.action = form.action + "/" + id
|
|
document.getElementById("itemName").value = "a";
|
|
document.getElementById("itemDescription").value = "b";
|
|
|
|
|
|
}
|
|
|
|
// Handle form submission
|
|
document.getElementById('addItemForm').addEventListener('submit', function (event) {
|
|
event.preventDefault();
|
|
const form = event.target.closest('form');
|
|
const formData = new FormData(form);
|
|
const json = Array.from(formData.entries()).reduce((acc, [k, v]) => ({...acc, [k]: v}), {});
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
xhr.open('POST', form.action, true);
|
|
xhr.setRequestHeader('Content-Type', 'application/json');
|
|
xhr.onload = console.log
|
|
xhr.onerror = console.log
|
|
xhr.send(JSON.stringify(json));
|
|
|
|
closeModal();
|
|
});
|
|
|
|
// Initially populate the table with sample data
|
|
populateTable(sampleData);
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html> |