【问题标题】:Batch update using CGridView使用 CGridView 批量更新
【发布时间】: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


    【解决方案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'
    ));?>
    

    使用此方法,您将获得字符串,用逗号分隔您选择的行。

    【讨论】:

    • 嗨@ineersa!谢谢你的帮助! “使用这种方法,您将获得字符串,用逗号与您选择的行分隔。”那就是问题所在。我需要所有行,而不仅仅是选定的:/
    • 嗯,没有这种方法,你可以自己写,但我觉得有点没用。正如您所写的perform a batch update on the table, setting the column ACTIVE to true or false,因此只需选中复选框并将您的 ACTIVE 设置为 true 并将默认值设置为 false。
    • 嗨@ineersa!好吧,我给出的例子只是一个假场景。实际情况更复杂,这样的方法对我很有用。但是好的,我会尝试一种解决方法。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-23
    • 2021-10-24
    • 2021-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    相关资源
    最近更新 更多