分页展示列表

pull/30/head
unknown 5 years ago
parent 81a0b762ad
commit 37f85ad673

@ -14,8 +14,12 @@ import (
type IndexData struct {
Folders map[string]int
Mails interface{}
}
CurrentPage int
NextPage,PrePage string
NumPages map[string]template.HTML
}
const PAGE_SIZE=20
func main() {
log.Println("listen on 8080...")
http.HandleFunc("/", index)
@ -69,16 +73,44 @@ func list(w http.ResponseWriter, r *http.Request) {
authStrings := strings.Split(auth, "|")
render := new(IndexData)
render.CurrentPage = currentPage
var prePage int
if(currentPage-1) <=0 {
prePage=1
}else{
prePage=currentPage-1
}
render.PrePage = fmt.Sprintf("/list?fid=%s&page=%d",fid,prePage)
render.NextPage = fmt.Sprintf("/list?fid=%s&page=%d",fid,currentPage+1)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
folders :=tools.GetFolders(authStrings[0], authStrings[1], authStrings[2])
folders :=tools.GetFolders(authStrings[0], authStrings[1], authStrings[2],fid)
render.Folders = folders
}()
mails := tools.GetFolderMail(authStrings[0], authStrings[1], authStrings[2], fid,currentPage, 20)
mails := tools.GetFolderMail(authStrings[0], authStrings[1], authStrings[2], fid,currentPage, PAGE_SIZE)
render.Mails = mails
wg.Wait()
PageCount:=(render.Folders[fid]/PAGE_SIZE)
log.Println(PageCount)
numPages:=""
start:=currentPage-5
if start <=0 {
start=1
}
end:=start+11
for i:=start;i<end;i++{
active:=""
if currentPage==i{
active="active"
}
numPages+=fmt.Sprintf("<li class=\"page-item %s\"><a class=\"page-link\" href=\"/list?fid=%s&page=%d\">%d</a></li>",active,fid,i,i)
}
render.NumPages=map[string]template.HTML{"NumPages": template.HTML(numPages)}
t, _ := template.ParseFiles("./tmpl/index.html")
t.Execute(w, render)
}

@ -39,6 +39,17 @@
</li>
{{end}}
</ul>
<nav aria-label="..." style="margin:20px 0;">
<ul class="pagination">
<li class="page-item">
<a class="page-link" href="{{.PrePage}}">Previous</a>
</li>
{{.NumPages.NumPages}}
<li class="page-item">
<a class="page-link" href="{{.NextPage}}">Next</a>
</li>
</ul>
</nav>
</div>
</div>
</body>

@ -56,7 +56,7 @@ func connect(server string, email string, password string)*client.Client{
return c
}
//获取邮件夹
func GetFolders(server string, email string, password string)map[string]int{
func GetFolders(server string, email string, password string,folder string)map[string]int{
var c *client.Client
//defer c.Logout()
c=connect(server,email,password)
@ -75,7 +75,7 @@ func GetFolders(server string, email string, password string)map[string]int{
folders[m.Name]=0
}
for m,_ := range folders {
if m=="INBOX" {
if m==folder {
mbox, _ := c.Select(m, true)
if mbox != nil {
folders[m] = int(mbox.Messages)

Loading…
Cancel
Save