【发布时间】:2014-10-16 04:49:49
【问题描述】:
我想对显示在 CGridView 中并使用 CCheckBoxColumn 的一些记录执行批量更新,但它不起作用!
这是一个示例场景:
考虑表(MySQL):
CREATE TABLE IF NOT EXISTS `tb_test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`description` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
`active` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
)
添加一些行:
INSERT INTO `tb_test` (`id`, `description`, `active`) VALUES
(1, 'Test #1', 0),
(2, 'Test #2', 1),
(3, 'Test #3', 0);
然后使用 Gii 生成默认的 Model、Controller 和 CRUD,好吗?
嗯,在那之后,我在Controller中做了一些改动:
// unlock "active" and "ajaxupdate"
public function accessRules()
{
...
array('allow',
'actions'=>array('active','ajaxupdate'),
'users'=>array('*'),
),
...
}
// ...other stuff
// action Active
public function actionActive()
{
$model=new Test('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Test']))
$model->attributes=$_GET['Test'];
$this->render('active',array(
'model'=>$model,
));
}
// action AjaxUpdate
public function actionAjaxUpdate()
{
$check_column = $_POST['check_column'];
print_r($check_column);
}
最后,这是测试视图:
<?php
$form=$this->beginWidget('CActiveForm', array(
'enableAjaxValidation'=>true,
));
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'test-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'enablePagination'=>false,
'selectableRows'=>2,
'columns'=>array(
array(
'id'=>'check_column',
'class'=>'CCheckBoxColumn',
'value'=>'$data->active',
'checked'=>'$data->active',
),
'id',
'description',
array(
'class'=>'CButtonColumn',
),
),
));
?>
<script>
function reloadGrid(data) {
$.fn.yiiGridView.update('test-grid');
}
</script>
<?php
echo CHtml::ajaxSubmitButton('Update All',array('test/ajaxupdate'), array('success'=>'reloadGrid'));
$this->endWidget();
?>
我的目标是获取所有复选框的值并对表执行批量更新,将 ACTIVE 列设置为 true 或 false。
但 Yii 框架只是将标记的检查发送给 Ajax。 通过 Firebug,我可以看到如下结果:
Array
(
[0] => 1
[1] => 0
)
即使我标记了所有 3 条记录!
有没有办法获取所有复选框的值?
谢谢!
【问题讨论】:
标签: php checkbox yii cgridview