dsj0312 2018-01-30 21:18
浏览 30

添加按钮以在发送到查看页面之前执行操作

I'm working on a program that is a reservation service for lab systems using Yii2.0. I've been using Yii for a little while but for some reason this one is stumping me

I have all of the systems listed in a gridview and I'd like to have a button in the actionColumn that will run the 'reserve' action and then show the view for the individual system.

I have the button added and it brings the user to the view page but I don't know what I need to do to have it run the reserve action first...or if it's even possible. I've tried pointing it to the reserve action in the controller but of course that looks for a view page rather than an action.

Here's some of the code from what I've been trying after looking through many pages of suggestions:

On the index page

['class' => 'yii\grid\ActionColumn',
                'template' => '{reserve}',
                'buttons' => [
                        'reserve' => function ($url, $model) {
                        return Html::a('<span class="glyphicon glyphicon-ok-circle"></span>',
                                $url,
                                [
                                        'title' => 'Reserve',
                                        'data-method' => 'post',
                                        'data-pjax' => 0,
                        ]);
                        }                   
                ],

        'urlCreator' => function ($action, $model, $key, $index) {
            return Url::toRoute(['cml/view', 'id' => $key]);
        }

Function in Controller

    public function actionReserve($id)
{
    $model = $this->findModel($id);

    if ($model->fkReservedTo == 1)
    {
        $model->fkReservedTo = Yii::$app->user->id;
        // shouldn't you call save here
        $model->save();
        return $this->redirect(['view','id'=>$id]);
    }
    else 
    {
        Yii::$app->session->setFlash('error', 'This system is not available to be reserved');
        return $this->showAlert();
    }
}

Any suggestions would be appreciated.

  • 写回答

1条回答 默认 最新

  • douquan9826 2018-01-31 10:43
    关注

    Change

    return Url::toRoute(['cml/view', 'id' => $key]);
    

    to

    return Url::toRoute(['cml/reserve', 'id' => $key]);
    

    in your urlCreator function.

    评论

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题