如何处理在Yii中CGridView选定的行
2014-12-14
展开全部
将窗体添加到您的视图文件 这将允许复选框的值,它输入到被张贴到您的控制器。<?php echo CHtml::beginForm(); ?>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'selectableRows' => 2,
'columns' => array(
array(
'id' => 'selectedIds',
'class' => 'CCheckBoxColumn'
),
'id',
'username',
'content',
array(
'name' => 'created',
'value' => '$data->created'
),
),
));
?>
<div>
<?php echo CHtml::submitButton('Approve', array('name' => 'ApproveButton')); ?>
<?php echo CHtml::submitButton('Delete',
array('name' => 'DeleteButton',
'confirm' => 'Are you sure you want to permanently delete these comments?'));
?>
</div>
<?php echo CHtml::endForm(); ?>
注意:通过传递一个“选项,它允许知道哪个按钮被点击在控制器中的提交按钮。 给你的复选框列一个id 我有:'columns' => array(
array(
'class' => 'CCheckBoxColumn'
),
我改变了这种到:'columns' => array(
array(
'id' => 'selectedIds',
'class' => 'CCheckBoxColumn'
),
现在你可以通过引用您选定的行作为一个数组$_POST['selectedIds']。默认情况下,CCheckBoxColumn在GridView模型项目的主键(但你可以改变这一点),所以selectedIds将所选主键的数组。 修改控制器来处理所选的行public function actionApprove()
{
if (isset($_POST['ApproveButton']))
{
if (isset($_POST['selectedIds']))
{
foreach ($_POST['selectedIds'] as $id)
{
$comment = $this->loadModel($id);
$comment->is_published = 1;
$comment->update(array('is_published'));
}
}
}
// similar code for delete button goes here
$criteria = new CDbCriteria();
$criteria->condition = 'is_published = 0';
$criteria->order = 'created DESC';
$dataProvider = new CActiveDataProvider('Comment');
$dataProvider->criteria = $criteria;
$this->render('approve', array(
'dataProvider' => $dataProvider,
));
}
<?php $this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'selectableRows' => 2,
'columns' => array(
array(
'id' => 'selectedIds',
'class' => 'CCheckBoxColumn'
),
'id',
'username',
'content',
array(
'name' => 'created',
'value' => '$data->created'
),
),
));
?>
<div>
<?php echo CHtml::submitButton('Approve', array('name' => 'ApproveButton')); ?>
<?php echo CHtml::submitButton('Delete',
array('name' => 'DeleteButton',
'confirm' => 'Are you sure you want to permanently delete these comments?'));
?>
</div>
<?php echo CHtml::endForm(); ?>
注意:通过传递一个“选项,它允许知道哪个按钮被点击在控制器中的提交按钮。 给你的复选框列一个id 我有:'columns' => array(
array(
'class' => 'CCheckBoxColumn'
),
我改变了这种到:'columns' => array(
array(
'id' => 'selectedIds',
'class' => 'CCheckBoxColumn'
),
现在你可以通过引用您选定的行作为一个数组$_POST['selectedIds']。默认情况下,CCheckBoxColumn在GridView模型项目的主键(但你可以改变这一点),所以selectedIds将所选主键的数组。 修改控制器来处理所选的行public function actionApprove()
{
if (isset($_POST['ApproveButton']))
{
if (isset($_POST['selectedIds']))
{
foreach ($_POST['selectedIds'] as $id)
{
$comment = $this->loadModel($id);
$comment->is_published = 1;
$comment->update(array('is_published'));
}
}
}
// similar code for delete button goes here
$criteria = new CDbCriteria();
$criteria->condition = 'is_published = 0';
$criteria->order = 'created DESC';
$dataProvider = new CActiveDataProvider('Comment');
$dataProvider->criteria = $criteria;
$this->render('approve', array(
'dataProvider' => $dataProvider,
));
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询