Yii Framework Forum: Delete button in CGrid view in Yii - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Delete button in CGrid view in Yii Rate Topic: -----

#1 User is offline   Mohammed Iqbal Khan 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 14-April 15
  • Location:India

Posted 24 August 2015 - 07:04 AM

I tried to create a Delete button in CButtonColumn,The problem is when I triggered the delete,It shows me an error Your request is invalid..Can anyone tell me why this is happening....
My controller

public function actionDelete($id)
{
$this->loadModel($id)->delete();

// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}

And my view

<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'replay-comment-grid',
'dataProvider'=>$model->search(),
// 'filter'=>$model,
'columns'=>array(
//'id',
//'comment_id',
'admin_replay',
array(
'class' => 'CButtonColumn',
'htmlOptions'=>array('width'=>'180px'),
'template' => '{view}&nbsp;{update}&nbsp;{delete}',
'buttons' => array(
'view'=>array(
'imageUrl' =>false,
'label' => 'View',
'url'=>'Yii::app()->createUrl("replayComment/view/$data->id")',
'options' => array('title'=>'view','class'=>'btn btn-success btn-xs'),
),
'update'=>array(
'imageUrl' =>false,
'label' => '',
'url'=>'Yii::app()->createUrl("replayComment/update/$data->id")',
'options' => array('title'=>'update','class'=>'btn btn-info btn-xs fa fa-pencil-square-o'),
),
'delete'=>array(
'imageUrl' =>false,
'label' => 'delete',
'url'=>'Yii::app()->createUrl("replayComment/delete/$data->id")',
'options' => array('title'=>'delete','class'=>'btn btn-danger btn-xs'),
),
)
),
),
'itemsCssClass'=>'table table-striped table-bordered table-hover',
'pagerCssClass'=>'pagination',
'pager'=>array( 'header' => '','lastPageLabel'=>'<span class="glyphicon glyphicon-chevron-right"></span><span class="glyphicon glyphicon-chevron-right"></span>','firstPageLabel'=>'<span class="glyphicon glyphicon-chevron-left"></span><span class="glyphicon glyphicon-chevron-left"></span>','prevPageLabel'=>'<span class="glyphicon glyphicon-chevron-left"></span>','nextPageLabel'=>'<span class="glyphicon glyphicon-chevron-right"></span>','header' => '','cssFile' => Yii::app()->baseUrl . '/css/pager.css','htmlOptions'=>array('class'=>'pagination'),'selectedPageCssClass'=>'active'),
)); ?>
MIK
0

#2 User is offline   estebanhere30 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 58
  • Joined: 13-November 14
  • Location:Colombia

Posted 24 August 2015 - 09:16 AM

Good morning Mohammed Iqbal Khan, you can try with exceptions in the controller en el action delete, so:

public function actionDelete($id)
	{
		try{
			$this->loadModel($id)->delete();

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_GET['ajax']))
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
		} catch(CDbException $e){
	        if($e->getCode()===23000){
	            //You can have nother error handling
	            header("HTTP/1.0 400 Relation Restriction");
	            echo 'Este registro no puede ser borrado, porque está relacionado con información importante de la base de datos';
	        }else{
	            throw $e;
	        }
		}
	}



or you can quit the buttons oin the view admin, so:

	array(
			'class'=>'CButtonColumn',
			'template'=>'{update}{view}{delete}',
            'buttons'=>array(
                'update'=>array(
                        'visible'=>'false',
                ),
                'view'=>array(
                        'visible'=>'true',
                ),
                'delete'=>array(
                        'visible'=>'false',
				),
			),
		),


I hope to help you
0

#3 User is offline   Yaasky 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 09-March 15
  • Location:Abuja, NG

Posted 10 September 2015 - 04:35 AM

In your routes, make sure you have a rule that allows replayComment/update/$data->id point to replayComment/update/id/$data->id
0

#4 User is offline   ilaiya 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 120
  • Joined: 09-October 14
  • Location:Bangalore, India

Posted 11 September 2015 - 01:51 AM

View PostMohammed Iqbal Khan, on 24 August 2015 - 07:04 AM, said:

I tried to create a Delete button in CButtonColumn,The problem is when I triggered the delete,It shows me an error Your request is invalid..Can anyone tell me why this is happening....


Check your top of the controller, By default delete action are not worked as GET Method, you can use POST method.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users