ThinkPHP5.1+Ajax實現(xiàn)的無刷新分頁功能示例
本文實例講述了ThinkPHP5.1+Ajax實現(xiàn)的無刷新分頁功能。分享給大家供大家參考,具體如下:
無刷新分頁可以減輕服務(wù)器負(fù)擔(dān),利用Ajax技術(shù),請求部分信息,提高網(wǎng)站訪問速度,是網(wǎng)站建設(shè)的必備技術(shù)。
需要在后臺展示自定義屬性列表(lst.html),其中的列表部分摘出來,放到(paginate1.html)中:
<div class="row">
<div class="col-sm-12">
<div class="ibox float-e-margins">
<div class="ibox-content">
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>名稱</th>
<th>取值</th>
<th>顯示</th>
<th>排序</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{volist name="self" id="vo"}
<tr>
<td>{$vo.id}</td>
<td>{$vo.name}</td>
<td>{$vo.value}</td>
<td>
{if $vo.isshow==1}
<button type="button" class="btn btn-success btn-sm">是</button>
{else/}
<button type="button" class="btn btn-danger btn-sm">否</button>
{/if}
</td>
<td><input type="text" value="{$vo.order}" name=""></td>
<td>
<div class="btn-group open">
<button data-toggle="dropdown" class="btn btn-primary dropdown-toggle" aria-expanded="true">操作 <span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li><a href="">修改</a>
</li>
<li><a href="">刪除</a>
</li>
</ul>
</div>
</td>
</tr>
{/volist}
</tbody>
</table>
{$self|raw}
<div class="row">
<div class="col-sm-2">
<button class="btn btn-success" type="button" id="changeOrder">
<i class="fa fa-plus-square"></i>
<span class="bold">排序</span>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
其中self是服務(wù)器端傳遞過來的自定義屬性,并進(jìn)行了分頁操作:
$selfattribute_select = db("selfattribute")->paginate(5);
$this->assign("self",$selfattribute_select);
因為lst.html把列表摘了出來,所以還要在引入回去,才能使頁面完整,同時,為了方便進(jìn)行jquery操作,把列表用帶id的div包裹起來:
<div id="paginate">
{include file="selfattribute/paginate1"}
</div>
ThinkPHP5.1帶的分頁類使用的是BootStrap樣式,它在頁面顯示時實際會有一個pagination的類,查看源代碼如下:
<ul class="pagination">
<li class="disabled">
<span>«</span></li>
<li class="active">
<span>1</span></li>
<li>
<a href="/xkershouche/public/admin/selfattribute/lst.html?page=2" rel="external nofollow" rel="external nofollow" >2</a></li>
<li>
<a href="/xkershouche/public/admin/selfattribute/lst.html?page=3" rel="external nofollow" >3</a></li>
<li>
<a href="/xkershouche/public/admin/selfattribute/lst.html?page=4" rel="external nofollow" >4</a></li>
<li>
<a href="/xkershouche/public/admin/selfattribute/lst.html?page=5" rel="external nofollow" >5</a></li>
<li>
<a href="/xkershouche/public/admin/selfattribute/lst.html?page=6" rel="external nofollow" >6</a></li>
<li>
<a href="/xkershouche/public/admin/selfattribute/lst.html?page=2" rel="external nofollow" rel="external nofollow" >»</a></li>
</ul>
這就是好多人搞不懂的pagination是怎么來的。
然后開始寫js代碼,因為我們的分頁按鈕也在被請求的頁面當(dāng)中,屬于“未來”的元素,所以這里我們要用on方法,這個方法是jquery1.7以后的方法,注意自己的jquery版本。
<script type="text/javascript">
$(document).on('click', '.pagination a', function(event) {
var url = $(this).attr('href');
$.ajax({
url: url,
type: 'get',
})
.done(function(data) {
$("#paginate").html(data);
})
return false;
});
</script>
其中.done()方法和success方法是一樣的,return false是為了阻止默認(rèn)事件,防止直接跳轉(zhuǎn)。
那么服務(wù)器端就可以根據(jù)情況渲染模板了,代碼如下:
public function lst()
{
$selfattribute_select = db("selfattribute")->paginate(5);
$this->assign("self",$selfattribute_select);
if (request()->isAjax()) {
return view("paginate1");
} else {
return view();
}
}
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對大家基于ThinkPHP框架的PHP程序設(shè)計有所幫助。
相關(guān)文章
PHP中使用memcache存儲session的三種配置方法
下面簡單說下PHP項目分布式部署中,SESSION的同步方案中的一種,使用Memcache來存儲SESSION。并總結(jié)了三種配置方式,需要的朋友可以參考下2014-04-04
php實現(xiàn)查詢功能(數(shù)據(jù)訪問)
這篇文章主要介紹了php實現(xiàn)查詢功能,數(shù)據(jù)進(jìn)行訪問,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
php設(shè)計模式之觀察者模式實例詳解【星際爭霸游戲案例】
這篇文章主要介紹了php設(shè)計模式之觀察者模式,結(jié)合星際爭霸游戲案例形式分析了php觀察者模式相關(guān)概念、原理、用法與操作注意事項,需要的朋友可以參考下2020-03-03
Laravel 已登陸用戶再次查看登陸頁面的自動跳轉(zhuǎn)設(shè)置方法
今天小編就為大家分享一篇Laravel 已登陸用戶再次查看登陸頁面的自動跳轉(zhuǎn)設(shè)置方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09
php將數(shù)據(jù)庫中的電話號碼讀取出來并生成圖片
本PHP程序作用是從數(shù)據(jù)庫中讀取出手機號碼或其他數(shù)據(jù)并生成圖片,起到干擾采集防采集的作用。(英文或數(shù)字,如果要支持中文的話需要額外添加字庫)。本代碼為原創(chuàng)代碼。2008-08-08

