filterModel自动搜索机制

来源:互联网 发布:游戏程序员工资多少 编辑:程序博客网 时间:2024/06/11 18:18

这里写图片描述
失去焦点后会自动搜索内容
1、前端实现
GridView中绑定yiiGridView方法,获取Url和Get数据,当input输入框失去焦点的时候,会有相应的方法建立Form表单,能够提交input输入框中的内容,提交地址是原来获取的Url(自己对于json具体实现函数不太理解,只有把答题思路讲出来)
2、后台实现
Controller中:

 public function actionIndex()    {        $searchModel = new CategorySearch();        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);        return $this->render('index', [            'searchModel' => $searchModel,            'dataProvider' => $dataProvider,        ]);    }

实例化CategorySearch类,之后调用search()方法,找到search方法

 public function search($params)    {        $query = Category::find();        // add conditions that should always apply here        $dataProvider = new ActiveDataProvider([            'query' => $query,        ]);        $this->load($params);        if (!$this->validate()) {            // uncomment the following line if you do not want to return any records when validation fails            // $query->where('0=1');            return $dataProvider;        }        // grid filtering conditions        $query->andFilterWhere([            'id' => $this->id,        ]);        $query->andFilterWhere(['like', 'name', $this->name]);        return $dataProvider;    }}

流程:
1、find()数据
2、建立数据模型
3、给模型属性赋值load()
4、验证validate()
5、SQL查询andFilterWhere()
6、返回数据模型

原创粉丝点击