Mass action YII checkbox cgridview

Hello

I would like to extend cgridview funcitonality that should:

  • show a list of query results in tabular format (eg, multiple data columns)

  • the leftmost column should be reserved for a checkbox

  • an action button should reside below the table that executes a CRUD action such as ‘delete’ or ‘update’ or ‘insert’

  • when clicked the same action should apply to each record for which the checkbox is checked

A similar example can be found at http://www.eha.ee/labs/yiiplay/index.php/en/person/batch

Could any one kindly help me in providing a code snippet that does the functionality that i am in need of?

Thank you.


// This is how I added delete all button with gridview checkboxes


<?php

$this->breadcrumbs=array(

	//'Campaigns'=>array('index'),

	//'Manage',

);


$this->menu=array(

	//array('label'=>'List Campaign', 'url'=>array('index')),

	//array('label'=>'New Campaign', 'url'=>array('create')),

);


Yii::app()->clientScript->registerScript('search', "

$('.search-button').click(function(){

	$('.search-form').toggle();

	return false;

});

$('.search-form form').submit(function(){

	$.fn.yiiGridView.update('campaign-grid', {

		data: $(this).serialize()

	});

	return false;

});

$('.deleteall-button').click(function(){

	

	var atLeastOneIsChecked = $('input[name=\"campaign-grid_c0[]\"]:checked').length > 0;


	if (!atLeastOneIsChecked)

	{

		alert('Please select atleast one Campaign to delete');

	}

	else if (window.confirm('Are you sure you want to delete the Campaign?'))

	{

		document.getElementById('campaign-search-form').action='index.php?r=campaign/deleteall';

		document.getElementById('campaign-search-form').submit();

	}

});

");


?>


<h2>Search Campaigns</h2>


<!--<p>

You may optionally enter a comparison operator (<b><</b>, <b><=</b>, <b>></b>, <b>>=</b>, <b><></b>

or <b>=</b>) at the beginning of each of your search values to specify how the comparison should be done.

</p>-->


<?php 

//echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); 

?>

<div class="search-form" style="display:block">

<?php 

$this->renderPartial('_search',array('model'=>$model)); 

?>

</div>

<!-- search-form -->


<div class="wide form">


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'campaign-search-form',

	'enableAjaxValidation'=>false,

	'htmlOptions'=>array('enctype' => 'multipart/form-data')

));

?>

<?php $this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'campaign-grid',

	'selectableRows'=>2,

	'dataProvider'=>$model->search(),

	'columns'=>array(

	

			array(

		'value'=>'$data->camp_id',

        'class'=>'CCheckBoxColumn',

      

		),

		'camp_id',

		'name',

		array('name'=>'start_time','value'=>'date(\'Y-m-d H:i:s\', $data->start_time)'),

		array('name'=>'end_time','value'=>'date(\'Y-m-d H:i:s\', $data->end_time)'),

		'manager',

		array('name' => 'priority','value'=>'$data->mPriorityOptions[$data->priority]'),

		array('name' => 'mode','value'=>'$data->mModeOptions[$data->mode]'),		

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>


<div class="row buttons">

	<?php echo CHtml::button('Delete',array('name'=>'btndeleteall','class'=>'deleteall-button')); ?>

</div>

<?php $this->endWidget(); ?>

</div>



Hi Aruna,

Thanks for your response. Your example actually does action for all the rows but not for selected rows on cgridview.

Hi,

Even I named it as deleteall but it does for selected rows.You can delete single selected row as well.

Below is deleteall controller


public function actionDeleteAll()

{

	if (isset($_POST['campaign-grid_c0']))

	{

		$del_camps = $_POST['campaign-grid_c0'];

		

		$model_camp=new Campaign;

		$model_receipiant = new Recipiants;

		

		foreach ($del_camps as $_camp_id)

		{

			$model_camp->deleteByPk($_camp_id);

			$model_receipiant->deleteAllByAttributes(array('camp_id'=>$_camp_id));

		}

					

		$this->actionAdmin();

	}

	else

	{

		Yii::app()->user->setFlash('error', 'Please select at least one record to delete.');

		$this->actionAdmin();

	}		

}



Thanks Again!!