第一步:導入類
下載AjaxPage.class.php
我這里的提供的是之前3.1時候用的,在3.2用的時候需要在類里面加個命名空間

Paste_Image.png
這個類放在ThinkPHP-Library-Org-Util路徑下

Paste_Image.png
第二步:php里該寫的代碼
ProductController是我建的產(chǎn)品類
01classProductController?extendsPublicController{
02?
03????publicfunctionindex()
04????{
05????????//導入三方庫
06?
07????????//?查詢滿足要求的總記錄數(shù)
08????????$count=?$this->model->count();
09?
10????????//創(chuàng)建對象,指定對象執(zhí)行的方法
11????????$Page=?new\Org\Util\AjaxPage($count,7,"productPage");//productPage是調(diào)用js中方法,通過該方法給js傳遞頁碼值,7代表一頁顯示7條數(shù)據(jù)
12?
13????????//?實例化分頁類?傳入總記錄數(shù)和每頁顯示的記錄數(shù)
14????????$show=?$Page->show();//?分頁顯示輸出
15????????//?進行分頁數(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????????//進入編輯頁面,顯示待編輯的信息
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該寫的代碼,把assign過來的數(shù)據(jù)賦值到td上

下面是對應的代碼
01
02????
03????????
04????????????NO
05????????????產(chǎn)品名稱???????????????????????
06????????????單價
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
解釋下關鍵代碼:
這里設置個id,在js里面會用到
foreach為tp的標簽,遍歷數(shù)組用的,name對應數(shù)據(jù)源,item是循環(huán)變量
編輯edit是寫在js里的方法,根據(jù)id編輯內(nèi)容
刪除del是寫在js里的方法,根據(jù)id刪除內(nèi)容
第四步:完成前三步,首頁的信息肯定可以顯示出來了,但是點擊頁碼沒有反應,這時候,我們需要在js里面請求新的數(shù)據(jù)
01
02????//點擊頁碼,重新請求數(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)成對象類型
13????????????//var?data?=?eval('('+content+')');??//強制將json轉(zhuǎn)成對象類型
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????????????//重新輸出整個表格
31????????????$('#neirong').html(l);
32????????});
33????}
34?
35????//點擊編輯按鈕
36????functionedit(id)?{
37????????window.location.href='__APP__/home/product/edit/id/'+id;
38????}
39?
40????//點擊刪除按鈕
41????functiondel(id)?{
42????????if(confirm("您確定要刪除么!")){
43????????????window.location.href='__APP__/home/product/delete/id/'+id;
44????????}
45????}
46
解釋下關鍵代碼:
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+')'); //強制將json轉(zhuǎn)成對象類型這里是注釋,沒錯.建議在這步alert一下content,看看有沒有數(shù)據(jù),沒有就打開這段代碼試試.
固定鏈接:http://www.tcode.me/article/751.html來自淘代碼轉(zhuǎn)載請注明