doudao2954 2014-10-15 20: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):

  1. CREATE TABLE IF NOT EXISTS `tb_test` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `description` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
  4. `active` tinyint(1) NOT NULL DEFAULT '0',
  5. PRIMARY KEY (`id`)
  6. )

Add some rows:

  1. INSERT INTO `tb_test` (`id`, `description`, `active`) VALUES
  2. (1, 'Test #1', 0),
  3. (2, 'Test #2', 1),
  4. (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:

  1. // unlock "active" and "ajaxupdate"
  2. public function accessRules()
  3. {
  4. ...
  5. array('allow',
  6. 'actions'=>array('active','ajaxupdate'),
  7. 'users'=>array('*'),
  8. ),
  9. ...
  10. }
  11. // ...other stuff
  12. // action Active
  13. public function actionActive()
  14. {
  15. $model=new Test('search');
  16. $model->unsetAttributes(); // clear any default values
  17. if(isset($_GET['Test']))
  18. $model->attributes=$_GET['Test'];
  19. $this->render('active',array(
  20. 'model'=>$model,
  21. ));
  22. }
  23. // action AjaxUpdate
  24. public function actionAjaxUpdate()
  25. {
  26. $check_column = $_POST['check_column'];
  27. print_r($check_column);
  28. }

And, finally, here is the test View:

  1. <?php
  2. $form=$this->beginWidget('CActiveForm', array(
  3. 'enableAjaxValidation'=>true,
  4. ));
  5. $this->widget('zii.widgets.grid.CGridView', array(
  6. 'id'=>'test-grid',
  7. 'dataProvider'=>$model->search(),
  8. 'filter'=>$model,
  9. 'enablePagination'=>false,
  10. 'selectableRows'=>2,
  11. 'columns'=>array(
  12. array(
  13. 'id'=>'check_column',
  14. 'class'=>'CCheckBoxColumn',
  15. 'value'=>'$data->active',
  16. 'checked'=>'$data->active',
  17. ),
  18. 'id',
  19. 'description',
  20. array(
  21. 'class'=>'CButtonColumn',
  22. ),
  23. ),
  24. ));
  25. ?>
  26. <script>
  27. function reloadGrid(data) {
  28. $.fn.yiiGridView.update('test-grid');
  29. }
  30. </script>
  31. <?php
  32. echo CHtml::ajaxSubmitButton('Update All',array('test/ajaxupdate'), array('success'=>'reloadGrid'));
  33. $this->endWidget();
  34. ?>

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:

  1. Array
  2. (
  3. [0] => 1
  4. [1] => 0
  5. )

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-15 23:21
    关注

    1st. Selectable rows should look like this:

    1. array(
    2. 'class' => 'CCheckBoxColumn',
    3. 'selectableRows' => '2',
    4. 'id'=>'check_column',
    5. 'value'=>'$data->active',
    6. 'checked'=>'$data->active',
    7. ),

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

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

    With this method you will get string, separated with comma with your selected rows.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部