本文實(shí)例講述了ThinkPHP5.1+Ajax實(shí)現(xiàn)的無(wú)刷新分頁(yè)功能。分享給大家供大家參考,具體如下:
無(wú)刷新分頁(yè)可以減輕服務(wù)器負(fù)擔(dān),利用Ajax技術(shù),請(qǐng)求部分信息,提高網(wǎng)站訪問(wèn)速度,是網(wǎng)站建設(shè)的必備技術(shù)。
需要在后臺(tái)展示自定義屬性列表(lst.html),其中的列表部分摘出來(lái),放到(paginate1.html)中:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
< 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ù)器端傳遞過(guò)來(lái)的自定義屬性,并進(jìn)行了分頁(yè)操作:
1
2
|
$selfattribute_select = db( "selfattribute" )->paginate(5); $this ->assign( "self" , $selfattribute_select ); |
因?yàn)閘st.html把列表摘了出來(lái),所以還要在引入回去,才能使頁(yè)面完整,同時(shí),為了方便進(jìn)行jquery操作,把列表用帶id的div包裹起來(lái):
1
2
3
|
< div id = "paginate" > {include file="selfattribute/paginate1"} </ div > |
ThinkPHP5.1帶的分頁(yè)類使用的是BootStrap樣式,它在頁(yè)面顯示時(shí)實(shí)際會(huì)有一個(gè)pagination的類,查看源代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
< 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是怎么來(lái)的。
然后開(kāi)始寫js代碼,因?yàn)槲覀兊姆猪?yè)按鈕也在被請(qǐng)求的頁(yè)面當(dāng)中,屬于“未來(lái)”的元素,所以這里我們要用on方法,這個(gè)方法是jquery1.7以后的方法,注意自己的jquery版本。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<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ù)情況渲染模板了,代碼如下:
1
2
3
4
5
6
7
8
9
10
|
public function lst() { $selfattribute_select = db( "selfattribute" )->paginate(5); $this ->assign( "self" , $selfattribute_select ); if (request()->isAjax()) { return view( "paginate1" ); } else { return view(); } } |
希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。
原文鏈接:https://blog.csdn.net/pan_yuyuan/article/details/81947057