fastadmin框架常用知識點

背景:

 最近在維護一個由fastadmin框架搭建的網站,第一次接觸fastadmin,在維護的過程中遇到了一些大大小小的問題,記錄下來,以供其他初學者參考;

知識點:

1. 列表頁指定字段取消篩選操作?

    答:在指定的js文本中,添加屬性 operate:false

 {field: 'ceshi', title: __('Ceshi'), operate:false},

2. 列表頁篩選區域的select賦初始默認值?

   答: 在指定的js文本中,添加屬性 defaultValue:1

 {field: 'ceshi', title: __('Ceshi'),searchList: {"1": __('yes'), "2": __('no')},defaultValue:1},

3. 列表頁指定字段不在表格中顯示,但是在篩選框中能夠顯示並篩選?

   答:在指定的js文本中,添加屬性 visible:false

 {field: 'ceshi', title: __('Ceshi'),visible:false},

4. 根據條件顯示或隱藏按鈕?

    答:在指定的js文本中,添加方法 hidden visible

  {field: 'ceshi', title: __('Ceshi'), operate:false, table: table,formatter: Table.api.formatter.buttons,buttons:
                                [
                                    {
                                        name: 'open', text: '未開通',  title: '去開通',
                                        classname: 'btn btn-xs btn-primary btn-dialog', url: function(row, column){
                                            return "user/test/open?id="+row.id+"&name="+row.name;
                                        },
                                        hidden:function(row){
                                            if(row.status!= 1){
                                                return true;
                                            }
                                        },
                                        visible:function(row){
                                            if(row.cicada == 0 && row.ishas== 2){
                                                return true;
                                            }
                                        },
                                    }]}

5. 自定義按鈕添加角色權限

    答: 在列表頁index.html的操作中經常會添加一些自定義按鈕,如問題4內的按鈕代碼;總後臺權限管理中的角色管理,不同角色展示不同內容。此時,需要修改列表頁index.html,添加  data-operate-open="{:$auth->check('user/test/open')}"

<div class="panel panel-default panel-intro">
    {:build_heading()}

    <div class="panel-body">
        <div id="myTabContent" class="tab-content">
            <div class="tab-pane fade active in" id="one">
                <div class="widget-body no-padding">
                    <div id="toolbar" class="toolbar">
                        {:build_toolbar('refresh')}
                    </div>
                    <table id="table" class="table table-striped table-bordered table-hover"
                           data-operate-detail="{:$auth->check('user/test/detail')}"
                           data-operate-edit="{:$auth->check('user/test/edit')}"
                           data-operate-open="{:$auth->check('user/test/open')}"
                           width="100%">
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

6. 校驗表單input格式

    要求必填,且長度範圍在6-32位;其它校驗可參考官方文檔;表單驗證

   <div class="col-xs-12 col-sm-8">
      <input id="c-account" data-rule="required;length(6~32);" class="form-control" name="row[account]" type="text" placeholder="請輸入您的賬號" >
   </div>

7. 工具欄‘添加按鈕’傳遞參數

  需求:根據傳的參數id值,添加頁面展示不同的內容

 答:1. location.search爲當前頁面的url參數; 在js中 add_url 最後面添加即可;

  add_url: 'edu/user/add'+location.search,

        2. 在控制器中的add方法get請求可以接收參數

$id = $this->request->param('id');

8. selectpage下拉框實現動態調取源數據

 答: data-scource調取數據源地址;

       源數據如果不是id,name 可通過data-primary-key="你的主鍵ID字段"data-field="你要顯示的字段"修改;

      data-params 進行動態傳參;

<div class="col-xs-12 col-sm-8">
   <!--多選-->
   <input id="c-test" data-rule="required" data-multiple="true" data-source="test/abc/passabc" data-pagination="true" data-page-size="10" data-params={"custom[merchantsid]":""}  class="form-control selectpage" name="row[test]" type="text" value="">
 </div>

  對應的js寫方法動態傳參:

add: function () {
      $("#c-test").data("params", function (obj) {
         return {custom: {merchantsid: $("#merchants_id").val()}};
      });
    Controller.api.bindevent();
 },

對應test/abc/passabc的php代碼:

 public function passsabc(){
        $page = $this->request->request("pageNumber");
        $pagesize = $this->request->request("pageSize");
        $request = $this->request->request();
         
         //動態接收參數
        $merchants_id = isset($request['custom']['merchantsid'])?$request['custom']['merchantsid']:'';
        if($merchants_id != ''){
            $where['merchants_id'] = $merchants_id;
        }

        $where['status'] = 2;
        $list = $this->model->field('`shop_id` as id,`shop_name` as name')->where($where)->select();

        //添加搜索功能
        $q_word = $this->request->request("q_word/a");
        if(!empty($q_word[0])){
            $data = [];
            foreach ($list as $item) {
                if(strpos($item['name'], $q_word[0]) !== false){
                    array_push($data,$item);
                }
            }
            $list = $data;
        }

        if($this->request->request("keyValue")){
            $arr = explode(',',$this->request->request("keyValue"));
            $newlist = array();
            foreach ($list as $k=>$v){
                if(in_array($v['id'],$arr)){
                    $newlist[] = $list[$k];
                }
            }
            return ['total'=>count($newlist), 'list'=>$newlist];
        }

        $total = count($list);
        $list = $this->page_array($pagesize,$page,$list,0);
        return json(['list' => $list, 'total' => $total]);
    }

    public function page_array($count,$page,$array,$order){
        global $countpage; #定全局變量
        $page=(empty($page))?'1':$page; #判斷當前頁面是否爲空 如果爲空就表示爲第一頁面
        $start=($page-1)*$count; #計算每次分頁的開始位置
        if($order==1){
            $array=array_reverse($array);
        }
        $totals=count($array);
        $countpage=ceil($totals/$count); #計算總頁面數
        $pagedata=array();
        $pagedata=array_slice($array,$start,$count);
        return $pagedata;  #返回查詢數據
    }

 

注: 後續會逐步補充遇到的問題及解決方法

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章