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/) The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/). 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 ## [v0.8.2] - 2023-01-02
### Added ### Added
- Option to ignore IP change [Issue #25](https://github.com/aceberg/WatchYourLAN/issues/25) - Option to ignore IP change [Issue #25](https://github.com/aceberg/WatchYourLAN/issues/25)

View file

@ -4,7 +4,9 @@ import (
"bytes" "bytes"
"net" "net"
"net/http" "net/http"
"reflect"
"sort" "sort"
"strconv"
"github.com/aceberg/WatchYourLAN/internal/db" "github.com/aceberg/WatchYourLAN/internal/db"
"github.com/aceberg/WatchYourLAN/internal/models" "github.com/aceberg/WatchYourLAN/internal/models"
@ -47,36 +49,69 @@ func sortHandler(w http.ResponseWriter, r *http.Request) {
switch sortMethod { switch sortMethod {
case "name-up": case "name-up":
sort.SliceStable(AllHosts, func(i, j int) bool { sortByField("asc", "Name")
return AllHosts[i].Name < AllHosts[j].Name
})
case "name-down": case "name-down":
sort.SliceStable(AllHosts, func(i, j int) bool { sortByField("desc", "Name")
return AllHosts[i].Name > AllHosts[j].Name
})
case "ip-up": case "ip-up":
sortByIPs("asc") sortByIPs("asc")
case "ip-down": case "ip-down":
sortByIPs("desc") 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": case "date-up":
sort.SliceStable(AllHosts, func(i, j int) bool { sortByField("asc", "Date")
return AllHosts[i].Date < AllHosts[j].Date
})
case "date-down": case "date-down":
sort.SliceStable(AllHosts, func(i, j int) bool { sortByField("desc", "Date")
return AllHosts[i].Date > AllHosts[j].Date
})
case "known-up": case "known-up":
sort.SliceStable(AllHosts, func(i, j int) bool { sortByField("asc", "Known")
return AllHosts[i].Known < AllHosts[j].Known
})
case "known-down": case "known-down":
sort.SliceStable(AllHosts, func(i, j int) bool { sortByField("desc", "Known")
return AllHosts[i].Known > AllHosts[j].Known
})
default: default:
AllHosts = db.Select(AppConfig.DbPath) AllHosts = db.Select(AppConfig.DbPath)
} }
http.Redirect(w, r, r.Header.Get("Referer"), 302) 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> <td>
<input name="name" type="text" class="form-control text-dark" value="{{ .Name }}"> <input name="name" type="text" class="form-control text-dark" value="{{ .Name }}">
</td> </td>
<td>{{ .IP }}</td> <td><a href="http://{{ .IP }}" target="_blank">{{ .IP }}</a></td>
<td><a href="/host?id={{ .ID }}">{{ .Mac }}</a></td> <td><a href="/host?id={{ .ID }}">{{ .Mac }}</a></td>
<td>{{ .Hw }}</td> <td>{{ .Hw }}</td>
<td>{{ .Date }}</td> <td>{{ .Date }}</td>

View file

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