删除不工作方法不允许(#405)gridview yii2

时间:2015-09-02 14:09:35

标签: jquery yii2 yii2-advanced-app yii2-basic-app

这是我的gridview,我从班级actionColumn更改为:

[   'format' => 'html',
            'contentOptions'=>['style'=>'width: 5px;'],
            'value' => function($model) {
                if($model->id == Yii::$app->user->identity->id) {
                    return  Html::a('<i class="glyphicon glyphicon-share-alt"></i>').' '. 
                            Html::a('<i class="glyphicon glyphicon-pencil"></i>', ['update', 'id' => $model->id]).' '.
                            Html::a('<i class="glyphicon glyphicon-trash"></i>', ['delete', 'id' => $model->id], ['data' => ['confirm' => 'Do you really want to delete this element?','method' => 'post']]);
                }
                return '';
            },
        ],

这给了我一个错误。

Method Not Allowed (#405)

Method Not Allowed. This url can only handle the following request methods: POST. 

当我再次更改为actionColumn时,它正常工作,但我更改了代码,它只是给了我一个错误。

2 个答案:

答案 0 :(得分:3)

由于html格式化程序将使用HtmlPurifier 清除值,因此您只需将格式更改为raw

了解详情:http://www.yiiframework.com/doc-2.0/guide-output-formatter.html#other-formatters

答案 1 :(得分:2)

你还可以做的是设置actioncoloumn中的按钮参数http://www.yiiframework.com/doc-2.0/yii-grid-actioncolumn.html# $ buttons-detail

e.g。像这样的东西:

      'buttons' => [
            'update' => function ($url, $model, $key) {
                if ($model->id == Yii::$app->user->identity->id) {
                    $options = [
                    'title' => Yii::t('yii', 'Update'),
                    'aria-label' => Yii::t('yii', 'Update'),
                    'data-pjax' => '0',
                    ];
                    return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url, $options);
                }
            },
            'delete' => function ($url, $model, $key) {
                if ($model->id == 6929) {
                    $options = [
                    'title' => Yii::t('yii', 'Delete'),
                    'aria-label' => Yii::t('yii', 'Delete'),
                    'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
                    'data-method' => 'post',
                    'data-pjax' => '0',
                    ];
                    return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, $options);
                }
            },
        ],

但我只是从ActionColumn类扩展我自己的类,创建一个这样的函数。我理解您的代码应该隐藏或显示所有按钮,具体取决于model-&gt; id是否为user-&gt; identity-&gt; id

protected function renderDataCellContent($model, $key, $index)
{
    if ($model->id == Yii::$app->user->identity->id) {
        return parent::renderDataCellContent($model, $key, $index);
    }
}

希望这会有所帮助。 我将使用扩展的Actioncolumn类的方法。因为那时所有链接仍然有效,例如urlCreator功能已更改,或者为网格等启用了pjax。

尽管如此,邮件请求不起作用的原因是正确的,因为soju已经在上面写了。