Yii2 如何在 Gridview 列表中新增自定义按钮?

Wiki · 本文由 · ITer · 发布于 2年前 · 2639 阅读

先贴一下 Gridview 列表尾部按钮的代码:

[
    'header'=>'操作',
    'headerOptions' => ['width' => '8%'],
    'class' => 'yii\grid\ActionColumn',
    'template' => '{update} {delete}',
    'buttons'=>[
        'update' => function ($url, $model, $key){
            return Html::a('编辑', '#', ['class' => 'j-edit', 'data-toggle' => 'modal', 'data-target' => '#page-modal']);
        },
        'delete'=> function ($url, $model, $key){
            return  Html::a('删除', ['delete', 'id'=>$model->id],[
                'data-method'=>'post',
                'data-confirm' => '确定删除该项?',
            ] ) ;
        }
     ],
 ],

添加一个自定义的按钮

[
    'header'=>'操作',
    'headerOptions' => ['width' => '8%'],
    'class' => 'yii\grid\ActionColumn',
    'template' => '{update} {delete} {test}',  //此处添加一个 {test}
    'buttons'=>[
        'update' => function ($url, $model, $key){
            return Html::a('编辑', '#', ['class' => 'j-edit', 'data-toggle' => 'modal', 'data-target' => '#page-modal']);
        },
        'delete'=> function ($url, $model, $key){
            return  Html::a('删除', ['delete', 'id'=>$model->id],[
                'data-method'=>'post',
                'data-confirm' => '确定删除该项?',
            ] ) ;
        }
        //这里对应添加一个test
        'test'=> function ($url, $model, $key){
            return  Html::a('测试', ['delete', 'id'=>$model->id]) ;
        }
     ],
 ],
评论数量:0