Submit CGridView Checked values using a form

后端 未结 3 1081
猫巷女王i
猫巷女王i 2021-01-20 08:03

I have a CGridView wigdet with CCheckBoxColumn like this:

$this->widget(\'zii.widgets.grid.CGridView\', array(
    \'dataProvider\'=>$dataProvider,
            


        
3条回答
  •  执笔经年
    2021-01-20 08:41

    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:

    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',
           ....
       ),
    ));
    ?>
    

    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.

提交回复
热议问题