简体   繁体   English

使用CGridView批量更新

[英]Batch update using CGridView

I want to perform a batch update on some records displayed in a CGridView and using CCheckBoxColumn, but it's not working! 我想对CGridView中显示的某些记录和使用CCheckBoxColumn的记录执行批处理更新,但是它不起作用!
Here is an example scenario: 这是一个示例方案:

Consider the table (MySQL): 考虑表(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`)
)

Add some rows: 添加一些行:

INSERT INTO `tb_test` (`id`, `description`, `active`) VALUES  
(1, 'Test #1', 0),  
(2, 'Test #2', 1),  
(3, 'Test #3', 0);

Then use Gii to generate defaults Model, Controller and CRUD, ok? 然后使用Gii生成默认的模型,控制器和CRUD,好吗?

Well, after that, I made some changes in the Controller: 好吧,之后,我对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);
}

And, finally, here is the test View: 最后,这是测试视图:

<?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(); 
?>

My objective is to get the values os ALL the check-boxes and perform a batch update on the table, setting the column ACTIVE to true or false. 我的目标是获取所有复选框的值并在表上执行批更新,将ACTIVE列设置为true或false。

But the Yii framework just send to Ajax the marked checks . 但是Yii框架只是将标记的支票发送给Ajax。 Through the Firebug I can see the result like: 通过Firebug,我可以看到如下结果:

Array
(
    [0] => 1
    [1] => 0
)

Even if I mark all 3 records! 即使我标记了所有3条记录!

Is there a way of getting the values of ALL the check-boxes? 有没有一种方法来获取所有复选框的值?

Thank you! 谢谢!

1st. 1。 Selectable rows should look like this: 可选行应如下所示:

array(
            'class' => 'CCheckBoxColumn',
            'selectableRows' => '2',
            'id'=>'check_column',
            'value'=>'$data->active',
            'checked'=>'$data->active',
      ),

2nd. 第2位。 You can pass them by your own, so ajaxButton will look like: 您可以自己传递它们,因此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'
));?>

With this method you will get string, separated with comma with your selected rows. 使用此方法,您将获得字符串,并用逗号分隔所选行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM