specialnot 2015-09-24 10:02:07 6839次浏览 0条评论 6 4 0

单元格返回html代码,并且控制单元格宽度

[
    'attribute'=>'title',
    'label'=>'文章标题',
    'format'=>'html',
    'value'=>function($model){
        return '<b>'.$model->title.'</b>';
    },
    'headerOptions'=>['width'=>'300px'],
],

自定义 按钮

['class' => 'yii\grid\ActionColumn',
    'header'=>'操作',
    'template' => '{add} {view} {update} {delete}',
    'buttons' => [
        'add' => function ($url, $model, $key) {
            return  Html::a('<span class="glyphicon glyphicon-plus></span>', $url, ['title' => '添加'] ) ;
        },
    ],
    'headerOptions' => ['width' => '180']
],

或者


[
   'label'=>'操作',
    'format'=>'raw',
    'value' => function($data){
        $url = "……";
        return Html::a('show', $url, ['title' => 'show']);
    }
],

在filter中生成下拉过滤效果

//在 Category 模型中添加如下函数
public static function get_category(){
    $category = self::find()->all();
    $category_array = ArrayHelper::map($category,'id','name');
    return $category_array;
}
public static function get_category_result($id){
    $category = self::find()->all();
    $category_array = ArrayHelper::map($category,'id','name');
    if(in_array($id,array_keys($category_array))){
        return $category_array[$id];
    }else{
        return '';
    }
}

在 article view 中添加

'filterModel' => $searchModel,
[
    'attribute'=>'category_id',
    'label'=>'文章分类',
    'value'=>function($model){
        return \common\models\Category::get_category_result($model->category_id);
    },
    'filter'=>\common\models\Category::get_category(),
],

格式化日期

[
               'attribute' => 'create_time',
               'format' => ['data', 'Y-m-d H:i:s'],
               'headerOptions' => ['width' => '200']
         ]],
觉得很赞
    没有找到数据。
您需要登录后才可以评论。登录 | 立即注册