Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Yii gridview分页中保留复选框值_Php_Ajax_Pagination_Yii - Fatal编程技术网

Php 在Yii gridview分页中保留复选框值

Php 在Yii gridview分页中保留复选框值,php,ajax,pagination,yii,Php,Ajax,Pagination,Yii,我有一个gridview,它包含一个复选框列,还使用分页。当我在第一页中选中一些复选框并导航到第二页并在第二页中选中另一个复选框时,我在第一页中选中的选项不会保留在那里。是否可以在分页期间保留复选框值 Gridview的代码为 $widget = $this->widget('zii.widgets.grid.CGridView', array( 'dataProvider' => $model->search(), 'cssFile'

我有一个gridview,它包含一个复选框列,还使用分页。当我在第一页中选中一些复选框并导航到第二页并在第二页中选中另一个复选框时,我在第一页中选中的选项不会保留在那里。是否可以在分页期间保留复选框值

Gridview的代码为

$widget = $this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'     => $model->search(),
    'cssFile'          => Yii::app()->baseUrl . '/media/js/admin/css/admingridview.css',
    //'filter' => $model,
    'ajaxUpdate'       => true,
    'enablePagination' => true,
    'columns'          => array(
        array(
            'name'   => 'id',
            'header' => '#',
            'value'  => '$this->grid->dataProvider->pagination->currentPage * $this->grid->dataProvider->pagination->pageSize + ($row+1)',
        ),
        array(
            'class'          => 'CCheckBoxColumn',
            'selectableRows' => '2',
            'header' => 'Selected',
        ),
        array(
            'name'   => 'fb_user_id',
            'header' => 'FaceBook Id',
            'value'  => 'CHtml::encode($data->fb_user_id)',
        ),
        array(
            'name'   => 'first_name',
            'header' => 'Name',
            'value'  => 'CHtml::encode($data->first_name)',
        ),
        array(
            'name'   => 'email_id',
            'header' => 'Email',
            'value'  => 'CHtml::encode($data->email_id)',
        ),
        array(
            'name'   => 'demo',
            'type'   => 'raw',
            'header' => "Select",
            'value'  => 'CHtml::checkBox("email[]","",array("class"=>"check","value"=>$data->email_id))',
        ),
    ),
));
编辑:

要在gridview中记住所选选项,请选中此链接


感谢

开发该方案,您需要了解导航时发生的情况

当您导航到分页页面时,会进行ajax调用,并接收新数据,然后从CActive记录或数据源中提取数据。新数据与数据库记录或源记录一致。当您再次回到上一页时,会调用Ajax并更新内容,使其与数据库中的内容相同

我的感觉是,您应该暂时保存已检查项目的数据,并在执行操作时将其永久保存

你可以这样做

<script type="text/javascript">
$("input:checkbox").click(function () {
    var thisCheck = $(this);
    if (thisCheck.is (':checked')){
        // do what you want here, the way to access the text is using the
        // $(this) selector. The following code would output pop up message with
        // the selected checkbox text
        $(this).val());
    }
});
</script>

$(“输入:复选框”)。单击(函数(){
var thisCheck=$(此);
if(thisCheck.is(':checked')){
//在这里做你想做的,访问文本的方法是使用
//$(此)选择器。以下代码将输出带有
//选中的复选框显示文本
$(this.val());
}
});

您可以将临时存储保存在某个位置

您可以使用会话/cookie来存储选中的值。我不太确定如何使cookies工作,所以我将告诉您如何使用会话。特别是yii创建的用户会话

现在要使用会话,我们需要将选中(和未选中)的ID传递给控制器,因此我们将在每次ajax更新时(即分页之间)修改发送给控制器的数据,为此,我们利用的
beforeAjaxUpdate
选项

我还在您的代码中使用而不是(当然,您可以根据自己的需要修改解决方案):

GridView更改: 在上述功能中,请注意选择器和
每个
推送
功能

完成后,我们需要修改此视图的控制器/操作

public function actionShowGrid(){
     // some code already existing
     // additional code follows
     if(isset($_GET['checkedIds'])){
          $chkArray=explode(",", $_GET['checkedIds']);
          foreach ($chkArray as $arow){
               Yii::app()->user->setState($arow,1);
          }
     }
     if(isset($_GET['uncheckedIds'])){
          $unchkArray=explode(",", $_GET['uncheckedIds']);
          foreach ($unchkArray as $arownon){
               Yii::app()->user->setState($arownon,0);
          }
     }
     // rest of the code namely render()
}

就这样,它现在应该可以工作了。

也可以在正常表单上完成此工作提交:

我想在bool.dev的回答中添加这一点作为评论,但我还没有足够的声誉来这么做。所以我不得不把它放在一个单独的答案里

bool.dev,你的答案很好,效果也很好,thanx

然而,正如预期的那样,它只在ajax调用更新gridview时起作用。我将gridview作为表单的一部分,因此我希望它也能正常提交表单,否则当表单上出现其他验证错误时,不会再次加载复选框

因此,除了您所做的之外,我还在表单上添加了隐藏字段,例如:

<input type="hidden" name='checkedBox1' id='checkedBox1' value=''>
<input type="hidden" name='uncheckedBox1' id='uncheckedBox1' value=''>
在控制器中,除了检查$\u GET['checkedIds']之外,还可以使用相同的会话变量检查$\u POST['checkedBox1'],并使用与$\u GET['checkedIds']相同的方式将其值存储到会话中

对$\u POST['uncheckedBox1']执行相同的操作


这应该行。

谢谢你的回复。你能提供一些例子吗?你是否选中了一个数据库字段?所有行的复选框都在吗?我的意思是,你为什么不选择CCheckBoxColumn?@bool.dev我使用了CCheckBoxColumn,但它也没有用除了你想要保留复选框状态这一事实之外,你可以使用CCheckBoxColumn吗?我的意思是,除了复选框状态,你能用ccheckboxcolumn做所有其他事情吗?@bool.dev我只添加了ccheckboxcolumn,所有其他正常工作的pblm都是用复选框值重新设置的。我没有在任何像这样开发的站点中找到OK。。你能在你的问题中添加ccheckboxcolumn版本吗?只添加修改过的部分,同时保留旧代码。非常感谢..将检查它…感谢您耐心地找出解决方案..欢迎您,下次在这些重大问题上悬赏:)。我说过,我会努力的,所以我做到了。无论如何,记住AjaxUpdate之前的要点、会话/cookie以及
setState()
getState()
函数。如果愿意,您可以将内容存储在单独的会话变量中,而不是用户会话中。此外,除了
isset
之外,您可能还需要检查
是否为空
当然……感谢您的大力支持。我今天还遇到了一个扩展,它可能会有所帮助:
Yii::app()->clientScript->registerScript('getUnchecked', "
       function getUncheckeds(){
            var unch = [];
            /*corrected typo: $('[name^=someChec]') => $('[name^=someChecks]') */
            $('[name^=someChecks]').not(':checked,[name$=all]').each(function(){unch.push($(this).val());});
            return unch.toString();
       }
       "
);
public function actionShowGrid(){
     // some code already existing
     // additional code follows
     if(isset($_GET['checkedIds'])){
          $chkArray=explode(",", $_GET['checkedIds']);
          foreach ($chkArray as $arow){
               Yii::app()->user->setState($arow,1);
          }
     }
     if(isset($_GET['uncheckedIds'])){
          $unchkArray=explode(",", $_GET['uncheckedIds']);
          foreach ($unchkArray as $arownon){
               Yii::app()->user->setState($arownon,0);
          }
     }
     // rest of the code namely render()
}
<input type="hidden" name='checkedBox1' id='checkedBox1' value=''>
<input type="hidden" name='uncheckedBox1' id='uncheckedBox1' value=''>
if ($('#checkedBox1').length >0)   {$('[name=checkedBox1]').val(getChecked());}
if ($('#uncheckedBox1').length >0) {$('[name=uncheckedBox1]').val(getUncheckeds());}