### 分頁 Page
分頁是在進行列表 List 查詢過程中,基本都會使用到,為了更加快速的進行產品開發,我們對分頁操作進行了封裝,并且繼承了 ThinkPHP 原生的 Page.class.php 類庫,封裝了分頁調用函數。
**位置:**Application\Common\Common\function.php
**參數:**
> * @param int $count 要分頁的總記錄數
> * @param int $pagesize 每頁查詢條數
**調用:**
> $page = getpage($count, 10); // 調用分頁函數
> limit($page->firstRow.','.$page->listRows) // 傳值分頁搜索
> $this->assign('page', $page->showFront()); // 分頁賦值模版
**實例:**
1、Controller 控制器的方法中:
~~~
$count = M('system_log')->count(); // 統計數量
$page = getpage($count, 10); // 調用分頁函數,Param總數,每一頁顯示10條
$log_list = M('system_log')
->limit($page->firstRow.','.$page->listRows) // 傳值分頁搜索
->select();
$this->assign('log_list', $log_list);
$this->assign('page', $page->showFront()); // 分頁賦值模版
$this->display();
~~~
2、View 模版賦值
~~~
<volist name="log_list" id="vo">
<tr>
<td>{$vo.id}</td>
<td>{$vo.nickname}</td>
</tr>
</volist>
{$page} <!-- 調用分頁 -->
~~~
在需要調用的分頁位置,一般在列表 List 下面
* * * * *
**帶搜索的分頁實例:**
在進行列表分頁的時候,經常會用到多個搜索條件的列表,而且要進行分頁,下面實例講到具體的使用和調用
1、搜索分頁第一步是進行條件的搜索,搜索 Form 的提交方式我們一般選擇 GET,POST 也可以,而 GET 我們可以在 URL 更完成的看到搜索的條件的傳值,更有利于分頁的調試,那么約定好搜索就使用 GET 方式傳值,其他的寫法都沒什么區別,**分頁方法會自動取值并賦值在分頁的 URL 上面進行分頁**。
搜索的URL:
> index?user_id=17 // 傳值 user_id
分頁后的URL
> index/user_id/17/p/2.html // 分頁帶出 user_id
2、多選條件的搜索分頁
在進行 Select 多選和 Input checkbox 多選框等的搜索的時候,后臺接收的數據會是一個數組,那該怎么在分頁進行傳值呢?
> array (size=3)
> 0 => string 'banana'
> 1 => string 'grape'
> 2 => string 'mango'
為了解決多選數據是數組的問題,在封裝的函數里面將數組自動判斷轉換成用“-” 符號連接的字符串,在賦值在分頁的URL上面
> index/name/banana-grape-mango/p/2.html
而在后臺取數據的時候,**提交 Form 表單的時候獲取的 $name 是一個數組,在分頁的時候獲取的 $name 是一個字符串**,需要將字符串進行轉換為數組
~~~
$name = I('param.name');
if(!is_array($name)) {
$name = explode("-", $name); // 拆分字符串為數組
}
~~~
**完整代碼:**
~~~
// 調用 Think Page 的分頁
/*
* @param int $count 要分頁的總記錄數
* @param int $pagesize 每頁查詢條數
* @return \Think\Page
*/
function getpage($count, $pagesize = 10){
$p = new Think\Page($count,$pagesize);
$p->lastSuffix = false;
$p->setConfig('prev','上一頁');
$p->setConfig('next','下一頁');
$p->setConfig('last','尾頁');
$p->setConfig('first','首頁');
$p->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
$arr_input = I('param.'); // 獲取 input 搜索表單數據
foreach($arr_input as $key => $val) {
if (is_array($val)) { // 如果是數組拆分成連接字符串
$str = '';
foreach ($val as $k => $v) {
$str = $str . '-' . $v;
}
$val = substr($str,1,strlen($str)); // 去掉第一個字符 -
}
$p->parameter[$key] = $val;
}
return $p;
}
~~~
**重寫的 page.class.php 類的 show 方法:**
~~~
/**
* [showFront 前端分頁樣式]
* @return [string] [組裝好的鏈接]
*/
public function showFront() {
$this->config['header'] = '<li><span class="rows">%NOW_PAGE% / %TOTAL_ROW% 條記錄</span></li>';
$this->config['prev'] = "上一頁";
$this->config['next'] = "下一頁";
$this->rollPage = 4;
if(0 == $this->totalRows) return '';
/* 生成URL */
$this->parameter[$this->p] = '[PAGE]';
$this->url = U(ACTION_NAME, $this->parameter);
/* 計算分頁信息 */
$this->totalPages = ceil($this->totalRows / $this->listRows); //總頁數
if(!empty($this->totalPages) && $this->nowPage > $this->totalPages) {
$this->nowPage = $this->totalPages;
}
/* 計算分頁零時變量 */
$now_cool_page = $this->rollPage/2;
$now_cool_page_ceil = ceil($now_cool_page);
$this->lastSuffix && $this->config['last'] = $this->totalPages;
//上一頁
$up_row = $this->nowPage - 1;
$up_page = $up_row > 0 ? '<li><a href="' . $this->url($up_row) . '">' . $this->config['prev'] . '</a></li>' : '';
//下一頁
$down_row = $this->nowPage + 1;
$down_page = ($down_row <= $this->totalPages) ? '<li>
<a href="' . $this->url($down_row) . '">' . $this->config['next'] . '</a>
</li>' : '';
//第一頁
$the_first = '';
if($this->totalPages > $this->rollPage && ($this->nowPage - $now_cool_page) >= 1){
$the_first = '<li><a href="' . $this->url(1) . '">' . $this->config['first'] . '</a></li>';
}
//最后一頁
$the_end = '';
if($this->totalPages > $this->rollPage && ($this->nowPage + $now_cool_page) < $this->totalPages){
$the_end = '<li><a href="' . $this->url($this->totalPages) . '">' . $this->config['last'] . '</a></li>';
}
//數字連接
$link_page = "";
for($i = 1; $i <= $this->rollPage; $i++){
if(($this->nowPage - $now_cool_page) <= 0 ){
$page = $i;
}elseif(($this->nowPage + $now_cool_page - 1) >= $this->totalPages){
$page = $this->totalPages - $this->rollPage + $i;
}else{
$page = $this->nowPage - $now_cool_page_ceil + $i;
}
if($page > 0 && $page != $this->nowPage){
if($page <= $this->totalPages){
$link_page .= '<li><a href="' . $this->url($page) . '">' . $page . '</a></li>';
}else{
break;
}
}else{
if($page > 0 && $this->totalPages != 1){
$link_page .= '<li><span class="current">' . $page . '</span></li>';
}
}
}
//替換分頁內容
$page_str = str_replace(
array('%UP_PAGE%', '%HEADER%', '%NOW_PAGE%', '%DOWN_PAGE%',
'%FIRST%', '%LINK_PAGE%', '%END%', '%TOTAL_ROW%', '%TOTAL_PAGE%'),
array(
$up_page,
$this->config['header'],
$this->nowPage,
$down_page, $the_first,
$link_page,
$the_end,
$this->totalRows,
$this->totalPages
),
$this->config['theme']);
// return "{$page_str}"; 底下是重寫的分頁樣式 2016-12-26 jig update
$result = '<div class="col-xs-4">
<div class="page-num">共' . $this->totalPages . '頁' . $this->totalRows . '條數據</div>
</div>
<div class="col-xs-8">
<div class="erppagination2">
<ul class="pagination pagination-sm" style="margin-top:0;">'.
$page_str
.'</ul>
</div>
</div>';
return $result;
}
~~~
- 模版
- 前言
- 項目架構
- 項目規范
- HTML
- CSS
- Javascript
- PHP
- MySQL
- 注意規范
- 開發版本管理
- 開發流程
- 系統配置
- 阿里云服務器配置
- 計劃任務配置說明
- 開發示例
- Page分頁
- Search_param搜索結果賦值
- Add新增
- Edit編輯
- Ajax表單驗證
- Ajax二級聯動
- Excel 導出數據首位不去0的方法
- POS總部控制
- 下載CSV格式的模板
- 訂單唯一碼表和訂單SKU表實收金額生成
- 快捷日期選擇
- JS函數
- ajax_send
- ajax_result
- createQrCodes
- createBarCodes
- printTpl
- JS插件
- BootstrapValidator表單驗證插件
- Address省市區插件
- Bootstrap-datepicker日期插件
- Bootstrap-select多選框插件
- Toastr消息提示插件
- PalyAudit掃描聲音提示插件
- WebUploader多圖片上傳插件
- Ueditor富文本編輯器插件
- Function
- alert
- object_to_array
- array_to_object
- get_address
- set_param_url
- get_shops_name
- get_user_name
- get_warehouse
- get_cheapest_sku
- print_attr(新)
- print_img(新)
- get_spu_no(新)
- get_type_name(新)
- get_brand_en(新)
- get_cat_name(新)
- get_attr_name(新)
- spu_cat_info(新)
- get_time_event_price
- get_vendors
- check_total_reduce
- check_total_discount
- get_inventory
- get_delivery
- get_sale_inventory
- get_customer_name
- phone_protection
- get_order_no
- get_event_name
- get_order_status
- get_item_status
- get_ditch_name
- get_card_no
- get_shop_sales
- get_pay_name
- get_season
- amt_format
- get_cat_parent
- print_attr_id
- round_bcadd
- round_bcsub
- round_bcmul
- round_bcdiv
- get_account_name
- Controller
- Common_BaseController
- check_membership_card
- get_menu_list
- importErrorMassage
- Wpos_IndexController
- get_customer_vip_card
- get_shops_id
- calculate_active_integral
- check_numbers_active
- check_goods_active
- Woms_IndexController
- Model
- View
- category
- cycle_date.html
- shop_select門店多選搜索框
- 品牌A-Z排序多選brand_mc.html
- 供應商代碼A-Z排序vendor_no_mc.html
- Lib
- BuyerLib
- WarehouseLib
- EventLib
- getTimeEventPrice
- getVipType
- getEvent
- orderTotalEvent
- orderTimeEvent
- getTotalReduce
- getTotalDiscount
- SaleLib
- CustomerLib
- addCustomerService
- GiftcardLib
- WechatLib
- wxRefund
- OrdersLib
- orderLog
- calculatePayinAmount
- calculateSubtotal
- correctPayinAmount
- saveOrderAddress
- getOrderAddress
- setDeliveryNo
- SyncLib
- updateOuterStock
- UserLib
- createCommission
- FlowLib
- orderList
- addOrder
- addLog
- orderInfo
- checkSku
- orderSave
- orderStop
- orderExecute
- skuEdit
- orderPrinta
- scanGoods
- boxClose
- orderOut
- take
- bview
- check
- deliveryStatus
- checkGoods
- GoodsLib
- createGoodsNo
- createNewGoodsNo
- getSystemStyleNo
- getDim
- MallLib
- smsLog
- GoodsBaseLib
- getBrandInfo
- getBrandsInfo
- getAttrIdArray
- getPrintAttr
- getMustAttr
- getCatIdInfo
- valTypeId
- valsTypeId
- getCatNoInfo
- getCatInfo
- getAttrArr
- getAttrInfo
- getValInfo
- getAttrId
- getValId
- getAttrSeaon
- getValueId
- PointsLog
- pointsIn
- pointsUp
- EcGoodsLib
- getSkuInventory
- Tools
- CsvTools
- csvImport
- csvExport
- ExcelTools
- importExcel
- exportExcel
- exportHeadExcel
- MailTools
- SmsTools
- sendMessage
- UploadTools
- ExportTools
- exportData
- TaobaoTools
- getOnsaleItems
- getSkusItems
- PicturesTools
- uploadPicture
- Plugins
- WxBase
- Taobao
- 問題反饋