模型类modules(CountrySearch -> search())

## 模型类modules(CountrySearch -> search())


public function search($params)
{
    $query = Country::find();
    // add conditions that should always apply here

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
        'pagination' => [
            'pageSize' => 2, //每页显示条数
        ],
    ]);

    $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([
        'population' => $this->population,
    ]);

    $query->andFilterWhere(['like', 'code', $this->code])
        ->andFilterWhere(['like', 'name', $this->name]);

    return $dataProvider;
}

###控制器类controllers ->Index()


namespace app\controllers;

use Yii;
use app\models\Country;
use app\models\CountrySearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
public function actionIndex()
{
    $searchModel = new CountrySearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
    //echo "
";
    //var_dump($dataProvider['countries']);
    //exit();
    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}


###视图views(country->index)



title = 'Countries';
$this->params['breadcrumbs'][] = $this->title;
?>
   

title) ?>

    render('_search', ['model' => $searchModel]); ?>    

        'btn btn-success']) ?>    

    $dataProvider,         'filterModel' => $searchModel,         //默认layout的表格三部分可不写:几条简介,表格,分页;可以去掉任意部分         'layout' => "{summary}\n{items}\n{pager}" ,         //没有数据时候显示的内容和html样式         'emptyText'=>'当前没有内容',         'emptyTextOptions'=>['style'=>'color:red;font-weight:bold'],         //显示底部(就是多了一栏),默认是关闭的         'showFooter'=>true,         'columns' => [             ['class' => 'yii\grid\SerialColumn'],             'code',             'name',             'population',             ['class' => 'yii\grid\ActionColumn'],         ],     ]); ?>    
分页结束
        content that may contain 'ssad                 'date',             'language' => 'zh-CN',             'dateFormat' => 'php:Y-m-d',         ]) ?>        
sd

模型类modules(CountrySearch -> search())_第1张图片

你可能感兴趣的:(php)