模型类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 "<pre/>";
    //var_dump($dataProvider['countries']);
    //exit();
    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}


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



<?php

use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\ActiveForm;
use app\components\HelloWidget;
use yii\base\Component;
use yii\jui\DatePicker;
use yii\widgets\LinkPager;

/* @var $this yii\web\View */
/* @var $searchModel app\models\CountrySearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Countries';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="country-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <p>
        <?= Html::a('Create Country', ['create'], ['class' => 'btn btn-success']) ?>
    </p>

    <?= GridView::widget([
        'dataProvider' => $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'],

        ],
    ]); ?>

    <div>分页结束</div>
    <?php HelloWidget::begin(); ?>

    content that may contain <tag>'ssad

        <?php HelloWidget::end(); ?>
        <?= DatePicker::widget([
            'name' => 'date',
            'language' => 'zh-CN',
            'dateFormat' => 'php:Y-m-d',
        ]) ?>
        <div>sd</div>
</div>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值