整理文档,搜刮出一个Yii实现复选框批量操作实例代码,稍微整理精简一下做下分享。
在视图下
PHP;">
widget('zii.widgets.grid.CGridView',array(
'id'=>'user-grid','dataProvider'=>$model->search(),//Model里需要有个search()方法,$model是Controller中传递的Model对象
// /'filter'=>$model,'columns'=>array(
array(
'selectableRows' => 2,'class' => 'CCheckBoxColumn','headerHtmlOptions' => array('width'=>'18px','align'=>'center'),'checkBoxHtmlOptions' => array('name' => 'ids[]',),'homework_numberID','type','target','state','author_id',array('name'=>'state','type'=>'raw','value'=>'($data->state =="not_read" ?"未读":"已审核")'),// array('name'=>'passRecord','type'=>'date'),array('name'=>'submit_time',array(
'class'=>'CButtonColumn',)); ?>
在Model中的search()函数
$criteria=new CDbCriteria;
//$criteria->with = array('author'); //<a href="/tag/tianjia/" target="_blank" class="keywords">添加</a>了和 author 的渴求式加载
// $this->state = "not_passed";
$criteria->compare('state',$this->state);
//这里<a href="/tag/tianjia/" target="_blank" class="keywords">添加</a>了一个 compare,username 是 User 表的字段,$this->name 是我们<a href="/tag/tianjia/" target="_blank" class="keywords">添加</a>的<a href="/tag/shuxing/" target="_blank" class="keywords">属性</a>,true 为模糊<a href="/tag/sousuo/" target="_blank" class="keywords">搜索</a>
//$criteria->compare('username',$this->name,true);
return new CActiveDataProvider($this,array(
'criteria'=>$criteria,));