5

这是我的网格视图,我从类更改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再次更改为它时它正在工作,但是我更改了代码,它只是给了我一个错误。

4

2 回答 2

4

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

阅读更多:http ://www.yiiframework.com/doc-2.0/guide-output-formatter.html#other-formatters

于 2015-09-02T15:15:50.857 回答
2

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

例如这样的:

      '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 类扩展我自己的类,创建一个这样的函数。我了解您的代码,所有按钮都应隐藏或显示,具体取决于模型-> id 是否为用户-> 身份-> id

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

希望这可以帮助。我会将该方法与扩展的 Actioncolumn 类一起使用。因为如果更改了 urlCreator 函数或为网格启用了 pjax 等,所有链接仍然有效。

尽管如此,发布请求不起作用的原因是正确的,正如上面烧酒所写的那样。

于 2015-09-02T19:34:27.683 回答