how to delete items in CGridView(my example)

HI, yii CGridView widget is powerful, but have something to be stronger.

I have tested mutiply items deleting in it.

in controller:




//add jquery.js, can be place these code in actionAdmin():

	public function actionAdmin()

	{

		$model=new Post('search'); 

		$model->unsetAttributes();  // clear any default values

		$clientScript = Yii::app()->clientScript;

		$clientScript->packages = array(

                  'jquery'=>array(

                    'basePath'=>'application.assets.jquery',

                    'baseUrl'=>'/assets/jquery',

                    'js'=>array('jquery-1.5.1.min.js'),

                  ),

                  'xheditor'=>array(

                    'basePath'=>'application.assets.jquery.xheditor-1.1.5',

                    'baseUrl'=>'/assets/jquery/xheditor-1.1.5',

                    'js'=>array('xheditor-en.min.js'),

                  ),

		);

		if(isset($_GET['Post']))

			$model->attributes=$_GET['Post'];

		$this->render('admin',array(

			'model'=>$model,

		));

	}


//delete items

	public function actionDeleteItems(){

		$model = new Post();

		$items = explode(',', rtrim($_POST['itemGroup'], ','));

		$num = 0;

		if (NULL != items){

			foreach($items as $item){

				$sql = 'DELETE from tbl_post WHERE id = :id';

				$command = Yii::app()->db->createCommand($sql);

				$command->bindValue(':id', $item);

				$return = $command->execute();

				if ($return == 1) $num++;

			}

		}

		echo $num;

	}



in view:

//register jquery (or other javascript code) and set baseUrl which can be pass to javascript:




$jquery = Yii::app()->clientScript->registerPackage('jquery');

Yii::app()->clientScript->registerScript('helpers', 'baseUrl = "' . Yii::app()->request->hostInfo . '/index.php?r=";', 1);

?>



edit CGridVIew:




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

	'id'=>'post-grid',

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

	'filter'=>$model,

	'columns'=>array(

		array(

			'name' => 'id',

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

			'selectableRows' => '2 or more', //notice

			'class' => 'CCheckBoxColumn',

		),

		array(

			'name' => 'title',

			'type' => 'raw',

			'value' => 'CHtml::link(CHtml::encode($data->title), $data->url)'

		),



//add a button:




<?php echo CHtml::submitButton('delete', array('id' => 'deleteSelected'));?>



//javascript code (jquey)




<script type="text/javascript">

<!--

$(function(){

	$("#deleteSelected").bind("click", function(){

		var itemGroup = "";

		var num = 0;

		$("input[type=checkbox]:checked").each(function(){

			itemGroup = itemGroup +  $(this).val() +  ",";

			num++;

		});


		if (itemGroup.length <= 0){

			alert("Did you select your item(s)?");

			return false;

		}


		$.post( baseUrl + 'post/deleteItems', {itemGroup: itemGroup}, function(msg){

			if (msg == num){

				alert("delete successful!");

				$("input[type=checkbox]:checked").each(function(){

					$(this).parent("td").parent("tr").remove();

				});

			}else{

				alert("delete can not be execute!");

			}

		});

	});

});

//-->

</script>



Thanks for your example… it help me a lot… instead of delete, i try to insert data to database… so i have to make several changes in your coding… Thanks god, it work find… thanks again… :)

NOTE_ moved to proper sub-forum (Tips, Snippets, Tutrials instead of General Discussion)