我现在只是想绕着yii框架,我创建了一个删除多个数据库记录的功能。在视图中,我有一个网格,其中包含对每个项目的引用以及旁边的复选框
<?php $this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'htmlOptions'=>array(
'class'=>''
),
'columns'=>array(
...
array(
'class'=>'CLinkColumn',
'header'=>'Handle',
'labelExpression'=>'$data->handle',
'urlExpression'=>'Yii::app()->createUrl(".../item/view", array("id"=>$data->id))',
),
array(
'class'=>'CCheckBoxColumn',
'header'=>'Select',
'selectableRows'=>'2',
),
),
然后在页面的下方我想要一个按钮(删除选中),它将所有项目的数组发送到删除功能。我的想法是类似于这个:
<a href="
<?php echo Yii::app()->createUrl('.../item/bulkDelete','array("id" => $data->id)')?>"
class="btn">Delete Selected</a>
但我不明白如何获取每个已检查项目的参考,而不是:
"id" => $data->id
因为我以前调用将项目传递给视图函数。如果有人能提供帮助,将不胜感激。
修改
查看:
$form = $this->beginWidget('CActiveForm', array(
'id' => 'itemForm',
'action' => array('.../item/bulkDelete'),
));
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'htmlOptions'=>array(
'class'=>''
),
array(
'class'=>'CLinkColumn',
'header'=>'Handle',
'labelExpression'=>'$data->handle',
'urlExpression'=>'Yii::app()->createUrl(".../item/view", array("id"=>$data->id))',
),
array(
'class'=>'CCheckBoxColumn',
'header'=>'Select',
'selectableRows'=>'2',
),
... //More Columns
), //End of Grid
...
echo CHtml::SubmitButton('Delete Multiple');
$this->endWidget();
控制器:
public function actionBulkDelete()
{
if(isset($_POST['id'])&& !empty($_POST['id']))
{
Yii::app()->user->setFlash('success', 'Delete Items');
$this->redirect(array('.../item/index'));
}
else
{
Yii::app()->user->setFlash('success', 'No Items Selected');
$this->redirect(array('.../item/index'));
}
}
答案 0 :(得分:1)
在表单中包装网格视图,并在columns
数组中执行以下更改:
$form = $this->beginWidget('CActiveForm', array(
'id' => 'some-grid-form',
'action' => array('myController/myAction'),
));
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'selectableRows' => 2,
'columns'=>array(
array(
'id' => 'id',
'class' => 'CCheckBoxColumn',
),
//... rest of your columns
),
array(
'class'=>'CButtonColumn',
),
),
));
echo CHtml::SubmitButton('Multiple Delete');
$this->endWidget(); // end form
在你的控制器myAction
中:
public function actionmyAction()
{
if(isset($_POST['id']) && !empty($_POST['id'])) { //you'll get all the ids in an array
//print_r($_POST['id']);
//your delete function here, also add a few validation here to authenticate deletion
$ids = $_POST['id'];
$criteria = new CDbCriteria;
$criteria->addInCondition('id',$ids);
MyModel::model()->deleteAll($criteria);
//.... render with success flash message etc.
}
}