Yii2.0 GridView

GridView 示例代码

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
//             ['class' => 'yii\grid\SerialColumn'],

            'au_id',
            'au_displayName',
            [
            'attribute'=>'au_package_id',
            'value'=>function ($data){ return $data->getPackageName();},
            'filter'=>Package::getPackageList()
            ],

            ...

            [
                'class' => 'yii\grid\ActionColumn', 
                'header' => '操作',
                'template' => '{view} {update} {delete} {changeDate}', 
                'buttons'=>[
                     'changeDate' => function ($url, $model, $key) {
                        if (Yii::$app->user->identity->u_id > 10) {
                            return '';
                        }//only show for admin user
                         return Html::a('<i class="glyphicon glyphicon-time"></i>', ['app-user/update-date', 'id'=>$model->au_id], ['title'=>'Change Time', 'target'=>'_blank']);
                     },

                ]
            ],
        ],
    ]); ?>
</div>

使用Ajax操作Gridview数据表格

[
    'class' => 'yii\grid\ActionColumn',
    'header'=>'操作',
    'template'=>'{view} {upDown}',
    'buttons'=>[
        'upDown' => function ($url, $model, $key) {
            $label = '显示';
            if ($model->uc_actived == CommonConst::COMSTATUS_YES) {
                $label = '隐藏';
            }
            return Html::a("[$label]", ['#'], ['title'=>$label, 'onclick'=>"
                    var that = $(this);
                    
                    $.ajax({
                        type     :'GET',
                        cache    : false,
                        url  : 'set-up',
                        data  : {id: {$model->uc_id}},
                        success  : function(response) {
                            // 改变Html文字内容
                            if(that.html() == '[显示]'){
                                that.html('[隐藏]')
                            }else{
                                that.html('[显示]')
                            }
                        }
                   })
    
                    // 取消Html link事件响应
                    return false;
    
            "]);
        },
    ]
],

GridView中实现断行断字

// 注意看代码中的 options 和 contentOptions

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'options' => [
                'style'=>'overflow: auto; word-wrap: break-word;'
        ],
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

// ....

            [
                'attribute'=>'pi_desc',
                'value'=>function ($model) { return $model->pi_desc; },
                'format'=>'raw',
                'label'=>'Desc',
                'contentOptions' => ['style'=>'max-width:300px;'],
            ],

Gridview 内容水平垂直居中

[
    'attribute'=>'yiilib',
    'value'=>function ($model) { return $model->yii.'<br/>'.$model->lib; },
    'filter'=>false,
    'format'=>'raw',
    'label'=>'YiiLib.com',
    'headerOptions' => ['width' => '300px'],
    'contentOptions' => ['style'=>'padding:0px 0px 0px 30px;vertical-align: middle;'],
],

下拉菜单实现

Model层:

public static function dropDown ($column,$value = null)
{

	$dropDownList = [
		'is_status'=>['0'=>'显示','1'=>'删除'],
		'is_type'  =>['0'=>'大','1'=>'小'],
		//...
	];

	//根据具体值显示对应的值
	if($value!== null){
		return
		array_key_exists($column,$dropDownList)? $dropDownList[$column][$value]: false;
	//返回关联数组,用户下拉的filter实现
	}
	else{
		return
		array_key_exists($column,$dropDownList)? $dropDownList[$column]: false;
	}
}

视图层:

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        // ['class' => 'yii\grid\SerialColumn'],
        ['attribute'=>'user_id','contentOptions'=>['width'=>'30px']],
        'number',
        // 类型
        [
        'attribute' => 'type',
// 此处调用-------
        'value' => function ($model) {
            return Book::dropDown('is_type', $model->type);
        },
        'filter' => Book::dropDown('is_type'),
        ],
        // 状态
        [
        'attribute' => 'status',
        'value' => function ($model) {
            return Book::dropDown('is_status', $model->status);
        },
        'filter' => Book::dropDown('is_status'),
        ],
        [
            'attribute' => 'account',
            'value' => function ($model) {
                return ($model->account)/100;
            },
        ],
        [
        'class' => 'yii\grid\ActionColumn',
        'template' => Helper::filterActionColumn('{view}{delete}'),
        ],
    ],
]); ?>



导航