yii GridView 常见操作

是否显示某列

代码实现如下:

[
    "attribute" => "name",`这里写代码片`
    "visible" => true,
],

//自定义值

[
    "attribute" => "create_time",
    "value" => function ($model) {
        return date("Y-m-d H:i:s", $model->create_time);
    },
],

显示图片

[
    "label" => "封面图",
    "format" => [
        "image", 
        [
            "width"=>"100",
            "height"=>"100"
        ]
    ],
    "value" => function ($model) { 
        return $model->image; 
    }
],

html渲染, 不做Html::encode()处理;

[
    "attribute" => "title",
    "value" => function ($model) { 
        return $model->content; 
    },
    "format" => "raw",
],
自定义按钮案例//$url 为Grid类拼接当前控制器名称/按钮模版名称/当前行模型id	controllerName/buttons-template-name/$model->id

[
    "class" => "yii\grid\ActionColumn",
    "template" => "{buttons-template-name} {view} {update}",
    "header" => "查看 修改 删除 其他",
    "buttons" => [
        "buttons-template-name" => function ($url, $model, $key) { 
            return Html::a("text-contenx", $url, ["title" => "xyz"] ); 
        },
    ],
],

设定宽度

[
    "attribute" => "title",
    "value" => "title",
    "headerOptions" => ["width" => "100"],
],

只需要指定配置项headerOptions即可。

自定义字段

[
    "attribute" => "自定义字段",
    "value" => function ($model) {
        return $model->xyz;
    }
],

样式

<?= GridView::widget([
    "dataProvider" => $dataProvider,
    "rowOptions" => function($model, $key, $index, $grid) {
        return ["class" => $index % 2 ==0 ? "label-red" : "label-green"];
    },
]); ?>

增加按钮调用js

[
    "class" => "yii\grid\ActionColumn",
    "header" => "修改",
    "template" => "{view} {update} {edit-isHot}",
    "buttons" => [
        "edit-isHot" => function ($url, $model, $key) {
            return Html::a("更新状态", "javascript:;", ["onclick"=>toggle_isHot(this, ".$model->id.");"]); },
    ],
],

禁止表头排序

$dataProvider = new ActiveDataProvider([ "query" => $query, ]); 
$dataProvider->setSort(false);

表格表头

echo GridView::widget([
    //......
    'caption' => '内容管理',
    'captionOptions' => ['style' => 'font-size: 16px; font-weight: bold; color: #000; text-align: center;'],

]);

字段内容换行

[
    'attribute' => 'api_url',
    'contentOptions' => ['style' => 'white-space: normal;', 'width' => '20%'],
],
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值