doudao2954 2014-10-16 04:49
浏览 35
已采纳

使用CGridView批量更新

I want to perform a batch update on some records displayed in a CGridView and using CCheckBoxColumn, but it's not working!
Here is an example scenario:

Consider the table (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?

Well, after that, I made some changes in the 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.

But the Yii framework just send to Ajax the marked checks. Through the Firebug I can see the result like:

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

Even if I mark all 3 records!

Is there a way of getting the values of ALL the check-boxes?

Thank you!

  • 写回答

1条回答 默认 最新

  • dongtuojuan8998 2014-10-16 07:21
    关注

    1st. Selectable rows should look like this:

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

    2nd. You can pass them by your own, so ajaxButton will look like:

    <?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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮