Thinkphp5為某資料表提供額外的排序功能

語言: CN / TW / HK

在顯示資料列表時,一般我們只是簡單的將資料進行展示,並不需要其它額外的操作。如果我們需要對資料再進行排序,可參考如下程式碼:

通過修改排序欄位,就可修改資料的顯示位置。

一、前臺lst.html程式碼

                                  <form action="" method="post"><!--在原先列表基礎上,額外新增程式碼-->
                                            <table class="table table-bordered table-hover">
                                                <thead class="">
                                                    <tr>
                                                        <th class="text-center" width=10%>ID</th>
                                                        <th class="text-center" width=10%>排序</th><!--新新增的欄位sort-->
                                                        <th>欄目名稱</th>
                                                        <th class="text-center">欄目型別</th>
                                                        <th class="text-center" width=20%>操作</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    {volist name="list" id="vo"}
                                                    <tr>
                                                        <td align="center">{$vo.id}</td>
                                                        <td align="center"><input type="text" name="{$vo.id}"
                                                                value="{$vo.sort}" <!--排序欄位的賦值很特殊name為當前每一條資料的id,值為資料庫中排序欄位的值-->
                                                                style="width:50px;text-align:center;" /></td>
                                                        <td>{neq name="vo.level" value="0"}|{/neq}
                                                            <?php echo str_repeat('-',$vo['level']*4) ?> {$vo.catename}
                                                        </td>
                                                        <td align="center">{eq name="vo.type" value="1"}列表{else/}單頁{/eq}
                                                        </td>
                                                        <td align="center">
                                                            <a href="{:url('cate/edit',array('id'=>$vo['id']))}"
                                                                class="btn btn-primary btn-sm shiny">
                                                                <i class="fa fa-edit"></i> 編輯
                                                            </a>
                                                            <a href="#"
                                                                onClick="warning('確實要刪除嗎', '{:url('cate/delete',array('id'=>$vo['id']))}')"
                                                                class="btn btn-danger btn-sm shiny">
                                                                <i class="fa fa-trash-o"></i> 刪除
                                                            </a>
                                                        </td>
                                                    </tr>
                                                    {/volist}
                                                </tbody>
                                            </table>
                                            <div><input class="btn btn-primary btn-sm shiny" type="submit" value="排序"
                                                    style="margin-left:160px;margin-top:10px;" /></div>
                                        </form><!--在原先列表基礎上,額外新增程式碼-->

二、後臺實現程式碼:Cate.php Controller中的程式碼:

  /***
     * 列表
     */
    public function lst(){
        $cate=new CateModel();
        if(request()->isPost()){
            $data=input('post.');
            foreach($data as $k=>$v){
                $cate->update(['id'=>$k,'sort'=>$v]);
            }
            $this->success("欄目排序更新成功!");
        }
        $list=$cate->catetree();//這個方法和以往有所區別,多加了一個sort排序功能
        $this->assign('list',$list);
        return $this->fetch();
    }
三、Cate.php Model層的變化:
 /**
         * 取得所有無限級欄目資訊並排序
         */
 
        public function catetree(){
            $cateres=db('cate')->order('sort desc')->select();
            return $this->sort($cateres);
 
        }
        public function sort($data,$pid=0,$level=0){
            static $arr=array();
            foreach($data as $k=>$v){
                if($v['pid']==$pid){//找到第一個頂級欄目
                    $v['level']=$level;
                    $arr[]=$v;
                    $this->sort($data,$v['id'],$level+1);
                }
            }
            return $arr;
        }