本文實例講述了thinkphp框架無限級欄目的排序功能實現方法。分享給大家供大家參考,具體如下:
題目中我們并沒有說明是tp5的無限級排序還是tp3的無限級排序就是為了讓小新手們明白,這些功能的實現跟你使用的框架是沒有關系的,不管你是tp5還是tp3還是laravel還是yii框架都沒有關系,我們強調的是思路,是解決問題的方法,演示的時候因為我在用tp3所以無所謂了。
無限級欄目的排序非常簡單,這次以博文的方式分享給大家解決的思路。
上圖:
上圖是我們實現的無限級分類,我們要注意兩個字段,id和排序sort字段,目前sort字段的值都是50,是默認值。接著為大家截圖數據表結構
上圖sort用來實現排序pid用來實現無限級分類
實現無限級分類的關鍵是我們對排序字段的寫法,我們把整塊代碼拿到,但是用到的只有一行:
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
|
<form method= "post" action= "" > <table class = "table table-bordered table-hover" > <thead class = "" > <tr> <th width= "6%" class = "text-center" >ID</th> <th width= "6%" class = "text-center" >pid</th> <th width= "8%" class = "text-center" >排序</th> <th>欄目名稱</th> <th width= "16%" class = "text-center" >操作</th> </tr> </thead> <tbody> <volist name= "cateRes" id= "cate" > <tr> <td align= "center" >{ $cate .id}</td> <td align= "center" >{ $cate .pid}</td> <td align= "center" > <input type= "text" name= "sort[{$cate.id}]" value= "{$cate.sort}" /></td> <td><?php echo str_repeat ( '-' , $cate [ 'level' ]*8);?>{ $cate .cate_name}</td> <td align= "center" > <a href= "" class = " rel=" external nofollow " btn btn-primary btn-sm shiny" > <i class = "fa fa-edit" ></i> 編輯 </a> <a href= "#" rel= "external nofollow" onClick= "warning('確實要刪除嗎', ”)" class = "btn btn-danger btn-sm shiny" > <i class = "fa fa-trash-o" ></i> 刪除 </a> </td> </tr> </volist> <tr> <td colspan= "4" > <button type= "button" tooltip= "排序" style= "margin-left:225px; width:50px;" class = "btn btn-sm btn-azure btn-addon" >排序</button> </td> </tr> </tbody> </table> </form> |
上面的代碼我們可以看出整個table是用form包裹的,因為我們要提交排序字段,所以需要表單。
我們實現無限極欄目排序的核心代碼:
1
|
<input type= "text" name= "sort[{$cate.id}]" value= "{$cate.sort}" /> |
就是這一句,實際上我們是拼裝了一個sort[]數組,整個數組的每個元素的鍵是當前欄目的id而值是當前欄目的排序的值,這樣我們一旦提交數組就可以根據id修改sort了
完整代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public function lst(){ $cate =D( 'Cate' ); if (IS_POST){ //排序 $data =I( 'sort' ); foreach ( $data as $k => $v ) { $cate ->where( array ( 'id' => $k ))->save([ 'sort' => $v ]); } return ; } $cateRes = $cate ->cateTree(); //無限級分類樹 $this ->assign([ 'cateRes' => $cateRes , ]); $this ->display( 'list' ); } |
希望本文所述對大家基于ThinkPHP框架的PHP程序設計有所幫助。