简体   繁体   中英

Usage of CGridView and CButtonColumn (Yii Framework)

I'm beginner for Yii and trying understand CGridView widget.

First i understand all of these codes :

$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider,
    'columns'=>array(
        "url",
        "allowRedirect",
        array(
            "name"=>"Kod",
            "value"=>array($model, "returnHTTPCode"),
            "type"=>"html"
            ),
        array(
            "class"=>"CButtonColumn",
            "template"=>"{update} {delete}",
            ),

        ),
));

Now i want to change delete button's url to http://domain.com/?r=pano/deleteSite . In documentation , there is a deleteButtonUrl property, i'm trying to use it

$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider,
    'columns'=>array(
        "url",
        "allowRedirect",
        array(
            "name"=>"Kod",
            "value"=>array($model, "returnHTTPCode"),
            "type"=>"html"
            ),
        array(
            "class"=>"CButtonColumn",
            "template"=>"{update} {delete}",
            "deleteButtonUrl"=>"deleteSite"            
            ),

        ),
));

But i got Use of undefined constant deleteSite - assumed 'deleteSite' error.

And then i tried to use buttons property of CButtonColumn.

$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider,
    'columns'=>array(
        "url",
        "allowRedirect",
        array(
            "name"=>"Kod",
            "value"=>array($model, "returnHTTPCode"),
            "type"=>"html"
            ),
        array(
            "class"=>"CButtonColumn",
            "template"=>"{update} {delete}",
            "buttons"=>array(
                "delete"=>array(
                            "url"=>"http://localhost/sitelerimacikmi/index.php?r=panos"
                            )
                ),

            ),

        ),
));

and got same error.

How can i use properties of CButtonColumn?

Note , English isn't my native language, so please use simple grammar, if possible.

You can try to use:

$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
    "url",
    "allowRedirect",
    array(
        "name"=>"Kod",
        "value"=>array($model, "returnHTTPCode"),
        "type"=>"html"
        ),
    array(
        "class"=>"CButtonColumn",
        "template"=>"{update} {delete}",
        "deleteButtonUrl" => "Yii::app()->createUrl('pano/deleteSite')" // <- changes here
        ),

    ),
));

Also, you can read this article: http://www.yiiframework.com/wiki/106/using-cbuttoncolumn-to-customize-buttons-in-cgridview/

If you need to specify a URL then you must specify it as a PHP expression. Here is how is defined default value for delete URL in the CButtonColumn class ,

public $deleteButtonUrl = 'Yii::app()->controller->createUrl(
    "delete",array("id"=>$data->primaryKey)
)';

This url is evaluted using php eval function, in this expression $data is current model being rendered.

You should use:

$this->widget('zii.widgets.grid.CGridView', array(
    ...
    'columns'=>array(
        ...
        array(
            'class'=>'CButtonColumn',
            'deleteButtonUrl' => 'Yii::app()->controller->createUrl(
                "pano/deleteSite", array("id" => $data->primaryKey)
            )',
        ),
    ),
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM