Sort buttons

This commit is contained in:
aceberg 2023-01-03 18:16:59 +07:00
parent 201250e3db
commit 5727a790fb
4 changed files with 79 additions and 21 deletions

View file

@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [v0.8.3] - 2023-01-03
### Added
- Clear table button (Config page)
- Delete host button
- Host name from DNS (Click on MAC to see host page)
- Sort buttons for all fields
## [v0.8.2] - 2023-01-02
### Added
- Option to ignore IP change [Issue #25](https://github.com/aceberg/WatchYourLAN/issues/25)

View file

@ -4,7 +4,9 @@ import (
"bytes"
"net"
"net/http"
"reflect"
"sort"
"strconv"
"github.com/aceberg/WatchYourLAN/internal/db"
"github.com/aceberg/WatchYourLAN/internal/models"
@ -47,36 +49,69 @@ func sortHandler(w http.ResponseWriter, r *http.Request) {
switch sortMethod {
case "name-up":
sort.SliceStable(AllHosts, func(i, j int) bool {
return AllHosts[i].Name < AllHosts[j].Name
})
sortByField("asc", "Name")
case "name-down":
sort.SliceStable(AllHosts, func(i, j int) bool {
return AllHosts[i].Name > AllHosts[j].Name
})
sortByField("desc", "Name")
case "ip-up":
sortByIPs("asc")
case "ip-down":
sortByIPs("desc")
case "mac-up":
sortByField("asc", "Mac")
case "mac-down":
sortByField("desc", "Mac")
case "hw-up":
sortByField("asc", "Hw")
case "hw-down":
sortByField("desc", "Hw")
case "date-up":
sort.SliceStable(AllHosts, func(i, j int) bool {
return AllHosts[i].Date < AllHosts[j].Date
})
sortByField("asc", "Date")
case "date-down":
sort.SliceStable(AllHosts, func(i, j int) bool {
return AllHosts[i].Date > AllHosts[j].Date
})
sortByField("desc", "Date")
case "known-up":
sort.SliceStable(AllHosts, func(i, j int) bool {
return AllHosts[i].Known < AllHosts[j].Known
})
sortByField("asc", "Known")
case "known-down":
sort.SliceStable(AllHosts, func(i, j int) bool {
return AllHosts[i].Known > AllHosts[j].Known
})
sortByField("desc", "Known")
default:
AllHosts = db.Select(AppConfig.DbPath)
}
http.Redirect(w, r, r.Header.Get("Referer"), 302)
}
func sortByField(method string, field string) {
type fieldHost struct {
Host models.Host
F string
}
toSort := []fieldHost{}
var oneSort fieldHost
for _, host := range AllHosts {
oneSort.Host = host
r := reflect.ValueOf(&host)
f := reflect.Indirect(r).FieldByName(field)
if field == "Known" {
oneSort.F = strconv.FormatUint(f.Uint(), 10)
} else {
oneSort.F = f.String()
}
toSort = append(toSort, oneSort)
}
switch method {
case "asc":
sort.Slice(toSort, func(i, j int) bool {
return toSort[i].F < toSort[j].F
})
case "desc":
sort.Slice(toSort, func(i, j int) bool {
return toSort[i].F > toSort[j].F
})
}
AllHosts = []models.Host{}
for _, oneSort := range toSort {
AllHosts = append(AllHosts, oneSort.Host)
}
}

View file

@ -49,7 +49,7 @@
<td>
<input name="name" type="text" class="form-control text-dark" value="{{ .Name }}">
</td>
<td>{{ .IP }}</td>
<td><a href="http://{{ .IP }}" target="_blank">{{ .IP }}</a></td>
<td><a href="/host?id={{ .ID }}">{{ .Mac }}</a></td>
<td>{{ .Hw }}</td>
<td>{{ .Date }}</td>

View file

@ -20,8 +20,23 @@
<i class="bi bi-caret-down"></i>
</button>
</th>
<th>Mac</th>
<th>Hardware</th>
<th>Mac<br>
<button type="submit" name="sort_method" value="mac-up" class="btn btn-primary btn-sm">
<i class="bi bi-caret-up"></i>
</button>
<button type="submit" name="sort_method" value="mac-down" class="btn btn-primary btn-sm">
<i class="bi bi-caret-down"></i>
</button>
</th>
<th>Hardware<br>
<button type="submit" name="sort_method" value="hw-up" class="btn btn-primary btn-sm">
<i class="bi bi-caret-up"></i>
</button>
<button type="submit" name="sort_method" value="hw-down" class="btn btn-primary btn-sm">
<i class="bi bi-caret-down"></i>
</button>
</th>
<th>Last seen<br>
<button type="submit" name="sort_method" value="date-up" class="btn btn-primary btn-sm">
<i class="bi bi-caret-up"></i>