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.
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.
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:
I changed this to:
Now you can reference your selected rows as an array via
$_POST['selectedIds']. By default theCCheckBoxColumnwill 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
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.