How to process selected rows in Yii CGridView?

18,169

Solution 1

I was able to get this working without depending on javascript. The steps are as follows:

Add a form to your view file

This will allow the checkbox values, which are html input elements, to be posted to your controller.

<?php echo CHtml::beginForm(); ?>

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider,
    'selectableRows' => 2,
    'columns' => array(
        array(
            'id' => 'selectedIds',
            'class' => 'CCheckBoxColumn'
        ),
        'id',
        'username',
        'content',
        array(
            'name' => 'created',
            'value' => '$data->created'
        ),
    ), 
));
?>

<div>
<?php echo CHtml::submitButton('Approve', array('name' => 'ApproveButton')); ?>
<?php echo CHtml::submitButton('Delete', 
array('name' => 'DeleteButton',
'confirm' => 'Are you sure you want to permanently delete these comments?'));
?>
</div>

<?php echo CHtml::endForm(); ?>

Note by passing in a "Name" option to the submitButton it allows to know which button was clicked in the controller.

Give your check box column an id

Previously I had:

'columns' => array(
    array(
        'class' => 'CCheckBoxColumn'
    ),

I changed this to:

'columns' => array(
    array(
        'id' => 'selectedIds',
        'class' => 'CCheckBoxColumn'
    ),

Now you can reference your selected rows as an array via $_POST['selectedIds']. By default the CCheckBoxColumn will use the primary key of your model items in the gridview (but you can change this), so selectedIds will be an array of the selected primary keys.

Modify controller to process the selected rows

public function actionApprove()
{
    if (isset($_POST['ApproveButton']))
    {
        if (isset($_POST['selectedIds']))
        {
            foreach ($_POST['selectedIds'] as $id)
            {
                $comment = $this->loadModel($id);
                $comment->is_published = 1;
                $comment->update(array('is_published'));
            }
        }
    }

    // similar code for delete button goes here

    $criteria = new CDbCriteria();
    $criteria->condition = 'is_published = 0';
    $criteria->order = 'created DESC';

    $dataProvider = new CActiveDataProvider('Comment');
    $dataProvider->criteria = $criteria;

    $this->render('approve', array(
        'dataProvider' => $dataProvider,
    ));
}

I used this Yii wiki article to help me come up with this solution: Working with CGridView in Admin Panel

Not sure if this is the ideal way to do it but it works. I'm open to suggestions for improvement or other methods.

Solution 2

Take a look at the docs :

A selected row will have a CSS class named 'selected'. You may also call the JavaScript function $.fn.yiiGridView.getSelection(containerID) to retrieve the key values of the selected rows.

So i guess JS is only way to go in your case.

Share:
18,169
User
Author by

User

Updated on June 05, 2022

Comments

  • User
    User almost 2 years

    I have a CGridView with a CCheckBoxColumn and selectableRows = 2. Users can select multiple rows of my grid view.

    The gridview is for a list of unapproved comments and I want to be able to select comments to approve, hit an "Approve" button and update a field in the comments table for each comment that was approved.

    How can I do this?

    I'm open to an ajax solution but really what I want to know is how to get the list of comment ids inside my controller action code with a normal post. I'd prefer a solution that doesn't involve javascript unless that is the only option.

  • User
    User over 11 years
    Where I think this answer could be improved is having it use a form model.
  • Gaurav Parashar
    Gaurav Parashar about 10 years
    can you please tell where i have to put this function ?