gpage分页管理开发中

This commit is contained in:
John
2018-04-22 11:26:19 +08:00
parent 4f66ab0953
commit d5c17371b4

View File

@ -9,20 +9,25 @@ package gpage
import (
"gitee.com/johng/gf/g/net/ghttp"
"math"
"fmt"
"net/url"
"gitee.com/johng/gf/g/util/gconv"
"strings"
)
type Page struct {
request *ghttp.Request
pageName string // page标签用来控制url页。比如说xxx.php?PBPage=2中的PBPage
nextPage string // 一页标签
prePage string // 上一页标签
firstPage string // 页标签
lastPage string // 尾页标签
pre_bar string // 上一分页条
next_bar string // 下一分页条
pageName string // 分页参数名称
nextPageTag string // 下一页标签
prevPageTag string // 一页标签
firstPageTag string // 页标签
lastPageTag string // 页标签
prevBar string // 上一分页条
nextBar string // 下一分页条
formatLeft string
formatRight string
isAjax bool // 是否支持AJAX分页模式
isAjax bool // 是否支持AJAX分页模式
totalSize int
pagebarNum int // 控制记录条的个数。
totalPage int // 总页数
@ -31,98 +36,45 @@ type Page struct {
url string // url地址头
offset int
}
/**
* constructor构造函数
*
* @param array array['total'], array['perpage'], array['currentPage'], array['url'], array['ajax']...
*/
func New(total, perpage int) {
total = intval(array['total']);
perpage = (array_key_exists('perpage',array)) ? intval(array['perpage']) : 10;
currentPage = (array_key_exists('currentPage',array)) ? intval(array['currentPage']) : '';
url = (array_key_exists('url',array)) ? array['url'] : '';
func New(totalSize, perPage, currentPage int, url string) *Page {
page := &Page {
pageName : "page",
totalSize : totalSize,
totalPage : int(math.Ceil(float64(totalSize/perPage))),
currentPage : currentPage,
offset : (currentPage - 1)*perPage,
url : url,
}
if strings.Index(url, "?") != -1 {
page.url = url + "&"
} else {
total = array;
perpage = 10;
currentPage ='';
url = '';
}
if ((!is_int(total)) || (total < 0)) {
this->_error(__FUNCTION__, 'invalid total');
}
if ((!is_int(perpage)) || (perpage <= 0)) {
this->_error(__FUNCTION__, 'invalid perpage');
}
if (!empty(array['pageName'])) {
// 设置pagename
this->set('pageName', array['pageName']);
}
this->_setCurrentPage(currentPage); // 设置当前页
this->_setUrl(url); // 设置链接地址
this->totalSize = total;
this->totalPage = ceil(total/perpage);
this->offset = (this->currentPage-1)*perpage;
if (!empty(array['ajax'])) {
this->openAjax(array['ajax']);//打开AJAX模式
page.url = url + "?"
}
page.url += page.pageName + "="
return page
}
// 启用AJAX分页
func (page *Page)EnableAjax(actionName string) {
page.isAjax = true
page.ajaxActionName = actionName
}
/**
* 设定类中指定变量名的值如果改变量不属于这个类将throw一个exception
*
* @param string var
* @param string value
*/
public function set(var, value)
{
if (inArray(var, get_object_vars(this))) {
this->var = value;
} else {
this->_error(__FUNCTION__, var." does not belong to PB_Page!");
// 获取显示"下一页"的内容.
func (page *Page) nextPage(curStyle , style string) string {
if page.currentPage < page.totalPage {
return page._getLink(page._getUrl(page.currentPage + 1), page.nextPageTag, "下一页", style)
}
return fmt.Sprintf(`<span class="%s">%s</span>`, curStyle, page.nextPageTag)
}
}
/**
* 使用AJAX模式。
*
* @param string action 默认ajax触发的动作名称。
*
* @return void
*/
public function openAjax(action)
{
this->isAjax = true;
this->ajaxActionName = action;
}
/**
* 获取显示"下一页"的代码.
*
* @param string style
* @return string
*/
public function nextPage(curStyle = '', style = '')
{
if (this->currentPage < this->totalPage) {
return this->_getLink(this->_getUrl(this->currentPage+1), this->nextPage, '下一页', style);
}
return '<span class="'.curStyle.'">'.this->nextPage.'</span>';
}
/**
* 获取显示“上一页”的代码
*
* @param string style
* @return string
*/
public function prePage(curStyle='', style='')
{
if (this->currentPage > 1) {
return this->_getLink(this->_getUrl(this->currentPage - 1), this->prePage, '上一页', style);
}
return '<span class="'.curStyle.'">'.this->prePage.'</span>';
/// 获取显示“上一页”的内容
func (page *Page) prevPage(curStyle , style string) string {
if page.currentPage > 1 {
return page._getLink(page._getUrl(page.currentPage - 1), page.prevPageTag, "上一页", style)
}
return fmt.Sprintf(`<span class="%s">%s</span>`, curStyle, page.prevPageTag)
}
/**
@ -130,87 +82,63 @@ return '<span class="'.curStyle.'">'.this->prePage.'</span>';
*
* @return string
*/
public function firstPage(curStyle = '', style = '')
{
if (this->currentPage == 1) {
return '<span class="'.curStyle.'">'.this->firstPage.'</span>';
}
return this->_getLink(this->_getUrl(1), this->firstPage, '第一页', style);
func (page *Page)firstPage(curStyle, style string) string {
if page.currentPage == 1 {
return fmt.Sprintf(`<span class="%s">%s</span>`, curStyle, page.firstPageTag)
}
return page._getLink(page._getUrl(1), page.firstPageTag, "第一页", style)
}
/**
* 获取显示“尾页”的代码
*
* @return string
*/
public function lastPage(curStyle='', style='')
{
if (this->currentPage == this->totalPage) {
return '<span class="'.curStyle.'">'.this->lastPage.'</span>';
}
return this->_getLink(this->_getUrl(this->totalPage), this->lastPage, '最后页', style);
// 获取显示“尾页”的内容
func (page *Page)lastPage(curStyle, style string) string {
if page.currentPage == page.totalPage {
return fmt.Sprintf(`<span class="%s">%s</span>`, curStyle, page.lastPageTag)
}
return page._getLink(page._getUrl(page.totalPage), page.lastPageTag, "最后页", style)
}
/**
* 获得分页条。
*
* @param 当前页码 curStyle
* @param 连接CSS style
* @return 分页条字符串
*/
public function nowbar(curStyle = '', style = '')
{
plus = ceil(this->pagebarNum / 2);
if (this->pagebarNum - plus + this->currentPage > this->totalPage) {
plus = (this->pagebarNum - this->totalPage + this->currentPage);
}
begin = this->currentPage - plus + 1;
begin = (begin>=1) ? begin : 1;
return = '';
for (i = begin; i < begin + this->pagebarNum; i++) {
if (i <= this->totalPage) {
if (i != this->currentPage) {
return .= this->_getText(this->_getLink(this->_getUrl(i), i, style));
} else {
return .= this->_getText('<span class="'.curStyle.'">'.i.'</span>');
}
} else {
break;
}
return .= "\n";
}
unset(begin);
return return;
// 获得分页条。
func (page *Page) nowBar(curStyle, style string) string {
plus := int(math.Ceil(float64(page.pagebarNum / 2)))
if page.pagebarNum - plus + page.currentPage > page.totalPage {
plus = page.pagebarNum - page.totalPage + page.currentPage
}
begin := page.currentPage - plus + 1
if begin < 1 {
begin = 1
}
ret := ""
for i := begin; i < begin + page.pagebarNum; i++ {
if i <= page.totalPage {
if i != page.currentPage {
ret += page._getText(page._getLink(page._getUrl(i), gconv.String(i), style, ""))
} else {
ret += page._getText(fmt.Sprintf(`<span class="%s">%d</span>`, curStyle, i))
}
} else {
break
}
ret += "\n"
}
return ret
}
/**
* 获取显示跳转按钮的代码
*
* @return string
*/
public function select()
{
url = this->_getUrl("' + this.value");
return = "<select name=\"PB_Page_Select\" onchange=\"window.location.href='url\">";
for (i=1; i <= this->totalPage; i++) {
if (i==this->currentPage) {
return .= '<option value="'.i.'" selected>'.i.'</option>';
} else {
return .= '<option value="'.i.'">'.i.'</option>';
}
}
unset(i);
return .= '</select>';
return return;
}
/**
* 获取mysql 语句中limit需要的值
*
* @return string
*/
public function offset()
{
return this->offset;
func (page *Page) selectBar() string {
url := page._getUrl("' + this.value")
ret := fmt.Sprintf(`<select name="gpage_select" onchange="window.location.href='%s'">`, url)
for i := 1; i <= page.totalPage; i++ {
if (i == page.currentPage) {
ret += fmt.Sprintf(`<option value="%d" selected>%d</option>`, i, i)
} else {
ret += fmt.Sprintf(`<option value="%d">%d</option>`, i, i)
}
}
ret += "</select>"
return ret
}
/**
@ -219,147 +147,67 @@ return this->offset;
* @param int mode 显示风格分类。
* @return string
*/
public function show(mode = 1)
{
switch (mode) {
case '1':
this->nextPage = '下一页';
this->prePage = '上一页';
return this->prePage()."<span class=\"current\">{this->currentPage}</span>".this->nextPage();
break;
case '2':
this->nextPage = '下一页>>';
this->prePage = '<<上一页';
this->firstPage = '首页';
this->lastPage = '尾页';
return this->firstPage().this->prePage().'<span class="current">['.this->currentPage.']</span>'.this->nextPage().this->lastPage().'第'.this->select().'页';
break;
case '3':
this->nextPage = '下一页';
this->prePage = '上一页';
this->firstPage = '首页';
this->lastPage = '尾页';
pageStr = this->firstPage()." ".this->prePage();
pageStr .= ' '.this->nowbar('current');
pageStr .= ' '.this->nextPage()." ".this->lastPage();
pageStr .= "<span>当前页{this->currentPage}/{this->totalPage}</span> <span>共{this->totalSize}条</span>";
return pageStr;
break;
case '4':
this->nextPage = '下一页';
this->prePage = '上一页';
this->firstPage = '首页';
this->lastPage = '尾页';
pageStr = this->firstPage()." ".this->prePage();
pageStr .= ' '.this->nowbar('current');
pageStr .= ' '.this->nextPage()." ".this->lastPage();
return pageStr;
break;
func (page *Page)show(mode int) string {
//switch (mode) {
//case '1':
//page.nextPage = '下一页'
//page.prevPage = '上一页'
//return page.prevPage()."<span class=\"current\">{page.currentPage}</span>".page.nextPage()
//break
//
//case '2':
//page.nextPage = '下一页>>'
//page.prevPage = '<<上一页'
//page.firstPage = '首页'
//page.lastPage = '尾页'
//return page.firstPage().page.prevPage().'<span class="current">[第'.page.currentPage.'页]</span>'.page.nextPage().page.lastPage().'第'.page.select().'页'
//break
//
//case '3':
//page.nextPage = '下一页'
//page.prevPage = '上一页'
//page.firstPage = '首页'
//page.lastPage = '尾页'
//pageStr = page.firstPage()." ".page.prevPage()
//pageStr .= ' '.page.nowbar('current')
//pageStr .= ' '.page.nextPage()." ".page.lastPage()
//pageStr .= "<span>当前页{page.currentPage}/{page.totalPage}</span> <span>共{page.totalSize}条</span>"
//return pageStr
//break
//
//case '4':
//page.nextPage = '下一页'
//page.prevPage = '上一页'
//page.firstPage = '首页'
//page.lastPage = '尾页'
//pageStr = page.firstPage()." ".page.prevPage()
//pageStr .= ' '.page.nowbar('current')
//pageStr .= ' '.page.nextPage()." ".page.lastPage()
//return pageStr
//break
//}
return ""
}
// 为指定的页面返回地址值
func (page *Page) _getUrl(pageNoStr string) string {
return page.url + pageNoStr
}
/*----------------private function (私有方法)-----------------------------------------------------------*/
/**
* 设置url头地址
* @param: string url
* @return boolean
*/
private function _setUrl(url = "")
{
if (!empty(url)) {
//手动设置
this->url = url.((stristr(url,'?')) ? '&' : '?').this->pageName."=";
} else {
parse = parse_url(_SERVER['REQUEST_URI']);
query = array();
if (!empty(parse['query'])) {
parse_str(parse['query'], query);
if (!empty(query) && isset(query[this->pageName])) {
unset(query[this->pageName]);
}
}
array = explode('?', _SERVER['REQUEST_URI']);
if (!empty(query)) {
this->url = array[0].'?'.http_build_query(query)."&{this->pageName}=";
} else {
this->url = array[0]."?{this->pageName}=";
}
}
// 获取分页显示文字比如说默认情况下_getText('<a href="">1</a>')将返回[<a href="">1</a>]
func (page *Page)_getText(str string) string {
return page.formatLeft + str + page.formatRight
}
/**
* 设置当前页面
*/
private function _setCurrentPage(currentPage)
{
if(empty(currentPage)) {
// 系统获取
if(isset(_GET[this->pageName])) {
this->currentPage = intval(_GET[this->pageName]);
}
} else {
//手动设置
this->currentPage = intval(currentPage);
}
// 获取链接地址
func (page *Page)_getLink(url, text, title, style string) string {
if len(style) > 0 {
style = fmt.Sprintf(`class="%s"`, style)
}
if (page.isAjax) {
return fmt.Sprintf(`<a %s href='#' onclick="%s('%s')">%s</a>`, style, page.ajaxActionName, url, text)
} else {
return fmt.Sprintf(`"<a %s href="%s" title="%s">%s</a>"`, style, url, title, text)
}
}
/**
* 为指定的页面返回地址值
*
* @param int pageNo
* @return string url
*/
private function _getUrl(pageNo=1)
{
return this->url.pageNo;
}
/**
* 获取分页显示文字比如说默认情况下_getText('<a href="">1</a>')将返回[<a href="">1</a>]
*
* @param String str
* @return string url
*/
private function _getText(str)
{
return this->formatLeft.str.this->formatRight;
}
//获取链接地址
private function _getLink(url, text, title='', style='')
{
style = (empty(style)) ? '' : 'class="'.style.'"';
if (this->isAjax) {
//如果是使用AJAX模式
return "<a style href='#' onclick=\"{this->ajaxActionName}('url');\">text</a>";
} else {
return "<a style href='url' title='title'>text</a>";
}
}
//出错处理方式
/**
* 展示错误病终止执行.
*
* @param string function 错误产生的函数名称.
* @param string errormsg 错误信息.
*
* @return void
*/
private function _error(function, errormsg)
{
die('Error in file <b>'.__FILE__.'</b> ,Function <b>'.function.'()</b> :'.errormsg);
}
}