From ec41bfe152a166ede9a18700a465a59ae40ca9ee Mon Sep 17 00:00:00 2001 From: John Date: Sun, 22 Apr 2018 10:07:24 +0800 Subject: [PATCH] =?UTF-8?q?gpage=E5=88=86=E9=A1=B5=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E5=BC=80=E5=8F=91=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/util/gpage/gpage.go | 365 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 365 insertions(+) create mode 100644 g/util/gpage/gpage.go diff --git a/g/util/gpage/gpage.go b/g/util/gpage/gpage.go new file mode 100644 index 000000000..fad7f6579 --- /dev/null +++ b/g/util/gpage/gpage.go @@ -0,0 +1,365 @@ +// Copyright 2018 gf Author(https://gitee.com/johng/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://gitee.com/johng/gf. + +// 分页管理. +package gpage + +import ( + "gitee.com/johng/gf/g/net/ghttp" +) + +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 // 下一分页条 + formatLeft string + formatRight string + isAjax bool // 是否支持AJAX分页模式 + totalSize int + pagebarNum int // 控制记录条的个数。 + totalPage int // 总页数 + ajaxActionName string // AJAX动作名 + currentPage int // 当前页 + 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'] : ''; + } 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模式 +} + +} + +/** +* 设定类中指定变量名的值,如果改变量不属于这个类,将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!"); +} + +} + +/** + * 使用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 ''.this->nextPage.''; +} + +/** +* 获取显示“上一页”的代码 +* +* @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 ''.this->prePage.''; +} + +/** +* 获取显示“首页”的代码 +* +* @return string +*/ +public function firstPage(curStyle = '', style = '') +{ +if (this->currentPage == 1) { +return ''.this->firstPage.''; +} +return this->_getLink(this->_getUrl(1), this->firstPage, '第一页', style); +} + +/** +* 获取显示“尾页”的代码 +* +* @return string +*/ +public function lastPage(curStyle='', style='') +{ +if (this->currentPage == this->totalPage) { +return ''.this->lastPage.''; +} +return this->_getLink(this->_getUrl(this->totalPage), this->lastPage, '最后页', 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(''.i.''); +} +} else { +break; +} +return .= "\n"; +} +unset(begin); +return return; +} +/** +* 获取显示跳转按钮的代码 +* +* @return string +*/ +public function select() +{ +url = this->_getUrl("' + this.value"); +return = "'; +return return; +} + +/** +* 获取mysql 语句中limit需要的值 +* +* @return string +*/ +public function offset() +{ +return this->offset; +} + +/** +* 控制分页显示风格(你可以继承后增加相应的风格) +* +* @param int mode 显示风格分类。 +* @return string +*/ +public function show(mode = 1) +{ +switch (mode) { +case '1': +this->nextPage = '下一页'; +this->prePage = '上一页'; +return this->prePage()."{this->currentPage}".this->nextPage(); +break; + +case '2': +this->nextPage = '下一页>>'; +this->prePage = '<<上一页'; +this->firstPage = '首页'; +this->lastPage = '尾页'; +return this->firstPage().this->prePage().'[第'.this->currentPage.'页]'.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 .= "当前页{this->currentPage}/{this->totalPage} 共{this->totalSize}条"; +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; +} + +} + +/*----------------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}="; +} +} +} + +/** +* 设置当前页面 +*/ +private function _setCurrentPage(currentPage) +{ +if(empty(currentPage)) { +// 系统获取 +if(isset(_GET[this->pageName])) { +this->currentPage = intval(_GET[this->pageName]); +} +} else { +//手动设置 +this->currentPage = intval(currentPage); +} +} + +/** +* 为指定的页面返回地址值 +* +* @param int pageNo +* @return string url +*/ +private function _getUrl(pageNo=1) +{ +return this->url.pageNo; +} + +/** +* 获取分页显示文字,比如说默认情况下_getText('1')将返回[1] +* +* @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 "ajaxActionName}('url');\">text"; +} else { +return "text"; +} +} + +//出错处理方式 +/** + * 展示错误病终止执行. + * + * @param string function 错误产生的函数名称. + * @param string errormsg 错误信息. + * + * @return void + */ +private function _error(function, errormsg) +{ +die('Error in file '.__FILE__.' ,Function '.function.'() :'.errormsg); +} +} + + + + + + + +