使用表单提交CGridView Checked值 [英] Submit CGridView Checked values using a form

查看:61
本文介绍了使用表单提交CGridView Checked值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有CCheckBoxColumn的CGridView wigdet,像这样:

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.

谢谢.

推荐答案

您不一定不需要其他表格.您只需使用附加了附加JavaScript的链接即可.

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

要获取检查值,可以调用javascript函数$.fn.yiiGridView.getChecked(containerID,columnID),请参见

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.

您认为:

<?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>

在您的控制器中:

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

在我们通过ajax传递的数据中(从视图中),您也可以使用json字符串而不是简单字符串,但是您可以使用json_decode()(在控制器中)代替explode().另外,最好在使用前验证/清除ID.

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.

请注意,该示例有点粗糙,因为我还没有检查空的已检查ID数组.

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

这篇关于使用表单提交CGridView Checked值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆