Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用CGridView进行批更新_Php_Checkbox_Yii_Cgridview - Fatal编程技术网

Php 使用CGridView进行批更新

Php 使用CGridView进行批更新,php,checkbox,yii,cgridview,Php,Checkbox,Yii,Cgridview,我想对CGridView中显示的一些记录执行批更新,并使用CCheckBoxColumn,但它不起作用 下面是一个示例场景: 以表(MySQL)为例: 添加一些行: INSERT INTO `tb_test` (`id`, `description`, `active`) VALUES (1, 'Test #1', 0), (2, 'Test #2', 1), (3, 'Test #3', 0); 然后使用Gii生成默认模型、控制器和CRUD,好吗 在那之后,我对控制器做了一些修改

我想对CGridView中显示的一些记录执行批更新,并使用CCheckBoxColumn,但它不起作用
下面是一个示例场景:

以表(MySQL)为例:

添加一些行:

INSERT INTO `tb_test` (`id`, `description`, `active`) VALUES  
(1, 'Test #1', 0),  
(2, 'Test #2', 1),  
(3, 'Test #3', 0);
然后使用Gii生成默认模型、控制器和CRUD,好吗

在那之后,我对控制器做了一些修改:

// 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(); 
?>
即使我标记了所有3条记录

是否有办法获取所有复选框的值


谢谢大家!

1。可选行应如下所示:

array(
            'class' => 'CCheckBoxColumn',
            'selectableRows' => '2',
            'id'=>'check_column',
            'value'=>'$data->active',
            'checked'=>'$data->active',
      ),
第二。您可以自己传递它们,因此ajaxButton看起来像:

<?php echo CHtml::ajaxButton(Yii::t("app","grid_update"), 'test/ajaxupdate', array(
    'type'=>'POST',
    'data'=>'js:{ids : $.fn.yiiGridView.getChecked("grid-id-here","check_columns").toString()}',
    'success' =>
        'js:function (data) {
               //do what you need here
           }',
), array(
    'id' => 'update_btn_'.rand(0,255),
    'class' => 'update_btn'
));?>

Hi@ineersa!谢谢你的帮助!“使用此方法,您将获得字符串,并用逗号与所选行分隔。”这就是问题所在。我需要所有的行,而不仅仅是所选的行。好吧,没有这样的方法,你可以自己写,但在我看来这是没用的。编写
时,对表执行批处理更新,将列ACTIVE设置为true或false
,因此只需选中复选框并将其ACTIVE设置为true,将默认值设置为false。Hi@ineersa!我举的例子只是一个假想。实际情况更复杂,像这样的方法对我很有用。但是好的,我会尝试一种解决方法。谢谢
array(
            'class' => 'CCheckBoxColumn',
            'selectableRows' => '2',
            'id'=>'check_column',
            'value'=>'$data->active',
            'checked'=>'$data->active',
      ),
<?php echo CHtml::ajaxButton(Yii::t("app","grid_update"), 'test/ajaxupdate', array(
    'type'=>'POST',
    'data'=>'js:{ids : $.fn.yiiGridView.getChecked("grid-id-here","check_columns").toString()}',
    'success' =>
        'js:function (data) {
               //do what you need here
           }',
), array(
    'id' => 'update_btn_'.rand(0,255),
    'class' => 'update_btn'
));?>