3

I have a CGridView wigdet with CCheckBoxColumn like this:

$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider,
    'columns'=>array(
        array(
            'class'=>'CCheckBoxColumn',
        ),
        'title',
        ....
    ),
));

Question: how to submit to controller action the checked values? I understand that I need a form, submit button, but I need a clear explanation where to put things, so that search boxes on the top appear.

Thanks in advance.

4
  • do you want to use ajax? Commented Apr 10, 2012 at 5:52
  • @bool.dev not necessarily; search - that's what I want, so that user can search, check, and submit. Commented Apr 10, 2012 at 6:16
  • ok, and what are you expecting in the controller? just the checked records' id(s)? or also the search parameters? don't think search parameters are necessary, btw. Commented Apr 10, 2012 at 6:27
  • have you tried my solution below? Commented Apr 13, 2012 at 8:20

3 Answers 3

4

You do not absolutely need another form. You can just use a link with additional javascript attached to it.

To get the checked values, you can call the javascript function $.fn.yiiGridView.getChecked(containerID,columnID), see here, it returns an array containing the ids.

Full example (with ajax):

In your view:

<?php
$this->widget('zii.widgets.grid.CGridView', array(
   'id'=>'example-grid-view-id', // the containerID for getChecked
   'dataProvider'=>$dataProvider,
   'columns'=>array(
       array(
           'class'=>'CCheckBoxColumn',
           'id'=>'example-check-boxes' // the columnID for getChecked
       ),
       'title',
       ....
   ),
));
?>
<div id="for-link">
<?php
   echo CHtml::ajaxLink('SomeLink',Yii::app->createUrl('somecontroller/someaction'),
        array(
           'type'=>'POST',
           'data'=>'js:{theIds : $.fn.yiiGridView.getChecked("example-grid-view-id","example-check-boxes").toString()}'
           // pay special attention to how the data is passed here
        )
   );
?>
<div>

In your controller:

...
public function actionSomeaction(){
    if(isset($_POST['theIds'])){
          $arra=explode(',', $_POST['theIds']);
          // now do something with the ids in $arra
          ...
    }
    ...
}
...

You could also use json string, instead of simple string, in the data we pass by ajax (from the view), but then instead of explode(), you would use json_decode() (in the controller). Also it would be better to validate/sanitize the ids before use.

Check out the documentation for CHtml::ajaxLink to know more about ajax links.

Note that the example is a little crude, since i haven't put in checks for empty array of checked ids.

Sign up to request clarification or add additional context in comments.

1 Comment

I haven't checked your solution, since I've already done it different way, without using checkboxes :) but I like your solution very much! Thanks for detailed explanation!
1

This one works with CSRF protection and updates the GridView.

<?php
echo CHtml::ajaxLink('ClickMe',Yii::app()->createUrl('controller/action'),
array(
'type'=>'POST',
'data'=>'js:{"ids" : $.fn.yiiGridView.getChecked("grid_view_id","check_box_id").toString(),"YII_CSRF_TOKEN" : "'.Yii::app()->request->csrfToken.'"}',
'success'=>"$('#grid_view_id').yiiGridView.update('grid_view_id')"
));
?>

Comments

0

If you wrap your Gridview into a simple form you can send the checkboxs selected to the value,

An example:

View

<form id="frmSubmit">
<?php
echo CHtml::dropDownList('user_id'
        , null
        , CHtml::listData(User::model()->findAll(), 'USER_ID', 'FULLNAME')
);

echo CHtml::ajaxSubmitButton('Save'
                            , Yii::app()->createUrl('query/ajaxUpdate')
                            , array('success' => 'js:reloadGrid', )
                            , array('class' => 'btn btn-success'));

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'query-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        array(
            'id' => 'query',
            'class'=>'CCheckBoxColumn',
            'selectableRows' => '2',
        ),
        'Contact',
        'Email',
        'Company',
    ),
)); ?>
</form>

<script type="text/javascript">
function reloadGrid(data) {
    $.fn.yiiGridView.update('query-grid');
}
</script>

Controller

public function actionAjaxUpdate()
{
    var_dump($_POST);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.