HTML-Seiten modularer aufgebaut und table.html hinzugefügt

This commit is contained in:
Jason Streifling 2023-10-07 18:32:33 +02:00
parent 8ae3019b9c
commit 15675d5e6c
6 changed files with 115 additions and 72 deletions

View File

@ -27,8 +27,14 @@ func main() {
log.Fatalln(err) log.Fatalln(err)
} }
bs, err := db.ReadAll()
if err != nil {
log.Fatalln(err)
}
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/")))) mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
mux.HandleFunc("/", server.DisplayForm(&i)) // mux.HandleFunc("/", server.DisplayForm(&i))
mux.HandleFunc("/", server.DisplayTable(bs))
mux.HandleFunc("/add-participant/", server.AddParticipant(&i)) mux.HandleFunc("/add-participant/", server.AddParticipant(&i))
mux.HandleFunc("/submit/", server.SubmitForm(cb, &i, &j)) mux.HandleFunc("/submit/", server.SubmitForm(cb, &i, &j))

View File

@ -8,16 +8,22 @@ import (
"streifling.com/jason/sicherheitsunterweisung/packages/types" "streifling.com/jason/sicherheitsunterweisung/packages/types"
) )
func DisplayTable(bs *[]types.Briefing) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
template.Must(template.ParseFiles("templates/index.html", "templates/table.html")).Execute(w, bs)
}
}
func DisplayForm(i *int64) http.HandlerFunc { func DisplayForm(i *int64) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
template.Must(template.ParseFiles("templates/form.html")).Execute(w, i) template.Must(template.ParseFiles("templates/index.html", "templates/form.html")).Execute(w, i)
} }
} }
func AddParticipant(i *int64) http.HandlerFunc { func AddParticipant(i *int64) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
*i++ *i++
template.Must(template.ParseFiles("templates/form.html", "templates/participant.html")).ExecuteTemplate(w, "participant", i) template.Must(template.ParseFiles("templates/form.html")).ExecuteTemplate(w, "participant", i)
} }
} }

View File

@ -1,16 +1,17 @@
<!DOCTYPE html> {{ define "participant" }}
<html lang="de"> <div id="participant-{{ . }}">
<label for="participant-first-input-{{ . }}">Vorname</label>
<input type="text" name="participant-first-{{ . }}" id="participant-first-input-{{ . }}" />
<head> <label for="participant-last-input-{{ . }}">Nachname</label>
<meta charset="UTF-8"> <input type="text" name="participant-last-{{ . }}" id="participant-last-input-{{ . }}" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/static/css/style.css">
<title>Sicherheitsunterweisung</title>
</head>
<body> <label for="participant-company-input-{{ . }}">Firma</label>
<h1>Sicherheitsunterweisung</h1> <input type="text" name="participant-company-{{ . }}" id="participant-company-input-{{ . }}" />
</div>
{{ end }}
{{ define "content" }}
<form> <form>
<div id="instructor"> <div id="instructor">
<label for="instructor-first-input">Unterweiser Vorname</label> <label for="instructor-first-input">Unterweiser Vorname</label>
@ -41,29 +42,13 @@
</div> </div>
<div id="participants"> <div id="participants">
<button type="button" hx-post="/add-participant/" hx-target="#participants" hx-swap="beforeend" <button type="button" hx-post="/add-participant/" hx-target="#participants" hx-swap="beforeend" hx-trigger="click">
hx-trigger="click">
+ +
</button> </button>
<div id="participant-{{ . }}"> {{ template "participant" . }}
<label for="participant-first-input-{{ . }}">Vorname</label>
<input type="text" name="participant-first-{{ . }}" id="participant-first-input-{{ . }}" />
<label for="participant-last-input-{{ . }}">Nachname</label>
<input type="text" name="participant-last-{{ . }}" id="participant-last-input-{{ . }}" />
<label for="participant-company-input-{{ . }}">Firma</label>
<input type="text" name="participant-company-{{ . }}" id="participant-company-input-{{ . }}" />
</div>
</div> </div>
<button type="submit" hx-post="/submit/"> <button type="submit" hx-post="/submit/">Senden</button>
Senden
</button>
</form> </form>
{{ end }}
<script src="/static/js/htmx.min.js" type="text/javascript"></script>
</body>
</html>

19
templates/index.html Normal file
View File

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/static/css/style.css">
<title>Sicherheitsunterweisung</title>
</head>
<body>
<h1>Sicherheitsunterweisung</h1>
{{ template "content" . }}
<script src="/static/js/htmx.min.js" type="text/javascript"></script>
</body>
</html>

View File

@ -1,12 +0,0 @@
{{ define "participant" }}
<div id="participant-{{ . }}">
<label for="participant-first-input-{{ . }}">Vorname</label>
<input type="text" name="participant-first-{{ . }}" id="participant-first-input-{{ . }}" />
<label for="participant-last-input-{{ . }}">Nachname</label>
<input type="text" name="participant-last-{{ . }}" id="participant-last-input-{{ . }}" />
<label for="participant-company-input-{{ . }}">Firma</label>
<input type="text" name="participant-company-{{ . }}" id="participant-company-input-{{ . }}" />
</div>
{{ end }}

39
templates/table.html Normal file
View File

@ -0,0 +1,39 @@
{{ define "row" }}
<tr>
<td>{{ .FirstName }}</td>
<td>{{ .LastName }}</td>
<td>{{ .Date }}</td>
<td>{{ .Time }}</td>
<td>{{ .State }}</td>
<td>{{ .Location }}</td>
{{ range .Participants }}
<td>{{ .FirstName }}</td>
<td>{{ .LastName }}</td>
<td>{{ .Company }}</td>
{{ end }}
</tr>
{{ end }}
{{ define "content" }}
<form>
<label for="search-input">Suche</label>
<input type="text" name="search" id="search-input" />
<button type="submit" hx-post="" hx-target="" hx-swap="">Suchen</button>
</form>
<table>
<tr>
<th colspan="2">Name Unterweiser</th>
<th>Datum</th>
<th>Uhrzeit</th>
<th>Stand</th>
<th>Ort</th>
<th colspan="2">Name Teilnehmer</th>
<th>Firma</th>
</tr>
{{ range . }}
{{ template "row" . }}
{{ end }}
</table>
{{ end }}