![](https://i-blog.csdnimg.cn/blog_migrate/08e4e7da870fb267299c7c43e963fcb8.png)
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'pager'=>Yii::app()->params['pager'], //定义在配置文件params.php里面
'ajaxUpdate'=>false,
'columns'=>array(
array(
'class'=>'CCheckBoxColumn',
'value'=>'$data->id', //注意这里用的是$data
'htmlOptions'=>array(
'width'=>'5px',
),
),
array(
'name'=>'序号',
'value'=>'$row+1', //序号$row+1;
'htmlOptions'=>array(
'width'=>'5px',
),
),
'title', //如无属性可直接写一个属性;可直接在数据库取得数据
array(
'name'=>'cid',
'value'=>'$data->category->title',
'htmlOptions'=>array(
'width'=>'80px',
'align'=>'center',
),
),
array(
'class'=>'CButtonColumn',
'header'=>'缩略图',
'buttons'=>array( //自定义按钮
'preview'=>array(
'url'=>'', //可以加createUrl链接 不能www
'imageUrl'=>BASEURL.'/resources/icons/picture.png', //图标图片
'visible'=>'Yii::app()->controller->girdShowImg($data);', //积累控制器方式,如果有缩略图就可见
),
),
'template'=>'{preview}', //自定义一定要加上这句话
),
array(
'name'=>'hits',
'value'=>'$data->hits',
'htmlOptions'=>array(
'width'=>'40px',
'align'=>'center',
),
),
array(
'name'=>'create_time',
'value'=>'date("Y-m-d",$data->create_time)',
'htmlOptions'=>array(
'width'=>'70px',
'align'=>'center',
),
'visible'=>'false',
),
array(
'name'=>'update_time',
'value'=>'date("Y-m-d",$data->update_time)',
'htmlOptions'=>array(
'width'=>'70px',
'align'=>'center',
),
'visible'=>'false',
),
array(
'class'=>'CButtonColumn',
'header'=>'操作',
'viewButtonUrl'=>'Yii::app()->controller->showViewUrl("article",$data)', //查看原文
'viewButtonOptions'=>array('target'=>'_blank'),
'updateButtonUrl'=>'Yii::app()->createUrl("article/update",array(
"id"=>"$data->id","menupanel"=>"$_GET[menupanel]","cid"=>"$_GET[cid]","title"=>"$_GET[title]"))',//编辑 ,然后ajax删除。
),
),
));
?>
基类控制器Controller代码:
public function actionDelete($id) //
{
if(Yii::app()->request->isPostRequest)
{
// we only allow deletion via POST request
if($this->loadModel($id)->delete()){
Yii::app()->user->setFlash('actionInfo',Yii::app()->params['actionInfo']['deleteSuccess']);
}else {
Yii::app()->user->setFlash('actionInfo',Yii::app()->params['actionInfo']['deleteFail']);
}
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_GET['ajax']))
$this->redirect($_POST['returnUrl']);
}
else
throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
}
protected function girdShowImg($data)
{
if(!empty($data->imgurl))
return true;
else
return false;
}
protected function showViewUrl($type,$data){
return str_replace('admin.php','index.php',Yii::app()->createUrl("$type/view",array('id'=>$data->id))); //因为这是在admin环境下
}
扩展:
批量删除的使用:
array(
'selectableRows' => 2,
'footer' => '<button type="button" οnclick="GetCheckbox();" style="width:76px">批量删除</button>',
'class' => 'CCheckBoxColumn',
'headerHtmlOptions' => array('width'=>'1px'),
'checkBoxHtmlOptions' => array('name' => 'selectdel[]'),
),
<script type="text/javascript">
/*<![CDATA[*/
var GetCheckbox = function (){
if (confirm("确定要删除此系别吗 ?")){
var data=new Array();
$("input:checkbox[name='selectdel[]']").each(function (){
if($(this).attr("checked")==true){
data.push($(this).val());
}
});
if(data.length > 0){
$.post('<?php echo CHtml::normalizeUrl(array('/article/delall/'));?>',
{'selectdel[]':data},
function (data) {
var ret = $.parseJSON(data);
if (ret != null && ret.success != null && ret.success) {
// $.fn.yiiGridView.update('yw1');
// alert('删除成功!');
setTimeout("window.location.href='<?php echo Yii::app()->createUrl('article/index',array('menupanel'=>'content|short|article_index'));?>';",1000);
}
// setTimeout("window.location.href='<?php echo Yii::app()->createUrl('article/index',array('menupanel'=>'content|short|article_index'));?>';",1000);
});
}else{
alert("请选择要删除的关键字!");
}
}
}
/*]]>*/
</script>
ublic function actionDelall()
{
if (Yii::app()->request->isPostRequest)
{
$criteria= new CDbCriteria;
$criteria->addInCondition('id', $_POST['selectdel']);
Article::model()->deleteAll($criteria);//Words换成你的模型
if(isset(Yii::app()->request->isAjaxRequest)) {
echo CJSON::encode(array('success' => true));
// Yii::app()->user->setFlash('actionInfo',Yii::app()->params['actionInfo']['deleteSuccess']);
} else
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
}
else
throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
}
其他一些扩展:
array(
'name'=>'content',
'type'=>'html',
'value'=>'mb_substr(htmlspecialchars_decode($data->content),0,150,"utf-8")',
),
array(
'name'=>'title',
'type'=>'raw',
'value'=>'CHtml::link($data->title,Yii::app()->controller->showViewUrl("article",$data))',
),
array(
'name'=>'imgurl',
'type'=>'image',
'value'=>'Yii::app()->request->baseUrl.$data->imgurl',//图片相对路径
),