yii2中的动作列

问题描述 投票:0回答:1

ActionColumnview, update, delete 默认的。

我想添加一个按钮 "制作" 记过,( 我在db中有一列叫 状况 所以我想用一个函数来实现这个逻辑,把任务标记为已完成,谁能帮我解决这个问题?

这个例子我在论坛上看到的,但我不太明白。

[
  'class' => 'yii\grid\ActionColumn',
  'template' => '{view} {update} {delete} {made}',
  'buttons'=> [
    'made' => function () {     
      return Html::button('<span class="glyphicon glyphicon-ok"></span>', [
        'title' => Yii::t('yii', 'made'),
      ]);                                
    }
  ],
php yii yii2 php-7
1个回答
1
投票

你可以这样做。

[
  'class' => 'yii\grid\ActionColumn',
  'template' => '{view} {update} {delete} {made}',
  'buttons'=> [
    ...
    'made' => function ($url, $model) {
       if($model->status === $model::STATUS_SUSPENDED){
          return Html::a("Activate", $url, [
              'title' => "Activate",
              'class' => 'btn btn-xs btn-success',
              'data' => [
                   'method' => 'post',
                   'confirm' => 'Are you sure? This will Activate this.',
              ],
          ]);
       }
       return Html::a("Suspend", $url, [
          'title' => "Suspend",
          'class' => 'btn btn-xs btn-danger',
          'data' => [
              'method' => 'post',
              'confirm' => 'Are you sure? This will Suspend this.',
          ],
       ]);
     }
  ],
]

在你的控制器中创建一个方法 actionMade() 在那里你检查 post 请求,并对指定的 id. 希望这能帮助你。

© www.soinside.com 2019 - 2024. All rights reserved.