I wanna delete checked records when i am click the delete button
i am try with below code but not success pls give the solution
i am new to the yii
below is my view file
<?php
/* @var $this UsersController */
/* @var $model Users */
$this->breadcrumbs=array(
'Users'=>array('index'),
'Manage',
);
$this->menu=array(
array('label'=>'List Users', 'url'=>array('index')),
array('label'=>'Create Users', 'url'=>array('create')),
);
Yii::app()->clientScript->registerScript(‘search’, "
$(’.search-button’).click(function(){
$('.search-form').toggle();
return false;
});
$(’.search-form form’).submit(function(){
$('#users-grid').yiiGridView('update', {
data: $(this).serialize()
});
return false;
});
$(’.deleteall-button’).click(function(){
var atLeastOneIsChecked = $('input[name=\"users-grid_c1[]\"]: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('users-form').action='index.php?r=users/DeleteAll';
document.getElementById('users-form').submit();
}
});
");
?>
<h1>Manage Users</h1>
<!–<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:none">
<?php $this->renderPartial(’_search’,array(
'model'=>$model,
)); ?>
</div><!-- search-form -->
<div class="wide form">
<?php $form=$this->beginWidget(‘CActiveForm’, array(
'id'=>'users-form',
'enableAjaxValidation'=>false,
'htmlOptions'=>array('enctype' => 'multipart/form-data')
));
?>
<!-------date ficker---->
<?php $form=$this->beginWidget(‘CActiveForm’, array(
'id'=>'users-form',
'enableAjaxValidation'=>true,
)); ?>
<b>From :</b>
<?php
$this->widget(‘zii.widgets.jui.CJuiDatePicker’, array(
'name'=>'from_date', // name of post parameter
'value'=>Yii::app()->request->cookies['from_date']->value, // value comes from cookie after submittion
'options'=>array(
'showAnim'=>'fold',
'dateFormat'=>'yy-mm-dd',
),
'htmlOptions'=>array(
'style'=>'height:20px;'
),
));
?>
<b>To :</b>
<?php
$this->widget(‘zii.widgets.jui.CJuiDatePicker’, array(
'name'=>'to_date',
'value'=>Yii::app()->request->cookies['to_date']->value,
'options'=>array(
'showAnim'=>'fold',
'dateFormat'=>'yy-mm-dd',
),
'htmlOptions'=>array(
'style'=>'height:20px;'
),
));
?>
<?php echo CHtml::submitButton(‘Go’); ?> // submit button
<?php $this->endWidget(); ?>
<!-------end date ficker--------->
<?php $this->widget(‘zii.widgets.grid.CGridView’, array(
'id'=>'users-grid',
// 'selectableRows'=>2,
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array('header' => '#', 'value' => '($row+1) . "."', 'htmlOptions' => array('style' => 'text-align:right;')),
array(
'value'=>'$data->id',
'class'=>'CCheckBoxColumn',
// 'id'=>'person-id',
'selectableRows' => '50',
),
array(
'name'=>'id',
'value'=>'CHtml::encode($data->id)'
),
array(
'name'=>'username',
'value'=>'CHtml::encode($data->username)'
),
array(
'name'=>'image',
'value'=>'CHtml::encode($data->image)'
),
array(
'name'=>'createtime',
'value'=>'CHtml::encode(date(\'Y-m-d,H:i:s\', $data->createtime))' //this will format the unix timestamp to a custom date format
),
array(
'name'=>'lastvisit',
'value'=>'CHtml::encode(date(\'Y-m-d\', $data->lastvisit))' // format the cell with date format
),
array(
'name'=>'status',
'value'=>'CHtml::encode($data->status==1 ? \'Active\': \'Inactive\' )'
// to render the gridview cell with a particular value
),
array(
'class'=>'CButtonColumn',
//y'afterDelete'=>'function(link,success,data){ if(success) $("#statusMsg").html(data); }',
),
),
'selectionChanged'=>'userClicks',
)); ?>
<div class="row buttons">
<?php echo CHtml::button('Delete',array('name'=>'btndeleteall','class'=>'deleteall-button')); ?>
</div>
<?php $this->endWidget(); ?>
</div>
In controoler:
public function actionDeleteAll()
{
if(Yii::app()->request->isPostRequest)
{
$del_camps = $_POST['users-grid_c1'];
//print_r($del_camps);
//echo $_POST['person-id'];
//exit();
$model_camp=new Users;
//$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.');
//echo 'sssss';
//exit();
$this->actionAdmin();
}
}
pls help me