go语言web项目翻页实现

u013834131 · · 1809 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

翻译自php的一个分页函数,能用!代码写得比较垃圾,抽空再改进。


package main

import (
	"bytes"
	"fmt"
	"math"
	"os"
	"strconv"
	"strings"
)

func main() {
	var html string
	html = multi(100, 10, 3, "http://xxoo.io")

	userFile := "log.html"
	fout, _ := os.Create(userFile)

	fmt.Println(html)
	fout.WriteString(html)

	fout.Close()
}

func multi(num int, perpage int, curpage int, url string) string {
	var (
		mpurl string
		from  int
		to    int
		//ret   *bytes.Buffer = new(bytes.Buffer)
		ret bytes.Buffer = bytes.Buffer{}
	)

	if strings.Index(url, "?") != -1 {
		mpurl = url + "&"
	} else {
		mpurl = url + "?;"
	}

	if num > perpage {
		page := 10
		offset := 5
		pages := int(math.Ceil(float64(num / perpage)))
		if page > pages {
			from = 1
			to = pages
		} else {
			from = curpage - offset
			to = curpage + page + offset - 1
			if from < 1 {
				to = curpage + 1 - from
				from = 1
				if (to-from) < page && (to-from) < pages {
					to = page
				}
			} else if to > pages {
				from = curpage - pages + to
				to = pages
				if (to-from) < page && (to-from) < pages {
					from = pages - page + 1
				}
			}
		}

		if (curpage-offset) > 1 && pages > page {
			ret.WriteString("<a href=\"" + mpurl + "page=1\" class=\"p_redirect\">«</a>")
		}

		if curpage > 1 {
			ret.WriteString("<a href=\"" + mpurl + "page=" + strconv.Itoa(curpage-1) + "\" class=\"p_redirect\">‹</a>")
		}

		for i := from; i <= to; i++ {
			if i == curpage {
				ret.WriteString("<span class=\"p_curpage\">" + strconv.Itoa(i) + "</span>")
			} else {
				ret.WriteString("<a href=\"" + mpurl + "page=" + strconv.Itoa(i) + "\" class=\"p_num\">" + strconv.Itoa(i) + "</a>")
			}
		}

		if curpage < pages {
			ret.WriteString("<a href=\"" + mpurl + "page=" + strconv.Itoa(curpage+1) + "\" class=\"p_redirect\">›</a>")
		}

		if to < pages {
			ret.WriteString("<a href=\"" + mpurl + "page=" + strconv.Itoa(pages) + "\" class=\"p_redirect\">»</a>")
		}

		if ret.Len() > 0 {
			return "<div class=\"p_bar\"><span class=\"p_info\">Records:" + strconv.Itoa(num) + "</span>" + ret.String() + "</div>"
		}

	}
	return ""

}

/*
function multi($num, $perpage, $curpage, $mpurl) {
	$multipage = '';
	$mpurl .= strpos($mpurl, '?') ? '&' : '?';
	if($num > $perpage) {
		$page = 10;
		$offset = 5;
		$pages = @ceil($num / $perpage);
		if($page > $pages) {
			$from = 1;
			$to = $pages;
		} else {
			$from = $curpage - $offset;
			$to = $curpage + $page - $offset - 1;
			if($from < 1) {
				$to = $curpage + 1 - $from;
				$from = 1;
				if(($to - $from) < $page && ($to - $from) < $pages) {
					$to = $page;
				}
			} elseif($to > $pages) {
				$from = $curpage - $pages + $to;
				$to = $pages;
				if(($to - $from) < $page && ($to - $from) < $pages) {
					$from = $pages - $page + 1;
				}
			}
		}

		$multipage = ($curpage - $offset > 1 && $pages > $page ? '<a href="'.$mpurl.'page=1" class="p_redirect">«</a>' : '').($curpage > 1 ? '<a href="'.$mpurl.'page='.($curpage - 1).'" class="p_redirect">‹</a>' : '');
		for($i = $from; $i <= $to; $i++) {
			$multipage .= $i == $curpage ? '<span class="p_curpage">'.$i.'</span>' : '<a href="'.$mpurl.'page='.$i.'" class="p_num">'.$i.'</a>';
		}
		$multipage .= ($curpage < $pages ? '<a href="'.$mpurl.'page='.($curpage + 1).'" class="p_redirect">›</a>' : '').($to < $pages ? '<a href="'.$mpurl.'page='.$pages.'" class="p_redirect">»</a>' : '');
		$multipage = $multipage ? '<div class="p_bar"><span class="p_info">Records:'.$num.'</span>'.$multipage.'</div>' : '';
	}
	return $multipage;
}

*/



有疑问加站长微信联系(非本文作者)

本文来自:CSDN博客

感谢作者:u013834131

查看原文:go语言web项目翻页实现

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1809 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传