ThinkPHP3.2分頁顯示,超詳細(xì),超簡單

第一步:導(dǎo)入類

下載AjaxPage.class.php

我這里的提供的是之前3.1時(shí)候用的,在3.2用的時(shí)候需要在類里面加個(gè)命名空間

Paste_Image.png

這個(gè)類放在ThinkPHP-Library-Org-Util路徑下

Paste_Image.png

第二步:php里該寫的代碼

ProductController是我建的產(chǎn)品類

01classProductController?extendsPublicController{

02?

03????publicfunctionindex()

04????{

05????????//導(dǎo)入三方庫

06?

07????????//?查詢滿足要求的總記錄數(shù)

08????????$count=?$this->model->count();

09?

10????????//創(chuàng)建對(duì)象,指定對(duì)象執(zhí)行的方法

11????????$Page=?new\Org\Util\AjaxPage($count,7,"productPage");//productPage是調(diào)用js中方法,通過該方法給js傳遞頁碼值,7代表一頁顯示7條數(shù)據(jù)

12?

13????????//?實(shí)例化分頁類?傳入總記錄數(shù)和每頁顯示的記錄數(shù)

14????????$show=?$Page->show();//?分頁顯示輸出

15????????//?進(jìn)行分頁數(shù)據(jù)查詢?注意limit方法的參數(shù)要使用Page類的屬性

16????????$list=?$this->model->order('id?asc')->limit($Page->firstRow.','.$Page->listRows)->select();

17?

18?

19????????//判斷是不是ajax,

20????????if(!IS_AJAX){

21?

22????????????$this->assign('result',$list);//?賦值數(shù)據(jù)集

23????????????$this->assign('page',$show);//?賦值分頁輸出

24????????????$this->view();?//?輸出模板

25?

26????????}else{

27?

28????????????$data['result']=$list;

29????????????$data['page']?=?$show;

30????????????$this->ajaxReturn($data);

31????????}

32????}

33?

34????//編輯產(chǎn)品

35????publicfunctionedit(){

36?

37????????//進(jìn)入編輯頁面,顯示待編輯的信息

38????????if(!empty($_GET)){

39????????????$result=?$this->model->where($_GET)->find();

40????????????$this->assign('result',$result);

41????????}

42????????//編輯完提交,保存,跳轉(zhuǎn)回首頁

43????????if(!empty($_POST)){

44?

45????????????$id=?$_POST['id'];

46????????????$result=?$this->model->where("id=$id")->save($_POST);

47????????????//0表示保存失敗

48????????????if($result!=?0){

49????????????????redirect('index');

50????????????}

51????????}

52????????$this->view();

53????}

54?

55????//刪除產(chǎn)品

56????publicfunctiondelete(){

57?

58????????$id=?$_GET['id'];

59????????$result=?$this->model->where("id=$id")->delete();

60????????if($result!=?0){

61????????????$this->redirect('index');

62????????}

63????}

64}

3.2的寫法

3.1的寫法

page輸出顯示是這樣的

result輸出顯示是這樣的

第三步:html該寫的代碼,把a(bǔ)ssign過來的數(shù)據(jù)賦值到td上

下面是對(duì)應(yīng)的代碼

01

02????

03????????

04????????????NO

05????????????產(chǎn)品名稱???????????????????????

06????????????單價(jià)

07????????????備注

08????????????操作

09????????

10????

11????

12????????

13????????????

14????????????????{$val.no}

15????????????????{$val.pro_name}

16????????????????{$val.price}

17????????????????{$val.remark}

18????????????????

19????????????????????編輯

20????????????????????刪除

21????????????????

22????????????

23????????

24????

25

26

27????

28????????{$page}

29????

30

解釋下關(guān)鍵代碼:

這里設(shè)置個(gè)id,在js里面會(huì)用到

foreach為tp的標(biāo)簽,遍歷數(shù)組用的,name對(duì)應(yīng)數(shù)據(jù)源,item是循環(huán)變量

編輯edit是寫在js里的方法,根據(jù)id編輯內(nèi)容

刪除del是寫在js里的方法,根據(jù)id刪除內(nèi)容

{$page}是顯示1,2,3分頁碼的,這個(gè)class類后面js會(huì)用到

第四步:完成前三步,首頁的信息肯定可以顯示出來了,但是點(diǎn)擊頁碼沒有反應(yīng),這時(shí)候,我們需要在js里面請(qǐng)求新的數(shù)據(jù)

01

02????//點(diǎn)擊頁碼,重新請(qǐng)求數(shù)據(jù)

03????functionproductPage(id)?{

04?

05????????varurl?=?'';

06????????url?=?'__APP__/home/product/index/p/'+id;

07?

08????????$.get(url,function(content)?{

09?

10????????????//重寫html代碼

11????????????//將pagination?div重寫一遍

12????????????//將json轉(zhuǎn)成對(duì)象類型

13????????????//var?data?=?eval('('+content+')');??//強(qiáng)制將json轉(zhuǎn)成對(duì)象類型

14????????????vardata?=?content;

15?

16????????????//輸出頁碼

17????????????$('.pagination').html(data.page);

18?

19????????????varl?=?'';

20????????????for(vari?=?0;?i?<?data.result.length;?i++){

21?

22????????????????l?+=?'';

23????????????????l?+=????''+?data.result[i].no?+'';

24????????????????l?+=????''+?data.result[i].pro_name?+'';

25????????????????l?+=????''+?data.result[i].price?+'';

26????????????????l?+=????''+?data.result[i].remake?+'';

27????????????????l?+=????'編輯刪除';

28????????????????l?+=?'';

29????????????}

30????????????//重新輸出整個(gè)表格

31????????????$('#neirong').html(l);

32????????});

33????}

34?

35????//點(diǎn)擊編輯按鈕

36????functionedit(id)?{

37????????window.location.href='__APP__/home/product/edit/id/'+id;

38????}

39?

40????//點(diǎn)擊刪除按鈕

41????functiondel(id)?{

42????????if(confirm("您確定要?jiǎng)h除么!")){

43????????????window.location.href='__APP__/home/product/delete/id/'+id;

44????????}

45????}

46

解釋下關(guān)鍵代碼:

function productPage(id) {這是我們php代碼$Page = new \Org\Util\AjaxPage($count,7,"productPage")里的方法

url = '__APP__/home/product/index/p/'+id;p參數(shù)為頁碼,

//var data = eval('('+content+')'); //強(qiáng)制將json轉(zhuǎn)成對(duì)象類型這里是注釋,沒錯(cuò).建議在這步alert一下content,看看有沒有數(shù)據(jù),沒有就打開這段代碼試試.

本文標(biāo)題:ThinkPHP3.2分頁顯示,超詳細(xì),超簡單

固定鏈接:http://www.tcode.me/article/751.html來自淘代碼轉(zhuǎn)載請(qǐng)注明

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容