【问题标题】:How to checked some checkbox at cgridview?如何在 cgridview 中选中某些复选框?
【发布时间】:2012-10-22 11:17:45
【问题描述】:

Q : 如何在 cgridview 中勾选一些复选框?

状态:我已经完成了一个带有复选框的网格视图。但我不知道如何预先检查一些复选框。 $current_reviewers 是一个数组。我想与 $current_reviewers 和复选框匹配以在 gridview 进行预检查。

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'acc-recei-grid',
    'dataProvider'=>$model->search_reviewerlist(),
    'filter'=>$model,
    'columns'=>array(
        array(
            'class' => 'CCheckBoxColumn',
            'selectableRows' => 2,
            'checkBoxHtmlOptions' => array(
                'name' => 'userids[]',
            ),
            'value'=>'$data->id',
            'checked'=>'(in_array($data->id, $current_reviewers) ? 1 : ""',
         ),
        'username',
        array(
            'type'=>'raw',
            'value'=>'$data->id',
            //'filter'=>array('style'=>'visible:none'), 
            //'headerHtmlOptions'=>array('style'=>'width:0px; display:none; border:none; textdecoration:none'),
            'htmlOptions'=>array('style'=>'display:none; border:none;', 'class'=>'user-id'),  
            //'header'=>false,
            //'filter'=>false,
        ),

    ),
)); ?>

【问题讨论】:

  • 感谢您帮助我,VibhaJ。 $current_reviewers 是一个数组。但我可以用 bool.dev 解决问题。感谢 bool.dev。

标签: php yii yii-extensions cgridview


【解决方案1】:

问题在于$current_reviewers 变量,在作为checked 值传递的php 表达式中无法访问它。为此,您可以使用匿名函数并使用外部变量,使用 use 关键字:

'checked'=>function($data, $row) use ($current_reviewers){
                return in_array($data->id, $current_reviewers);
}

检查use keyword的使用情况。

【讨论】:

    【解决方案2】:

    试试这个:

    array(
                'id'=>'id',
                'class'=>'CCheckBoxColumn',
                'selectableRows' => '50',   
                'checked'=>'($data->id==$current_reviewers)?(1):(0)',   
    
            ),
    

    【讨论】:

      猜你喜欢
      • 2022-07-21
      • 2018-10-20
      • 1970-01-01
      • 2015-06-18
      • 2016-12-19
      • 2013-05-10
      • 1970-01-01
      • 1970-01-01
      • 2022-12-18
      相关资源
      最近更新 更多