zoukankan      html  css  js  c++  java
  • yii2的GridView和ActiveDataProvider具体使用

    1.控制器中(以User模块的列表为例):

    第一步:

    use backendmodelsUser;
    use yiidataActiveDataProvider;

    第二步:

    public function actionIndex()
    {
    $model=new User();
    $dataProvider = new ActiveDataProvider([
    'query' => User::find()->orderBy('id'),//此处添加where条件时:'query'=>User::find()->where(['username'=>'lizi']);
    ]);
    return $this->render('index', [
    'model' => $model,
    'dataProvider' => $dataProvider,
    ]);
    }

    2.view文件中:

    第一步:

    use yiihelpersHtml;
    use yiigridGridView;
    use backendmodelsUser;

    第二步:

    <?=
    GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
    'id',
    [
    'attribute' => 'username',
    'content' => function($dataProvider){
    return $dataProvider['username'];
    },
    ],
    'email:email',
    [
    'attribute' => 'created_at',
    'format' => ['date', 'php:Y-m-d H:i:s'],
    ],
    [
    'class' => 'yiigridActionColumn',
    'header' => '操作',
    'template' => '{view}{update}{password}{delete}',
    'headerOptions' => ['width' => '128', 'class' => 'padding-left-5px',],
    'contentOptions' => ['class' => 'padding-left-5px'],
    'buttons' => [
    'password' => function ($url, $model, $key) {
    return Html::a('<span class="glyphicon glyphicon-edit"></span>', $url, [
    'title' => '修改密码',
    'data-method' => 'post',
    'data-pjax' => '0',
    ]);
    },
    ],
    ],
    ],
    ]);
    ?>

    3.运行效果如下图:

     
  • 相关阅读:
    Centos 设置开机进图形界面/终端
    Ubuntu 开机慢(networking.service导致)
    Linux 内核下载地址
    C/C++中内存对齐
    编译器数据模型
    CPU中断
    sql语句
    mysql学习
    Active进阶
    SpringBoot整合ActiveMQ
  • 原文地址:https://www.cnblogs.com/xlz307/p/4187154.html
Copyright © 2011-2022 走看看