more table

This commit is contained in:
moerp 2024-01-30 22:24:48 +01:00
parent 1e8ae0105a
commit 8f15a05a4e
3 changed files with 109 additions and 0 deletions

View File

@ -140,6 +140,34 @@ def get_token():
return jsonify(token=oidc.get_access_token())
@app.route('/get_database_entries')
def get_database_entries():
try:
# Ersetzen Sie diese Werte mit Ihren Datenbank-Verbindungsinformationen
conn = psycopg2.connect()
cursor = conn.cursor()
with conn.cursor() as cursor:
cursor.execute("SELECT name, kcal, ew, fett, kh, bst, ca FROM nutrition_table")
entries = cursor.fetchall()
# Umwandeln der Daten in ein JSON-freundliches Format
entries_list = []
for entry in entries:
entries_list.append({
"food": entry[0],
"kcal": entry[1],
"ew": entry[2],
"fett": entry[3],
"kh": entry[4],
"bst": entry[5],
"ca": entry[6]
})
return jsonify(entries_list)
except Exception as e:
return jsonify({"error": str(e)}), 500
finally:
if conn:
conn.close()
app = ProxyFix(app, x_for=1, x_host=1)

View File

@ -152,3 +152,25 @@ tr:hover:not(.selected) {
overflow-x: auto; /* Ermöglicht horizontales Scrollen auf kleinen Bildschirmen */
}
}
#table-container {
overflow-y: auto;
max-height: 400px; /* Passen Sie die Höhe nach Bedarf an */
border-radius: 10px;
margin-top: 20px;
}
#database-nutrition-table {
width: 100%;
border-collapse: collapse;
}
#database-nutrition-table th, #database-nutrition-table td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
#database-nutrition-table tr.selected {
background-color: #f0e68c;
}

View File

@ -55,6 +55,44 @@
});
}
document.addEventListener('DOMContentLoaded', function() {
const table = document.getElementById('database-nutrition-table');
table.addEventListener('click', function(e) {
if (e.target.tagName === 'TD') {
e.target.parentNode.classList.toggle('selected');
}
});
});
function deleteSelectedRows() {
const table = document.getElementById('database-nutrition-table');
Array.from(table.rows).forEach(row => {
if (row.classList.contains('selected')) {
// Logik zum Löschen der Zeile aus der Datenbank
table.deleteRow(row.rowIndex);
}
});
}
function loadDatabaseEntries() {
fetch('/get_database_entries') // Pfad zur entsprechenden Flask-Route
.then(response => response.json())
.then(data => {
const tableBody = document.getElementById('database-nutrition-table').getElementsByTagName('tbody')[0];
data.forEach(entry => {
const row = tableBody.insertRow();
row.insertCell(0).innerHTML = entry.food;
row.insertCell(1).innerHTML = entry.kcal;
// ... Fügen Sie weitere Zellen für die anderen Werte hinzu ...
});
})
.catch(error => console.error('Fehler:', error));
}
document.addEventListener('DOMContentLoaded', loadDatabaseEntries);
</script>
</head>
<body>
@ -90,6 +128,27 @@
</table>
<button type="submit" id="submit-button" disabled>Hinzufügen</button>
</form>
<div id="table-container">
<table id="database-nutrition-table">
<thead>
<tr>
<th>Lebensmittel</th>
<th>kcal</th>
<th>EW</th>
<th>Fett</th>
<th>KH</th>
<th>BST</th>
<th>CA</th>
</tr>
</thead>
<tbody>
<!-- Die Zeilen werden dynamisch aus der Datenbank geladen -->
</tbody>
</table>
</div>
<button id="delete-row-button" onclick="deleteSelectedRows()">Ausgewählte Zeilen löschen</button>
</div>
</body>
</html>