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!